ericz 12 лет назад
Родитель
Сommit
08df4056da
2 измененных файлов с 45 добавлено и 31 удалено
  1. 44 30
      lib/server.js
  2. 1 1
      lib/util.js

+ 44 - 30
lib/server.js

@@ -17,7 +17,8 @@ function PeerServer(options) {
   this._options = util.extend({
     port: 80,
     debug: false,
-    timeout: 5000
+    timeout: 5000,
+    path: 'peerjs'
   }, options);
 
   util.debug = this._options.debug;
@@ -34,6 +35,11 @@ function PeerServer(options) {
   // Initialize HTTP routes. This is only used for the first few milliseconds
   // before a socket is opened for a Peer.
   this._initializeHTTP();
+  
+  var self = this;
+  setInterval(function(){
+    self._pruneOutstanding();
+  }, 5000);
 };
 
 util.inherits(PeerServer, EventEmitter);
@@ -44,21 +50,27 @@ PeerServer.prototype._initializeWSS = function() {
   var self = this;
   
   // Create WebSocket server as well.
-  this._wss = new WebSocketServer({ path: '/ws', server: this._httpServer });
+  this._wss = new WebSocketServer({ path: '/' + this._options.path, server: this._httpServer });
 
   
   this._wss.on('connection', function(socket) {
     var query = url.parse(socket.upgradeReq.url, true).query;
-    var id = query.id || self.generateClientId();
+    var id = query.id;
     var token = query.token;
+    var key = query.key;
    
     var client = self._clients[id];
     
+    if (!id || !token || !key) {
+      socket.send(JSON.stringify({ type: 'ERROR', payload: { msg: 'No id, token, or key supplied to websocket server' } }));
+      socket.close();
+      return;
+    }
+    
     if (!client) {
-      // New client, save info
       client = { token: token };
       self._clients[id] = client;
-      socket.send(JSON.stringify({ type: 'OPEN', id: id }));
+      socket.send(JSON.stringify({ type: 'OPEN' }));
     } 
     
     if (token === client.token) {
@@ -110,13 +122,14 @@ PeerServer.prototype._initializeWSS = function() {
               type: message.type,
               src: id,
               dst: message.dst,
-              payload: msg.payload
+              payload: message.payload
             });
             break;
           default:
             util.prettyError('Message unrecognized');
         }
       } catch(e) {
+        throw e;
         util.log('Invalid message', data);
       }
     });
@@ -135,29 +148,28 @@ PeerServer.prototype._initializeHTTP = function() {
     res.send(200);
   });
 
-  // Server sets up HTTP streaming whether you get or post an ID.
+  
   // Retrieve guaranteed random ID.
-  this._app.get('/id', function(req, res) {
-    var id = self.generateClientId();
-    var token = req.query.token;
-    self._startStreaming(res, id, token);
+  this._app.get('/:key/id', function(req, res) {
+    res.send(self._generateClientId());
   });
 
-  this._app.post('/id', function(req, res) {
-    self._startStreaming(res, req.body.id, req.body.token);
+  // Server sets up HTTP streaming when you get post an ID.
+  this._app.post('/:key/:id/:token/id', function(req, res) {
+    self._startStreaming(res, req.params.id, req.params.token);
   });
 
     
   var handle = function(req, res){
-    var client = self._clients[req.body.id];
+    var client = self._clients[req.params.id];
     // Auth the req
-    if (!client || req.body.token !== client.token) {
+    if (!client || req.params.token !== client.token) {
       res.send(401);
       return;
     } else {
       self._handleTransmission({
         type: req.body.type,
-        src: client.id,
+        src: req.params.id,
         dst: req.body.dst,
         payload: req.body.payload
       });
@@ -165,15 +177,15 @@ PeerServer.prototype._initializeHTTP = function() {
     }
   };
   
-  this._app.post('/offer', handle);
+  this._app.post('/:key/:id/:token/offer', handle);
 
-  this._app.post('/candidate', handle);
+  this._app.post('/:key/:id/:token/candidate', handle);
 
-  this._app.post('/answer', handle);
+  this._app.post('/:key/:id/:token/answer', handle);
 
-  this._app.post('/leave', handle);
+  this._app.post('/:key/:id/:token/leave', handle);
 
-  this._app.post('/port', handle);
+  //this._app.post('/port', handle);
   
   // Listen on user-specified port and 
   this._httpServer.listen(this._options.port);
@@ -182,8 +194,16 @@ PeerServer.prototype._initializeHTTP = function() {
 
 /** Saves a streaming response and takes care of timeouts and headers. */
 PeerServer.prototype._startStreaming = function(res, id, token) {
+  var self = this;
+  
   res.writeHead(200, {'Content-Type': 'application/octet-stream'});
 
+  var pad = '00';
+  for (var i = 0; i < 10; i++) {
+    pad += pad;
+  }
+  res.write(pad + '\n');
+  
   var client = this._clients[id];
   
   // Save res so we can write to it.
@@ -191,22 +211,16 @@ PeerServer.prototype._startStreaming = function(res, id, token) {
     // New client, save info
     client = { token: token };
     this._clients[id] = client;
-    res.write(JSON.stringify({ type: 'OPEN', id: id }) + '\n');
+    res.write(JSON.stringify({ type: 'OPEN' }) + '\n');
   }
-    
-  var pad = '00';
-  for (var i = 0; i < 10; i++) {
-    pad += pad;
-  }
-  res.write(pad + '\n');
-
+  
   if (token === client.token) {
     // Client already exists
     res.on('close', function(){
       if (client.res === res) {
         if (!client.socket) {
           // No new request yet, peer dead
-          self._remotePeer(id);
+          self._removePeer(id);
           return;
         }
         delete client.res;

+ 1 - 1
lib/util.js

@@ -37,7 +37,7 @@ var util = {
       console.log.apply(console, copy);
     }
   },
-  _allowCrossDomain: function(req, res, next) {
+  allowCrossDomain: function(req, res, next) {
     res.setHeader('Access-Control-Allow-Origin', '*');
     res.setHeader('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
     res.setHeader('Access-Control-Allow-Headers', 'Content-Type');