socket close handler, message unification
This commit is contained in:
parent
d998280445
commit
f60bfa8a96
112
lib/server.js
112
lib/server.js
@ -63,12 +63,12 @@ PeerServer.prototype._initializeWSS = function() {
|
|||||||
if (!!self._timeouts[id]) {
|
if (!!self._timeouts[id]) {
|
||||||
clearTimeout(self._timeouts[id]);
|
clearTimeout(self._timeouts[id]);
|
||||||
delete self._timeouts[id];
|
delete self._timeouts[id];
|
||||||
self._clients[id].end('socket');
|
self._clients[id].end(JSON.stringify({ type: 'HTTP-SOCKET' }));
|
||||||
} else {
|
} else {
|
||||||
socket.send(JSON.stringify({ type: 'ERROR', msg: 'ID is taken' }));
|
socket.send(JSON.stringify({ type: 'ERROR', msg: 'ID is taken' }));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
} else if (!id) {
|
} else if (id === undefined) {
|
||||||
id = self._generateClientId();
|
id = self._generateClientId();
|
||||||
socket.send(JSON.stringify({ type: 'ID', id: id }));
|
socket.send(JSON.stringify({ type: 'ID', id: id }));
|
||||||
}
|
}
|
||||||
@ -78,6 +78,12 @@ PeerServer.prototype._initializeWSS = function() {
|
|||||||
|
|
||||||
self._processOutstandingOffers(id);
|
self._processOutstandingOffers(id);
|
||||||
|
|
||||||
|
// Cleanup after a socket closes.
|
||||||
|
socket.on('close', function() {
|
||||||
|
util.log('Socket closed:', id);
|
||||||
|
self._removePeer(id);
|
||||||
|
});
|
||||||
|
// Handle messages from peers.
|
||||||
socket.on('message', function(data) {
|
socket.on('message', function(data) {
|
||||||
try {
|
try {
|
||||||
var message = JSON.parse(data);
|
var message = JSON.parse(data);
|
||||||
@ -87,8 +93,7 @@ PeerServer.prototype._initializeWSS = function() {
|
|||||||
case 'LEAVE':
|
case 'LEAVE':
|
||||||
// Clean up if a Peer sends a LEAVE.
|
// Clean up if a Peer sends a LEAVE.
|
||||||
if (!message.dst) {
|
if (!message.dst) {
|
||||||
delete self._clients[message.src];
|
self._removePeer(message.src);
|
||||||
delete self._timeouts[message.src];
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
// ICE candidates
|
// ICE candidates
|
||||||
@ -98,7 +103,7 @@ PeerServer.prototype._initializeWSS = function() {
|
|||||||
case 'ANSWER':
|
case 'ANSWER':
|
||||||
// Firefoxism (connectDataConnection ports)
|
// Firefoxism (connectDataConnection ports)
|
||||||
case 'PORT':
|
case 'PORT':
|
||||||
self._handleTransmission(message.type, message.src, message.dst, data);
|
self._handleTransmission(message);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
util.prettyError('message unrecognized');
|
util.prettyError('message unrecognized');
|
||||||
@ -113,7 +118,6 @@ PeerServer.prototype._initializeWSS = function() {
|
|||||||
|
|
||||||
/** Process outstanding peer offers. */
|
/** Process outstanding peer offers. */
|
||||||
PeerServer.prototype._processOutstandingOffers = function(id) {
|
PeerServer.prototype._processOutstandingOffers = function(id) {
|
||||||
console.log('processing outstanding offers');
|
|
||||||
var offers = this._outstandingOffers[id];
|
var offers = this._outstandingOffers[id];
|
||||||
if (offers === undefined)
|
if (offers === undefined)
|
||||||
return;
|
return;
|
||||||
@ -125,7 +129,6 @@ PeerServer.prototype._processOutstandingOffers = function(id) {
|
|||||||
|
|
||||||
delete this._outstandingOffers[id][sources[i]];
|
delete this._outstandingOffers[id][sources[i]];
|
||||||
}
|
}
|
||||||
console.log(this._outstandingOffers[id]);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/** Initialize HTTP server routes. */
|
/** Initialize HTTP server routes. */
|
||||||
@ -155,36 +158,34 @@ PeerServer.prototype._initializeHTTP = function() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
this._app.post('/offer', function(req, res) {
|
this._app.post('/offer', function(req, res) {
|
||||||
var src = req.body.src;
|
self._handleTransmission(req.body, res);
|
||||||
var dst = req.body.dst;
|
|
||||||
self._handleTransmission('OFFER', src, dst, JSON.stringify(req.body), res);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
this._app.post('/ice', function(req, res) {
|
this._app.post('/ice', function(req, res) {
|
||||||
var src = req.body.src;
|
self._handleTransmission(req.body, res);
|
||||||
var dst = req.body.dst;
|
|
||||||
self._handleTransmission('CANDIDATE', src, dst, JSON.stringify(req.body), res);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
this._app.post('/answer', function(req, res) {
|
this._app.post('/answer', function(req, res) {
|
||||||
var src = req.body.src;
|
self._handleTransmission(req.body, res);
|
||||||
var dst = req.body.dst;
|
|
||||||
self._handleTransmission('ANSWER', src, dst, JSON.stringify(req.body), res);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
this._app.post('/leave', function(req, res) {
|
this._app.post('/leave', function(req, res) {
|
||||||
var src = req.body.src;
|
self._handleTransmission(req.body, res);
|
||||||
var dst = req.body.dst;
|
|
||||||
self._handleTransmission('LEAVE', src, dst, JSON.stringify(req.body), res);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
this._app.post('/port', function(req, res) {
|
this._app.post('/port', function(req, res) {
|
||||||
var src = req.body.src;
|
self._handleTransmission(req.body, res);
|
||||||
var dst = req.body.dst;
|
|
||||||
self._handleTransmission('PORT', src, dst, JSON.stringify(req.body), res);
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
PeerServer.prototype._removePeer = function(id) {
|
||||||
|
delete this._clients[id];
|
||||||
|
if (this._timeouts[id]) {
|
||||||
|
clearTimeout(this._timeouts[id]);
|
||||||
|
delete this._timeouts[id];
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
/** Saves a streaming response and takes care of timeouts and headers. */
|
/** Saves a streaming response and takes care of timeouts and headers. */
|
||||||
PeerServer.prototype._startStreaming = function(res, id, write) {
|
PeerServer.prototype._startStreaming = function(res, id, write) {
|
||||||
res.writeHead(200, {'Content-Type': 'application/octet-stream'});
|
res.writeHead(200, {'Content-Type': 'application/octet-stream'});
|
||||||
@ -203,59 +204,75 @@ PeerServer.prototype._startStreaming = function(res, id, write) {
|
|||||||
if (!this._clients[id]) {
|
if (!this._clients[id]) {
|
||||||
this._clients[id] = res;
|
this._clients[id] = res;
|
||||||
// Set timeout to expire.
|
// Set timeout to expire.
|
||||||
this._timeouts[id] = setTimeout(function() { res.end('end') }, 10000);
|
var self = this;
|
||||||
|
this._timeouts[id] = setTimeout(function() {
|
||||||
|
self._removePeer(id);
|
||||||
|
res.end(JSON.stringify({ type: 'HTTP-END' }));
|
||||||
|
}, 30000);
|
||||||
|
this._processOutstandingOffers(id);
|
||||||
} else {
|
} else {
|
||||||
res.write(JSON.stringify({ type: 'ERROR', msg: 'ID is taken' }) + '\n');
|
res.write(JSON.stringify({ type: 'ERROR', msg: 'ID is taken' }) + '\n');
|
||||||
res.end('error');
|
res.end(JSON.stringify({ type: 'HTTP-ERROR' }));
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/** Handles passing on a message. */
|
/** Handles passing on a message. */
|
||||||
PeerServer.prototype._handleTransmission = function(type, src, dst, data, res) {
|
PeerServer.prototype._handleTransmission = function(message, res) {
|
||||||
|
var type = message.type;
|
||||||
|
var src = message.src;
|
||||||
|
var dst = message.dst;
|
||||||
|
var data = JSON.stringify(message);
|
||||||
|
|
||||||
var destination = this._clients[dst];
|
var destination = this._clients[dst];
|
||||||
|
|
||||||
if (!!destination) {
|
if (!!destination) {
|
||||||
try {
|
try {
|
||||||
if (this._timeouts[dst]) {
|
if (this._timeouts[dst]) {
|
||||||
data += '\n';
|
data += '\n';
|
||||||
|
destination.write(data);
|
||||||
|
} else {
|
||||||
|
destination.send(data);
|
||||||
}
|
}
|
||||||
// We have to let the source peer know that the offer was sent
|
// We have to let the source peer know that the offer was sent
|
||||||
// successfully so that ice can start being processed.
|
// successfully so that ice can start being processed.
|
||||||
if (type === 'OFFER') {
|
if (!!res) {
|
||||||
if (!!res) {
|
res.send(200);
|
||||||
res.send(200);
|
|
||||||
} else if (!this._timeouts[src] && !!this._clients[src]) {
|
|
||||||
this._clients[src].send(JSON.stringify({ type: 'PEER_READY', src: dst, dst: src }));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
destination.send(data);
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
// This happens when a peer disconnects without closing connections and
|
||||||
|
// the associated WebSocket has not closed.
|
||||||
util.prettyError(e);
|
util.prettyError(e);
|
||||||
// This really shouldn't happen given correct client browsers.
|
// Tell other side to stop trying.
|
||||||
// 501: Server does not support this functionality.
|
this._removePeer(dst);
|
||||||
|
this._handleTransmission({
|
||||||
|
type: 'LEAVE',
|
||||||
|
src: dst,
|
||||||
|
dst: src
|
||||||
|
});
|
||||||
if (!!res) res.send(501);
|
if (!!res) res.send(501);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (type === 'OFFER' && (!this._outstandingOffers[dst] || !this._outstandingOffers[dst][src])) {
|
if (type !== 'LEAVE') {
|
||||||
// Wait 5 seconds for this client to connect.
|
// Wait 5 seconds for this client to connect.
|
||||||
var self = this;
|
var self = this;
|
||||||
if (!this._outstandingOffers[dst])
|
if (!this._outstandingOffers[dst])
|
||||||
this._outstandingOffers[dst] = {};
|
this._outstandingOffers[dst] = {};
|
||||||
this._outstandingOffers[dst][src] = [];
|
if (!this._outstandingOffers[dst][src])
|
||||||
|
this._outstandingOffers[dst][src] = [];
|
||||||
|
setTimeout(function() {
|
||||||
|
delete self._outstandingOffers[dst][src]
|
||||||
|
}, 30000);
|
||||||
this._outstandingOffers[dst][src].push(Array.prototype.slice.apply(arguments));
|
this._outstandingOffers[dst][src].push(Array.prototype.slice.apply(arguments));
|
||||||
|
} else if (type === 'LEAVE' && !dst) {
|
||||||
console.log('offer on queue');
|
this._removePeer(src);
|
||||||
setTimeout(function() {
|
|
||||||
delete self._outstandingOffers[dst][src]
|
|
||||||
}, 30000);
|
|
||||||
} else if (type === 'CANDIDATE' && !!this._outstandingOffers[dst][src]) {
|
|
||||||
this._outstandingOffers[dst][src].push(Array.prototype.slice.apply(arguments));
|
|
||||||
console.log('ice on queue');
|
|
||||||
} else {
|
} else {
|
||||||
// Assume a disconnect if the client no longer exists.
|
// Assume a disconnect if the client no longer exists.
|
||||||
util.log('destination does not exist');
|
this._handleTransmission({
|
||||||
this._handleTransmission('LEAVE', dst, src, JSON.stringify({ type: 'LEAVE', dst: src, src: dst }));
|
type: 'LEAVE',
|
||||||
|
src: dst,
|
||||||
|
dst: src
|
||||||
|
});
|
||||||
// 410: Resource not available.
|
// 410: Resource not available.
|
||||||
if (!!res) res.send(410);
|
if (!!res) res.send(410);
|
||||||
}
|
}
|
||||||
@ -264,9 +281,10 @@ PeerServer.prototype._handleTransmission = function(type, src, dst, data, res) {
|
|||||||
|
|
||||||
PeerServer.prototype._generateClientId = function() {
|
PeerServer.prototype._generateClientId = function() {
|
||||||
var clientId = util.randomId();
|
var clientId = util.randomId();
|
||||||
while (!!self._clients[clientId]) {
|
while (!!this._clients[clientId]) {
|
||||||
clientId = util.randomId();
|
clientId = util.randomId();
|
||||||
}
|
}
|
||||||
|
return clientId;
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.PeerServer = PeerServer;
|
exports.PeerServer = PeerServer;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user