add a queue for all sent messages

This commit is contained in:
sparks-alec
2023-05-19 03:32:10 -04:00
parent 37a3fde2e0
commit e90aa6fc17
+19 -2
View File
@@ -44,6 +44,7 @@ function registerDevice(newDevice, discoveryMethod) {
lastDrawn: 0,
lastHeartbeat: 0,
lastMessage: 0,
sendQueue: [],
heartbeatInterval: PLUGINS.all[newDevice.type].heartbeatInterval,
heartbeatTimeout: PLUGINS.all[newDevice.type].heartbeatTimeout,
draw() {
@@ -130,7 +131,12 @@ function initDeviceConnection(id) {
device.lastMessage = Date.now();
});
device.send = (address, args) => {
device.connection.send({ address, args });
const addr = address;
const arg = args;
device.sendQueue.push({ address: addr, args: arg });
};
device.sendNow = (data) => {
device.connection.send(data);
};
} else if (plugins[type].config.connectionType === 'TCPsocket') {
device.connection = new net.Socket();
@@ -159,6 +165,9 @@ function initDeviceConnection(id) {
});
device.send = (data) => {
// log("SOCK OUT", data);
device.sendQueue.push(data);
};
device.sendNow = (data) => {
device.connection.write(data);
};
} else if (plugins[type].config.connectionType === 'UDPsocket') {
@@ -175,6 +184,9 @@ function initDeviceConnection(id) {
});
device.send = (data) => {
device.sendQueue.push(data);
};
device.sendNow = (data) => {
device.connection.send(Buffer.from(data), device.port, device.addresses[0], (err) => {
// console.log(err);
});
@@ -324,9 +336,14 @@ function heartbeat() {
}
d.lastHeartbeat = Date.now();
}
if (d.sendQueue.length > 0 && d.sendNow) {
d.sendNow(d.sendQueue[0]);
d.sendQueue.shift();
}
});
}
setInterval(heartbeat, 100);
setInterval(heartbeat, 50);
function isDeviceAlreadyAdded(newDevice) {
let deviceAlreadyAdded = false;