diff --git a/lib/server.js b/lib/server.js index 5e4989e..479994c 100644 --- a/lib/server.js +++ b/lib/server.js @@ -25,8 +25,7 @@ function PeerServer(options) { // WebSockets that are opened. this._clients = {}; - // Requests that are awaiting response. - this._requests = {}; + this._timeouts = {}; // Initailize WebSocket server handlers. this._initializeWSS(); @@ -45,10 +44,14 @@ 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]) { + if (!!id && (!self._clients[id] || !(self._clients[id] instanceof WebSocket))) { + if (!(self._clients[id] instanceof WebSocket)) { + clearTimeout(self._timeouts[id]); + self._clients[id].end('socket'); + } self._clients[id] = socket; } else if (!id) { - // TODO: error + // TODO: assign id. }; socket.on('message', function(data) { @@ -89,29 +92,33 @@ PeerServer.prototype._initializeWSS = function() { PeerServer.prototype._initializeHTTP = function() { var self = this; + // Server sets up HTTP streaming whether you get or post an ID. // Retrieve guaranteed random ID. this._app.get('/id', function(req, res) { var clientId = util.randomId(); while (!!self._clients[clientId] || !!self._requests[clientId]) { clientId = util.randomId(); } - res.send(clientId); + + self._startStreaming(res, clientId, function() { + res.write(clientId); + }); }); this._app.post('/id', function(req, res) { var id = req.body.id; // Checked in with ID, now waiting for an offer. - self._requests[id, 'offer'] = res; + self._startStreaming(res, id); }); this._app.post('/offer', function(req, res) { + // TODO: if offer person does not exist, set a timeout for 10s. may need to + // change switch. 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._requests[src, dst] = res; - self._ice[src, dst] = []; }); this._app.post('/answer', function(req, res) { @@ -120,11 +127,24 @@ PeerServer.prototype._initializeHTTP = function() { var dst = data.dst; self._handleTransmission(src, dst, JSON.stringify(data)); // Now expecting ice from same dst. - self._requests[src, dst] = res; - self._ice[src, dst] = []; }); }; +/** Saves a streaming response and takes care of timeouts and headers. */ +PeerServer.prototype._startStreaming(res, id, write) { + res.writeHead(200, {'Content-Type': 'text/plain'}); + if (!!write) { + write(); + } + + // Save res so we can write to it. + this._clients[id] = res; + + // Set timeout to expire. + this._timeouts[id] = setTimeout(function() { res.end('end') }, 10000); +}; + +// TODO: fix for streaming /** Handles passing on a message. */ PeerServer.prototype._handleTransmission = function(src, dst, data, ice) { var destination = this._clients[dst]; @@ -155,8 +175,4 @@ PeerServer.prototype._handleTransmission = function(src, dst, data, ice) { } } - - exports.PeerServer = PeerServer; - -var ps = new PeerServer({ debug: true });