Przeglądaj źródła

expire correctly

ericz 12 lat temu
rodzic
commit
44ee59e18e
1 zmienionych plików z 25 dodań i 11 usunięć
  1. 25 11
      lib/server.js

+ 25 - 11
lib/server.js

@@ -16,14 +16,16 @@ function PeerServer(options) {
   this._app.use(express.bodyParser());
   this._app.use(this._allowCrossDomain);
 
-  options = util.extend({
-    port: 80
+  this._options = util.extend({
+    port: 80,
+    debug: false,
+    timeout: 5000
   }, options);
 
-  util.debug = options.debug;
+  util.debug = this._options.debug;
 
   // Listen on user-specified port and create WebSocket server as well.
-  this._httpServer.listen(options.port);
+  this._httpServer.listen(this._options.port);
   this._wss = new WebSocketServer({ path: '/ws', server: this._httpServer });
 
   // WebSockets that are opened or HTTP responses (which are paired with
@@ -119,14 +121,15 @@ PeerServer.prototype._initializeWSS = function() {
 /** Process outstanding peer offers. */
 PeerServer.prototype._processOutstandingOffers = function(id) {
   var offers = this._outstandingOffers[id];
-  if (offers === undefined)
+  if (offers === undefined) {
     return;
+  }
   var sources = Object.keys(offers);
   for (var i = 0, ii = sources.length; i < ii; i += 1) {
     var messages = offers[sources[i]];
-    for (var j = 0, jj = messages.length; j < jj; j += 1)
+    for (var j = 0, jj = messages.length; j < jj; j += 1) {
       this._handleTransmission.apply(this, messages[j]);
-
+    }
     delete this._outstandingOffers[id][sources[i]];
   }
 };
@@ -255,16 +258,27 @@ PeerServer.prototype._handleTransmission = function(message, res) {
     // messages.
     if (type !== 'LEAVE') {
       var self = this;
-      if (!this._outstandingOffers[dst])
+      if (!this._outstandingOffers[dst]) {
         this._outstandingOffers[dst] = {};
-      if (!this._outstandingOffers[dst][src])
+      }
+      if (!this._outstandingOffers[dst][src]) {
         this._outstandingOffers[dst][src] = [];
         setTimeout(function() {
-          delete self._outstandingOffers[dst][src]
-        }, 5000);
+          if(!!self._outstandingOffers[dst][src]) {
+            delete self._outstandingOffers[dst][src];
+            this._handleTransmission({
+              type: 'EXPIRE',
+              src: dst,
+              dst: src
+            });   
+          }
+        }, this._options.timeout);
+      }
       this._outstandingOffers[dst][src].push(Array.prototype.slice.apply(arguments));
+      res.send(200);
     } else if (type === 'LEAVE' && !dst) {
       this._removePeer(src);
+      res.send(200);
     } else {
       // Assume a disconnect if the client no longer exists.
       this._handleTransmission({