xhr streaming works

This commit is contained in:
Michelle Bu 2013-01-31 13:34:01 -08:00
parent f66efcff91
commit 9a21d8ec7d

View File

@ -5,12 +5,15 @@ var EventEmitter = require('events').EventEmitter;
var WebSocketServer = require('ws').Server; var WebSocketServer = require('ws').Server;
var url = require('url'); var url = require('url');
var allowCrossDomain = function(req, res, next) { var allowCrossDomain = function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*'); res.setHeader('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE'); res.setHeader('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.header('Access-Control-Allow-Headers', 'Content-Type'); res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
next(); next();
}; }
function PeerServer(options) { function PeerServer(options) {
if (!(this instanceof PeerServer)) return new PeerServer(options); if (!(this instanceof PeerServer)) return new PeerServer(options);
@ -18,11 +21,8 @@ function PeerServer(options) {
this._app = express(); this._app = express();
this._httpServer = http.createServer(this._app); this._httpServer = http.createServer(this._app);
var self = this; this._app.use(express.bodyParser());
this._app.configure(function() { this._app.use(allowCrossDomain);
self._app.use(express.bodyParser());
self._app.use(allowCrossDomain);
});
options = util.extend({ options = util.extend({
port: 80 port: 80
@ -102,6 +102,10 @@ PeerServer.prototype._initializeWSS = function() {
PeerServer.prototype._initializeHTTP = function() { PeerServer.prototype._initializeHTTP = function() {
var self = this; var self = this;
this._app.options('/*', function(req, res, next) {
res.send(200);
});
// Server sets up HTTP streaming whether you get or post an ID. // Server sets up HTTP streaming whether you get or post an ID.
// Retrieve guaranteed random ID. // Retrieve guaranteed random ID.
this._app.get('/id', function(req, res) { this._app.get('/id', function(req, res) {
@ -123,39 +127,46 @@ PeerServer.prototype._initializeHTTP = function() {
this._app.post('/offer', function(req, res) { this._app.post('/offer', function(req, res) {
// TODO: if offer person does not exist, set a timeout for 10s. may need to // TODO: if offer person does not exist, set a timeout for 10s. may need to
// change switch. // change switch.
var data = req.body.data; var src = req.body.src;
var src = data.src; var dst = req.body.dst;
var dst = data.dst; self._handleTransmission('OFFER', src, dst, JSON.stringify(req.body));
self._handleTransmission('OFFER', src, dst, JSON.stringify(data)); res.send('success');
});
this._app.post('/ice', function(req, res) {
var src = req.body.src;
var dst = req.body.dst;
self._handleTransmission('ICE', src, dst, JSON.stringify(req.body));
res.send('success');
}); });
this._app.post('/answer', function(req, res) { this._app.post('/answer', function(req, res) {
var data = req.body.data; var src = req.body.src;
var src = data.src; var dst = req.body.dst;
var dst = data.dst; self._handleTransmission('ANSWER', src, dst, JSON.stringify(req.body));
self._handleTransmission('ANSWER', src, dst, JSON.stringify(data)); res.send('success');
}); });
}; };
/** 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 = function(res, id, write) { PeerServer.prototype._startStreaming = function(res, id, write) {
res.writeHead(200, {'Content-Type': 'application/octet-stream'}); res.writeHead(200, {'Content-Type': 'application/octet-stream'});
if (!!write) { if (!!write) {
write(); write();
} }
var pad = '00'; var pad = '00';
var iterations = 10; var iterations = 10;
for (var i = 0; i < iterations; i++) { for (var i = 0; i < iterations; i++) {
pad += pad; pad += pad;
} }
res.write(pad + '\n'); res.write(pad + '\n');
// Save res so we can write to it. // Save res so we can write to it.
this._clients[id] = res; this._clients[id] = res;
// Set timeout to expire. // Set timeout to expire.
this._timeouts[id] = setTimeout(function() { res.end('end') }, 10000); this._timeouts[id] = setTimeout(function() { res.end('end') }, 10000);
}; };
// TODO: fix for streaming // TODO: fix for streaming