expire correctly

This commit is contained in:
ericz 2013-02-06 15:26:13 -08:00
parent 1ac03a1039
commit 44ee59e18e

View File

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