projects
/
vchess.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Small fix + attempt to improve socket messages reliability...
[vchess.git]
/
server
/
sockets.js
diff --git
a/server/sockets.js
b/server/sockets.js
index
c2b41e8
..
520b8b7
100644
(file)
--- a/
server/sockets.js
+++ b/
server/sockets.js
@@
-18,6
+18,12
@@
function send(socket, message) {
socket.send(JSON.stringify(message));
}
socket.send(JSON.stringify(message));
}
+// https://www.npmjs.com/package/ws - detect lost connections...
+function noop() {}
+function heartbeat() {
+ this.isAlive = true;
+}
+
module.exports = function(wss) {
// Associative array page --> sid --> tmpId --> socket
// "page" is either "/" for hall or "/game/some_gid" for Game,
module.exports = function(wss) {
// Associative array page --> sid --> tmpId --> socket
// "page" is either "/" for hall or "/game/some_gid" for Game,
@@
-36,6
+42,8
@@
module.exports = function(wss) {
});
}
wss.on("connection", (socket, req) => {
});
}
wss.on("connection", (socket, req) => {
+ socket.isAlive = true;
+ socket.on('pong', heartbeat);
const query = getJsonFromUrl(req.url);
const sid = query["sid"];
const id = query["id"];
const query = getJsonFromUrl(req.url);
const sid = query["sid"];
const id = query["id"];
@@
-368,4
+376,15
@@
module.exports = function(wss) {
socket.on("message", messageListener);
socket.on("close", closeListener);
});
socket.on("message", messageListener);
socket.on("close", closeListener);
});
+ const interval = setInterval(
+ () => {
+ wss.clients.forEach(ws => {
+ if (ws.isAlive === false) return ws.terminate();
+ ws.isAlive = false;
+ ws.ping(noop);
+ });
+ },
+ 30000
+ );
+ wss.on('close', () => clearInterval(interval));
}
}