add ping pong to make sure websocket connection doesn't go stale

This commit is contained in:
liamcottle 2025-02-08 11:53:10 +13:00
commit 9e7d0cdfeb
2 changed files with 27 additions and 1 deletions

View file

@ -2081,8 +2081,14 @@ class ReticulumMeshChat:
# get type from client data
_type = data["type"]
# handle ping
if _type == "ping":
AsyncUtils.run_async(client.send_str(json.dumps({
"type": "pong",
})))
# handle updating config
if _type == "config.set":
elif _type == "config.set":
# get config from websocket
config = data["config"]

View file

@ -3,8 +3,18 @@ import mitt from 'mitt';
class WebSocketConnection {
constructor() {
this.emitter = mitt();
this.reconnect();
/**
* ping websocket server every 30 seconds
* this helps to prevent the underlying tcp connection from going stale when there's no traffic for a long time
*/
setInterval(() => {
this.ping();
}, 30000);
}
// add event listener
@ -47,6 +57,16 @@ class WebSocketConnection {
}
}
ping() {
try {
this.send(JSON.stringify({
"type": "ping",
}));
} catch(e) {
// ignore error
}
}
}
export default new WebSocketConnection();