mirror of
https://github.com/liamcottle/reticulum-meshchat.git
synced 2026-10-26 02:48:47 -04:00
add ping pong to make sure websocket connection doesn't go stale
This commit is contained in:
parent
e6ff5097c0
commit
9e7d0cdfeb
2 changed files with 27 additions and 1 deletions
|
|
@ -2081,8 +2081,14 @@ class ReticulumMeshChat:
|
||||||
# get type from client data
|
# get type from client data
|
||||||
_type = data["type"]
|
_type = data["type"]
|
||||||
|
|
||||||
|
# handle ping
|
||||||
|
if _type == "ping":
|
||||||
|
AsyncUtils.run_async(client.send_str(json.dumps({
|
||||||
|
"type": "pong",
|
||||||
|
})))
|
||||||
|
|
||||||
# handle updating config
|
# handle updating config
|
||||||
if _type == "config.set":
|
elif _type == "config.set":
|
||||||
|
|
||||||
# get config from websocket
|
# get config from websocket
|
||||||
config = data["config"]
|
config = data["config"]
|
||||||
|
|
|
||||||
|
|
@ -3,8 +3,18 @@ import mitt from 'mitt';
|
||||||
class WebSocketConnection {
|
class WebSocketConnection {
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
|
|
||||||
this.emitter = mitt();
|
this.emitter = mitt();
|
||||||
this.reconnect();
|
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
|
// 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();
|
export default new WebSocketConnection();
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue