Having trouble getting the desired array of arrays from my socket.io emitter. The structure I need is:
[ [{...},{...},{...}] , [{...},{...}] , [{...}] ]
But instead, I am receiving a different format.
https://i.stack.imgur.com/3PY0u.jpg
I require all the arrays to be in one master array for rendering bootstrap cards for each sub array.
Here is the client-side code snippet:
const socket = io("http://localhost:5000");
socket.on('data', (dta) => {
handleData(dta.data);
})
function handleData(data) {
const masterArray= [];
masterArray.push(data);
console.log(masterArray);
}
And here's the server-side code:
for(let i = 0 ; i < alarmpanels.length ; i++) {
const ElkClient = elkClient.ElkClient;
let client = new ElkClient({
connection: {
name: alarmpanels[i].name,
host: alarmpanels[i].host,
port: alarmpanels[i].port,
secure: alarmpanels[i].secure,
zones: alarmpanels[i].zones
}
});
connectClient(client);
}
async function connectClient(client) {
await client.connect();
const zonesArray = client.options.connection.zones;
const arr = [];
try {
const clhost = client.options.connection.host;
const clport = client.options.connection.port;
const clsecure = client.options.connection.secure;
let data = await client.getArmingStatus();
for (i = 0 ; i < zonesArray.length ; i ++) {
const armUpState = await data.areas[i].armUpState;
const clName = client.options.connection.name;
const zoneName = zonesArray[i].name;
const siteName = zonesArray[i].site;
const clzone = zonesArray[i].zone;
const totalPanels = zonesArray[i].length;
const info = new PanelStatus(clhost, clport ,clsecure, clzone, siteName, clName, zoneName, armUpState, totalPanels);
arr.push(info);
}
io.on('connection', (socket, req) => {
socket.emit('data', {data: arr});
})
}
catch (err) {
console.log("Connection Lost!");
}
}