diff --git a/client/connection.js b/client/connection.js index 9a76e7d..b9cc27e 100644 --- a/client/connection.js +++ b/client/connection.js @@ -17,6 +17,10 @@ const Connection = function() { this.connect(); }); + document.addEventListener('G-heartbeat', () => { + this.heartbeat(); + }); + document.addEventListener('L-newround', () => this.send({ type: 'newround' }) ); document.addEventListener('L-objective', () => this.send({ type: 'objective' }) ); @@ -49,13 +53,16 @@ Connection.prototype.send = function(json) { } }; +Connection.prototype.heartbeat = function() { + setTimeout(this.send.bind(this, { type: 'heartbeat' }), 30000); +}; + //===== Connection event handlers Connection.prototype.onOpen = function() { this.retryCounter = 0; - clearTimeout(this.heartbeatTimer); - this.heartbeatTimer = setTimeout(() => { this.ws.send({ type: 'heartbeat' }); }, 30000); + this.heartbeat(); const evt = new Event('L-conn-open'); document.dispatchEvent(evt); @@ -95,6 +102,7 @@ Connection.prototype.onReceiveMessage = function({ data }) { case 'connected': eventName = 'G-connected'; break; case 'countdown': eventName = 'G-countdown'; break; case 'full': eventName = 'G-full'; break; + case 'heartbeat': eventName = 'G-heartbeat'; break; case 'newround': eventName = 'G-newround'; break; case 'objective': eventName = 'G-objective'; break; case 'players': eventName = 'G-players'; break; diff --git a/server/ricochet.js b/server/ricochet.js index 5748f19..e3d059d 100644 --- a/server/ricochet.js +++ b/server/ricochet.js @@ -248,7 +248,7 @@ Ricochet.prototype.onMessage = function(ws, rawBody) { }; //===== Message handlers -Ricochet.prototype.msgHeartbeat = function() { +Ricochet.prototype.msgHeartbeat = function(ws) { this.messenger.messageOne(ws, { type: 'heartbeat' }); };