Browse Source

Compatibility mode

Michelle Bu 11 năm trước cách đây
mục cha
commit
fde9a3e1eb
1 tập tin đã thay đổi với 39 bổ sung1 xóa
  1. 39 1
      lib/server.js

+ 39 - 1
lib/server.js

@@ -5,6 +5,8 @@ var EventEmitter = require('events').EventEmitter;
 var WebSocketServer = require('ws').Server;
 var url = require('url');
 
+var MAXLENGTH = 150;
+
 function PeerServer(options) {
   if (!(this instanceof PeerServer)) return new PeerServer(options);
   EventEmitter.call(this);
@@ -32,6 +34,7 @@ function PeerServer(options) {
 
   // Connected clients
   this._clients = {};
+  this._compatibility = {};
 
   // Messages waiting for another peer.
   this._outstanding = {};
@@ -211,7 +214,8 @@ PeerServer.prototype._initializeHTTP = function() {
     var id = req.params.id;
 
     var client;
-    if (!self._clients[key] || !(client = self._clients[key][id])) {
+    if (!self._clients[key] || !self._clients[key][id]) {
+      client = self._clients[key][id];
       if (req.params.retry) {
         res.send(401);
       } else {
@@ -246,6 +250,37 @@ PeerServer.prototype._initializeHTTP = function() {
 
   this._app.post('/:key/:id/:token/leave', handle);
 
+
+  // Legacy mode!
+  this._app.get('/:key/compatibility/:peer', function(req, res, next) {
+    var peer = req.params.peer;
+    var key = req.params.key;
+
+      console.log(self._compatibility)
+    if (self._compatibility[key] && self._compatibility[key][peer]) {
+      res.send(self._compatibility[key][peer]);
+    } else {
+      res.send(404);
+    }
+    return next();
+  });
+
+  this._app.post('/:key/compatibility/:id', function(req, res, next) {
+    var id = req.params.id;
+    var key = req.params.key;
+
+    if (self._clients[key] && self._clients[key][id]) {
+      if (req.body && req.body.length && req.body.length <= MAXLENGTH) {
+        self._compatibility[key] = self._compatibility[key] || {};
+        self._compatibility[key][id] = req.body;
+        res.send(200);
+        return next();
+      }
+    }
+    res.send(401);
+    return next();
+  });
+
   // Listen on user-specified port.
   this._app.listen(this._options.port);
 };
@@ -345,6 +380,9 @@ PeerServer.prototype._removePeer = function(key, id) {
   if (this._clients[key] && this._clients[key][id]) {
     this._ips[this._clients[key][id].ip]--;
     delete this._clients[key][id];
+    if (this._compatibility[key] && this._compatibility[key][id]) {
+      delete this._compatibility[key][id];
+    }
   }
 };