time to test xhr streaming

This commit is contained in:
Michelle Bu 2013-01-31 10:21:08 -08:00
parent db3982b52d
commit d22646805c

View File

@ -44,36 +44,32 @@ 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) {
try {
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);
self._handleTransmission(message.type, message.src, message.dst, data);
// Clean up.
if (message.type === 'LEAVE') {
@ -84,6 +80,9 @@ PeerServer.prototype._initializeWSS = function() {
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);
// 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();
}
} else {
// Place in queue for 10 seconds.
}
}
};
exports.PeerServer = PeerServer;