Detect broken connection via app layer ping

This commit is contained in:
Martin Bauer
2021-07-23 16:25:26 +02:00
parent db3589c86d
commit 03812fe514
2 changed files with 38 additions and 12 deletions

View File

@@ -14,6 +14,7 @@ const OpCodes = {
ANSWER_SESSION_LIST: 7,
WIFI_STATE_RESPONSE: 8,
WIFI_SCAN_RESPONSE: 9,
APP_LAYER_PING: 10,
// from frontend to device
START_SESSION: 128,
@@ -26,11 +27,12 @@ const OpCodes = {
WIFI_TRIGGER_SCAN: 135,
};
const HEARTBEAT_TIMEOUT = 3000;
export default class SwimTrackerWebsocketConnection {
constructor(swimTrackerHost, onData, onStarted, onStopped, onWifiStateInfo, onConnect, onDisconnect) {
this.swimTrackerHost = swimTrackerHost;
this.onData = onData;
this.onStarted = onStarted;
this.onStopped = onStopped;
@@ -57,15 +59,27 @@ export default class SwimTrackerWebsocketConnection {
});
this._wifiScanPromises = [];
this.pingTimeout = null;
}
heartbeat() {
clearTimeout(this.pingTimeout);
let connection = this;
this.pingTimeout = setTimeout(() => {
connection.ws.reconnect();
}, HEARTBEAT_TIMEOUT);
}
close() {
this.ws.onmessage = null;
this.ws.onopen = null;
this.ws.onclose = null;
this.ws.onerror = null;
this.ws.close();
this.ws = null;
if (this.ws !== null) {
this.ws.onmessage = null;
this.ws.onopen = null;
this.ws.onclose = null;
this.ws.onerror = null;
this.ws.close();
this.ws = null;
}
}
sendStartCommand() {
@@ -128,6 +142,8 @@ export default class SwimTrackerWebsocketConnection {
const dv = new DataView(e.data);
const opCode = dv.getInt8(0);
const payload = new Uint8Array(e.data).slice(1);
this.heartbeat();
if (opCode === OpCodes.INITIAL_INFO) {
const headerSize = 6;
@@ -157,6 +173,8 @@ export default class SwimTrackerWebsocketConnection {
} else if (opCode === OpCodes.WIFI_STATE_RESPONSE) {
const wifiInfo = msgpack.decode(payload, { codec: this.msgpackCodec });
this.onWifiStateInfo(wifiInfo);
} else if (opCode === OpCodes.APP_LAYER_PING) {
//console.log("got heartbeat");
}
}