diff --git a/lib/server.js b/lib/server.js index 479994c..cb8a3d5 100644 --- a/lib/server.js +++ b/lib/server.js @@ -44,45 +44,44 @@ PeerServer.prototype._initializeWSS = function() { var id = url.parse(socket.upgradeReq.url, true).query.id; // Save the socket for this id. - if (!!id && (!self._clients[id] || !(self._clients[id] instanceof WebSocket))) { - if (!(self._clients[id] instanceof WebSocket)) { + if (!!id && (!self._clients[id] || !!self._timeouts[id])) { + if (!!self._timeouts[id]) { clearTimeout(self._timeouts[id]); + delete self._timeouts[id]; self._clients[id].end('socket'); } - self._clients[id] = socket; } else if (!id) { - // TODO: assign id. - }; + id = self._generateClientId(); + socket.send(JSON.stringify({ type: 'ID', id: id })); + } + self._clients[id] = socket; socket.on('message', function(data) { - var message = JSON.parse(data); - var ice = false; - util.log(message); + try { + var message = JSON.parse(data); + util.log(message); - // Save the socket. - if (!self._clients[message.src]) { - self._clients[message.src] = socket; - } + switch (message.type) { + // ICE candidates + case 'CANDIDATE': + // Offer or answer between peers. + case 'OFFER': + case 'ANSWER': + // Firefoxism (connectDataConnection ports) + case 'PORT': + self._handleTransmission(message.type, message.src, message.dst, data); - switch (message.type) { - // ICE candidates - case 'CANDIDATE': - ice = true; - // Offer or answer between peers. - case 'OFFER': - case 'ANSWER': - // Firefoxism (connectDataConnection ports) - case 'PORT': - self._handleTransmission(message.src, message.dst, data, ice); - - // Clean up. - if (message.type === 'LEAVE') { - delete self._clients[message.src]; - delete self._requests[message.src]; - } - break; - default: - util.prettyError('message unrecognized'); + // Clean up. + if (message.type === 'LEAVE') { + delete self._clients[message.src]; + delete self._requests[message.src]; + } + break; + default: + util.prettyError('message unrecognized'); + } + } catch(e) { + util.log('invalid message'); } }); }); @@ -99,7 +98,6 @@ PeerServer.prototype._initializeHTTP = function() { while (!!self._clients[clientId] || !!self._requests[clientId]) { clientId = util.randomId(); } - self._startStreaming(res, clientId, function() { res.write(clientId); }); @@ -107,7 +105,6 @@ PeerServer.prototype._initializeHTTP = function() { this._app.post('/id', function(req, res) { var id = req.body.id; - // Checked in with ID, now waiting for an offer. self._startStreaming(res, id); }); @@ -117,21 +114,19 @@ PeerServer.prototype._initializeHTTP = function() { var data = req.body.data; var src = data.src; var dst = data.dst; - self._handleTransmission(src, dst, JSON.stringify(data)); - // Now expecting ice from same dst. + self._handleTransmission('OFFER', src, dst, JSON.stringify(data)); }); this._app.post('/answer', function(req, res) { var data = req.body.data; var src = data.src; var dst = data.dst; - self._handleTransmission(src, dst, JSON.stringify(data)); - // Now expecting ice from same dst. + self._handleTransmission('ANSWER', src, dst, JSON.stringify(data)); }); }; /** Saves a streaming response and takes care of timeouts and headers. */ -PeerServer.prototype._startStreaming(res, id, write) { +PeerServer.prototype._startStreaming = function(res, id, write) { res.writeHead(200, {'Content-Type': 'text/plain'}); if (!!write) { write(); @@ -146,33 +141,26 @@ PeerServer.prototype._startStreaming(res, id, write) { // TODO: fix for streaming /** Handles passing on a message. */ -PeerServer.prototype._handleTransmission = function(src, dst, data, ice) { +PeerServer.prototype._handleTransmission = function(type, src, dst, data) { var destination = this._clients[dst]; - if (!destination) { - // For ICE, ports, and answers this should be here. - destination = this._requests[dst, src]; - if (!destination) { - // Otherwise it's a new offer. - destination = this._requests[dst, 'offer']; - } - } if (!!destination) { - if (!ice) { - try { - destination.send(data); - } catch (e) { - util.prettyError(e); - } - } else { - if (!!this._ice[dst, src]) { - // TODO: see if we can save less. - this._ice[dst, src].push(data); - } + try { + destination.send(data); + } catch (e) { + util.prettyError(e); } } else { - // Place in queue for 10 seconds. + // TODO: IF OFFER: Place in queue for 10 seconds. + util.log('TODO/handle: destination does not exist'); } -} +}; + +PeerServer.prototype._generateClientId = function() { + var clientId = util.randomId(); + while (!!self._clients[clientId] || !!self._requests[clientId]) { + clientId = util.randomId(); + } +}; exports.PeerServer = PeerServer;