time to test xhr streaming
This commit is contained in:
parent
db3982b52d
commit
d22646805c
@ -44,36 +44,32 @@ 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 (!!id && (!self._clients[id] || !!self._timeouts[id])) {
|
||||||
if (!(self._clients[id] instanceof WebSocket)) {
|
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) {
|
||||||
|
try {
|
||||||
var message = JSON.parse(data);
|
var message = JSON.parse(data);
|
||||||
var ice = false;
|
|
||||||
util.log(message);
|
util.log(message);
|
||||||
|
|
||||||
// Save the socket.
|
|
||||||
if (!self._clients[message.src]) {
|
|
||||||
self._clients[message.src] = socket;
|
|
||||||
}
|
|
||||||
|
|
||||||
switch (message.type) {
|
switch (message.type) {
|
||||||
// ICE candidates
|
// ICE candidates
|
||||||
case 'CANDIDATE':
|
case 'CANDIDATE':
|
||||||
ice = true;
|
|
||||||
// Offer or answer between peers.
|
// Offer or answer between peers.
|
||||||
case 'OFFER':
|
case 'OFFER':
|
||||||
case 'ANSWER':
|
case 'ANSWER':
|
||||||
// Firefoxism (connectDataConnection ports)
|
// Firefoxism (connectDataConnection ports)
|
||||||
case 'PORT':
|
case 'PORT':
|
||||||
self._handleTransmission(message.src, message.dst, data, ice);
|
self._handleTransmission(message.type, message.src, message.dst, data);
|
||||||
|
|
||||||
// Clean up.
|
// Clean up.
|
||||||
if (message.type === 'LEAVE') {
|
if (message.type === 'LEAVE') {
|
||||||
@ -84,6 +80,9 @@ PeerServer.prototype._initializeWSS = function() {
|
|||||||
default:
|
default:
|
||||||
util.prettyError('message unrecognized');
|
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));
|
self._handleTransmission('OFFER', src, dst, JSON.stringify(data));
|
||||||
// Now expecting ice from same dst.
|
|
||||||
});
|
});
|
||||||
|
|
||||||
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));
|
self._handleTransmission('ANSWER', src, dst, JSON.stringify(data));
|
||||||
// Now expecting ice from same dst.
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
/** 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 {
|
try {
|
||||||
destination.send(data);
|
destination.send(data);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
util.prettyError(e);
|
util.prettyError(e);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (!!this._ice[dst, src]) {
|
// TODO: IF OFFER: Place in queue for 10 seconds.
|
||||||
// TODO: see if we can save less.
|
util.log('TODO/handle: destination does not exist');
|
||||||
this._ice[dst, src].push(data);
|
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
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;
|
exports.PeerServer = PeerServer;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user