Browse Source

Cancel keep alive on join room

RobinLinus 6 years ago
parent
commit
0731a21d68
1 changed files with 7 additions and 6 deletions
  1. 7 6
      server/index.js

+ 7 - 6
server/index.js

@@ -111,6 +111,7 @@ class SnapdropServer {
 
 
     _send(peer, message) {
     _send(peer, message) {
         if (!peer) return console.error('undefined peer');
         if (!peer) return console.error('undefined peer');
+        if (this._wss.readyState !== this._wss.OPEN) return console.error('Socket is closed');
         message = JSON.stringify(message);
         message = JSON.stringify(message);
         peer.socket.send(message, error => {
         peer.socket.send(message, error => {
             if (error) this._leaveRoom(peer);
             if (error) this._leaveRoom(peer);
@@ -119,17 +120,16 @@ class SnapdropServer {
 
 
     _keepAlive(peer) {
     _keepAlive(peer) {
         var timeout = 10000;
         var timeout = 10000;
-        // console.log(Date.now() - peer.lastBeat);
+        if (!peer.lastBeat) {
+            peer.lastBeat = Date.now();
+        }
         if (Date.now() - peer.lastBeat > 2 * timeout) {
         if (Date.now() - peer.lastBeat > 2 * timeout) {
             this._leaveRoom(peer);
             this._leaveRoom(peer);
             return;
             return;
         }
         }
 
 
-        if (this._wss.readyState == this._wss.OPEN) {
-            this._send(peer, {
-                type: 'ping'
-            });
-        }
+        this._send(peer, { type: 'ping' });
+
         this._cancelKeepAlive(peer);
         this._cancelKeepAlive(peer);
         peer.timerId = setTimeout(() => this._keepAlive(peer), timeout);
         peer.timerId = setTimeout(() => this._keepAlive(peer), timeout);
     }
     }
@@ -137,6 +137,7 @@ class SnapdropServer {
     _cancelKeepAlive(peer) {
     _cancelKeepAlive(peer) {
         if (peer.timerId) {
         if (peer.timerId) {
             clearTimeout(peer.timerId);
             clearTimeout(peer.timerId);
+            peer.lastBeat = null;
         }
         }
     }
     }
 }
 }