commit 034b8974b002f1e50e6d6c301e0d3e4b03bc0ff8
parent 5302412294f80bfd6f5ae4ddb553e4c1e4b28643
Author: finwo <finwo@pm.me>
Date: Fri, 16 Nov 2018 12:36:06 +0100
wsrpc now has a heartbeat
Diffstat:
1 file changed, 12 insertions(+), 1 deletion(-)
diff --git a/src/custom/wsrpc.js b/src/custom/wsrpc.js
@@ -75,13 +75,24 @@ module.exports = function (location, options, callback) {
// Handle auth & callback
ws.on('open', function() {
if (auth) return db.auth(auth,cb);
- cb();
+ if ('function' === typeof cb) cb();
});
// Handle reconnect
ws.on('close', function() {
setTimeout(reconnect,10);
});
+
+ // Heartbeat
+ ws.isAlive = true;
+ ws.on('pong', function() {
+ ws.isAlive = true;
+ });
+ setTimeout(function heartbeat() {
+ if (!ws.isAlive) return reconnect();
+ ws.isAlive = false;
+ ws.ping();
+ })
}
// Initial connect