remove whitespace

This commit is contained in:
Michelle Bu 2013-06-15 18:31:44 -07:00
parent eb0ddfcc6b
commit c0130c418b

View File

@ -25,7 +25,7 @@ function PeerServer(options) {
// Connected clients // Connected clients
this._clients = {}; this._clients = {};
// Messages waiting for another peer. // Messages waiting for another peer.
this._outstanding = {}; this._outstanding = {};
@ -35,12 +35,11 @@ function PeerServer(options) {
// Initialize HTTP routes. This is only used for the first few milliseconds // Initialize HTTP routes. This is only used for the first few milliseconds
// before a socket is opened for a Peer. // before a socket is opened for a Peer.
this._initializeHTTP(); this._initializeHTTP();
// Mark concurrent users per ip // Mark concurrent users per ip
this._ips = {}; this._ips = {};
this._setCleanupIntervals(); this._setCleanupIntervals();
}; };
util.inherits(PeerServer, EventEmitter); util.inherits(PeerServer, EventEmitter);
@ -49,11 +48,10 @@ util.inherits(PeerServer, EventEmitter);
/** Initialize WebSocket server. */ /** Initialize WebSocket server. */
PeerServer.prototype._initializeWSS = function() { PeerServer.prototype._initializeWSS = function() {
var self = this; var self = this;
// Create WebSocket server as well. // Create WebSocket server as well.
this._wss = new WebSocketServer({ path: '/peerjs', server: this._httpServer }); this._wss = new WebSocketServer({ path: '/peerjs', server: this._httpServer });
this._wss.on('connection', function(socket) { this._wss.on('connection', function(socket) {
var query = url.parse(socket.upgradeReq.url, true).query; var query = url.parse(socket.upgradeReq.url, true).query;
var id = query.id; var id = query.id;
@ -88,10 +86,8 @@ PeerServer.prototype._initializeWSS = function() {
PeerServer.prototype._configureWS = function(socket, key, id, token) { PeerServer.prototype._configureWS = function(socket, key, id, token) {
var self = this; var self = this;
var client = this._clients[key][id]; var client = this._clients[key][id];
if (token === client.token) { if (token === client.token) {
// res 'close' event will delete client.res for us // res 'close' event will delete client.res for us
client.socket = socket; client.socket = socket;
@ -115,7 +111,7 @@ PeerServer.prototype._configureWS = function(socket, key, id, token) {
self._removePeer(key, id); self._removePeer(key, id);
} }
}); });
// Handle messages from peers. // Handle messages from peers.
socket.on('message', function(data) { socket.on('message', function(data) {
try { try {
@ -181,7 +177,7 @@ PeerServer.prototype._checkKey = function(key, ip, cb) {
/** Initialize HTTP server routes. */ /** Initialize HTTP server routes. */
PeerServer.prototype._initializeHTTP = function() { PeerServer.prototype._initializeHTTP = function() {
var self = this; var self = this;
this._app.use(restify.bodyParser({ mapParams: false })); this._app.use(restify.bodyParser({ mapParams: false }));
this._app.use(restify.queryParser()) this._app.use(restify.queryParser())
this._app.use(util.allowCrossDomain); this._app.use(util.allowCrossDomain);
@ -199,7 +195,7 @@ PeerServer.prototype._initializeHTTP = function() {
var token = req.params.token; var token = req.params.token;
var key = req.params.key; var key = req.params.key;
var ip = req.ip; var ip = req.ip;
if (!self._clients[key] || !self._clients[key][id]) { if (!self._clients[key] || !self._clients[key][id]) {
self._checkKey(key, ip, function(err) { self._checkKey(key, ip, function(err) {
if (!err && !self._clients[key][id]) { if (!err && !self._clients[key][id]) {
@ -216,11 +212,10 @@ PeerServer.prototype._initializeHTTP = function() {
return next(); return next();
}); });
var handle = function(req, res, next) { var handle = function(req, res, next) {
var key = req.params.key; var key = req.params.key;
var id = req.params.id; var id = req.params.id;
var client; var client;
if (!self._clients[key] || !(client = self._clients[key][id])) { if (!self._clients[key] || !(client = self._clients[key][id])) {
if (req.params.retry) { if (req.params.retry) {
@ -232,7 +227,7 @@ PeerServer.prototype._initializeHTTP = function() {
} }
return; return;
} }
// Auth the req // Auth the req
if (req.params.token !== client.token) { if (req.params.token !== client.token) {
res.send(401); res.send(401);
@ -248,7 +243,7 @@ PeerServer.prototype._initializeHTTP = function() {
} }
return next(); return next();
}; };
this._app.post('/:key/:id/:token/offer', handle); this._app.post('/:key/:id/:token/offer', handle);
this._app.post('/:key/:id/:token/candidate', handle); this._app.post('/:key/:id/:token/candidate', handle);
@ -259,13 +254,12 @@ PeerServer.prototype._initializeHTTP = function() {
// Listen on user-specified port. // Listen on user-specified port.
this._httpServer.listen(this._options.port); this._httpServer.listen(this._options.port);
}; };
/** 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, key, id, token, open) { PeerServer.prototype._startStreaming = function(res, key, id, token, open) {
var self = this; var self = this;
res.writeHead(200, {'Content-Type': 'application/octet-stream'}); res.writeHead(200, {'Content-Type': 'application/octet-stream'});
var pad = '00'; var pad = '00';
@ -273,13 +267,13 @@ PeerServer.prototype._startStreaming = function(res, key, id, token, open) {
pad += pad; pad += pad;
} }
res.write(pad + '\n'); res.write(pad + '\n');
if (open) { if (open) {
res.write(JSON.stringify({ type: 'OPEN' }) + '\n'); res.write(JSON.stringify({ type: 'OPEN' }) + '\n');
} }
var client = this._clients[key][id]; var client = this._clients[key][id];
if (token === client.token) { if (token === client.token) {
// Client already exists // Client already exists
res.on('close', function() { res.on('close', function() {
@ -323,7 +317,7 @@ PeerServer.prototype._pruneOutstanding = function() {
/** Cleanup */ /** Cleanup */
PeerServer.prototype._setCleanupIntervals = function() { PeerServer.prototype._setCleanupIntervals = function() {
var self = this; var self = this;
// Clean up ips every 10 minutes // Clean up ips every 10 minutes
setInterval(function() { setInterval(function() {
var keys = Object.keys(self._ips); var keys = Object.keys(self._ips);
@ -334,7 +328,7 @@ PeerServer.prototype._setCleanupIntervals = function() {
} }
} }
}, 600000); }, 600000);
// Clean up outstanding messages every 5 seconds // Clean up outstanding messages every 5 seconds
setInterval(function() { setInterval(function() {
self._pruneOutstanding(); self._pruneOutstanding();