mirror of
https://gitlab.com/futo-org/fcast.git
synced 2025-06-24 21:25:23 +00:00
Receivers: Fixed logic issues with connection monitor
This commit is contained in:
parent
270a998e18
commit
9b47627801
9 changed files with 39 additions and 57 deletions
|
@ -13,26 +13,30 @@ export function setUiUpdateCallbacks(callbacks: any) {
|
|||
}
|
||||
|
||||
// Window might be re-created while devices are still connected
|
||||
window.targetAPI.onPing((_event, value: any) => {
|
||||
function onPingPong(value: any) {
|
||||
if (value) {
|
||||
heartbeatRetries[value.id] = 0;
|
||||
heartbeatRetries[value.sessionId] = 0;
|
||||
|
||||
if (!connections.includes(value.id)) {
|
||||
connections.push(value.id);
|
||||
uiUpdateCallbacks.onConnect(connections, value.id);
|
||||
if (!connections.includes(value.sessionId)) {
|
||||
connections.push(value.sessionId);
|
||||
uiUpdateCallbacks.onConnect(connections, value.sessionId);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
window.targetAPI.onPing((_event, value: any) => onPingPong(value));
|
||||
window.targetAPI.onPong((_event, value: any) => onPingPong(value));
|
||||
|
||||
window.targetAPI.onConnect((_event, value: any) => {
|
||||
connections.push(value.id);
|
||||
console.log(`Device connected: ${JSON.stringify(value)}`);
|
||||
connections.push(value.sessionId);
|
||||
uiUpdateCallbacks.onConnect(connections, value);
|
||||
});
|
||||
window.targetAPI.onDisconnect((_event, value: any) => {
|
||||
console.log(`Device disconnected: ${JSON.stringify(value)}`);
|
||||
const index = connections.indexOf(value.id);
|
||||
const index = connections.indexOf(value.sessionId);
|
||||
if (index != -1) {
|
||||
connections.splice(index, 1);
|
||||
uiUpdateCallbacks.onDisconnect(connections, value.id);
|
||||
uiUpdateCallbacks.onDisconnect(connections, value.sessionId);
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -40,13 +44,13 @@ setInterval(() => {
|
|||
if (connections.length > 0) {
|
||||
window.targetAPI.sendSessionMessage(Opcode.Ping, null);
|
||||
|
||||
for (const session of connections) {
|
||||
if (heartbeatRetries[session] > 3) {
|
||||
console.warn(`Could not ping device with connection id ${session}. Disconnecting...`);
|
||||
window.targetAPI.disconnectDevice(session);
|
||||
for (const sessionId of connections) {
|
||||
if (heartbeatRetries[sessionId] > 3) {
|
||||
console.warn(`Could not ping device with connection id ${sessionId}. Disconnecting...`);
|
||||
window.targetAPI.disconnectDevice(sessionId);
|
||||
}
|
||||
|
||||
heartbeatRetries[session] = heartbeatRetries[session] === undefined ? 1 : heartbeatRetries[session] + 1;
|
||||
heartbeatRetries[sessionId] = heartbeatRetries[sessionId] === undefined ? 1 : heartbeatRetries[sessionId] + 1;
|
||||
}
|
||||
}
|
||||
}, connectionPingTimeout);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue