|
@@ -44,45 +44,44 @@ PeerServer.prototype._initializeWSS = function() {
|
|
var id = url.parse(socket.upgradeReq.url, true).query.id;
|
|
var id = url.parse(socket.upgradeReq.url, true).query.id;
|
|
|
|
|
|
// Save the socket for this 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]);
|
|
clearTimeout(self._timeouts[id]);
|
|
|
|
+ delete self._timeouts[id];
|
|
self._clients[id].end('socket');
|
|
self._clients[id].end('socket');
|
|
}
|
|
}
|
|
- self._clients[id] = socket;
|
|
|
|
} else if (!id) {
|
|
} 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) {
|
|
socket.on('message', function(data) {
|
|
- var message = JSON.parse(data);
|
|
|
|
- var ice = false;
|
|
|
|
- util.log(message);
|
|
|
|
-
|
|
|
|
- // Save the socket.
|
|
|
|
- if (!self._clients[message.src]) {
|
|
|
|
- self._clients[message.src] = socket;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- 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');
|
|
|
|
|
|
+ try {
|
|
|
|
+ var message = JSON.parse(data);
|
|
|
|
+ util.log(message);
|
|
|
|
+
|
|
|
|
+ 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);
|
|
|
|
+
|
|
|
|
+ // 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]) {
|
|
while (!!self._clients[clientId] || !!self._requests[clientId]) {
|
|
clientId = util.randomId();
|
|
clientId = util.randomId();
|
|
}
|
|
}
|
|
-
|
|
|
|
self._startStreaming(res, clientId, function() {
|
|
self._startStreaming(res, clientId, function() {
|
|
res.write(clientId);
|
|
res.write(clientId);
|
|
});
|
|
});
|
|
@@ -107,7 +105,6 @@ PeerServer.prototype._initializeHTTP = function() {
|
|
|
|
|
|
this._app.post('/id', function(req, res) {
|
|
this._app.post('/id', function(req, res) {
|
|
var id = req.body.id;
|
|
var id = req.body.id;
|
|
- // Checked in with ID, now waiting for an offer.
|
|
|
|
self._startStreaming(res, id);
|
|
self._startStreaming(res, id);
|
|
});
|
|
});
|
|
|
|
|
|
@@ -117,21 +114,19 @@ PeerServer.prototype._initializeHTTP = function() {
|
|
var data = req.body.data;
|
|
var data = req.body.data;
|
|
var src = data.src;
|
|
var src = data.src;
|
|
var dst = data.dst;
|
|
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) {
|
|
this._app.post('/answer', function(req, res) {
|
|
var data = req.body.data;
|
|
var data = req.body.data;
|
|
var src = data.src;
|
|
var src = data.src;
|
|
var dst = data.dst;
|
|
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. */
|
|
/** 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'});
|
|
res.writeHead(200, {'Content-Type': 'text/plain'});
|
|
if (!!write) {
|
|
if (!!write) {
|
|
write();
|
|
write();
|
|
@@ -146,33 +141,26 @@ PeerServer.prototype._startStreaming(res, id, write) {
|
|
|
|
|
|
// TODO: fix for streaming
|
|
// TODO: fix for streaming
|
|
/** Handles passing on a message. */
|
|
/** 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];
|
|
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 (!!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 {
|
|
} 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;
|
|
exports.PeerServer = PeerServer;
|