Răsfoiți Sursa

Add a "path" option for a namespaced peer server. Addresses #78

Michelle Bu 11 ani în urmă
părinte
comite
dd046c2d65
2 a modificat fișierele cu 12 adăugiri și 6 ștergeri
  1. 6 2
      lib/peer.js
  2. 6 4
      lib/socket.js

+ 6 - 2
lib/peer.js

@@ -77,7 +77,7 @@ function Peer(id, options) {
   // Initialize the 'socket' (which is actually a mix of XHR streaming and
   // websockets.)
   var self = this;
-  this.socket = new Socket(this.options.secure, this.options.host, this.options.port, this.options.key);
+  this.socket = new Socket(this.options.secure, this.options.host, this.options.port, this.options.path, this.options.key);
   this.socket.on('message', function(data) {
     self._handleMessage(data);
   });
@@ -107,7 +107,7 @@ Peer.prototype._retrieveId = function(cb) {
   var self = this;
   var http = new XMLHttpRequest();
   var protocol = this.options.secure ? 'https://' : 'http://';
-  var url = protocol + this.options.host + ':' + this.options.port + '/' + this.options.key + '/id';
+  var url = protocol + this.options.host + ':' + this.options.port + '/' + this._fullPath() + this.options.key + '/id';
   var queryString = '?ts=' + new Date().getTime() + '' + Math.random();
   url += queryString;
 
@@ -228,6 +228,10 @@ Peer.prototype._handleMessage = function(message) {
   }
 }
 
+Peer.prototype._fullPath = function() {
+  return this.options.path ? this.options.path + '/' : '';
+}
+
 /** Stores messages without a set up connection, to be claimed later. */
 Peer.prototype._storeMessage = function(connectionId, message) {
   if (!this._lostMessages[connectionId]) {

+ 6 - 4
lib/socket.js

@@ -2,8 +2,8 @@
  * An abstraction on top of WebSockets and XHR streaming to provide fastest
  * possible connection for peers.
  */
-function Socket(secure, host, port, key) {
-  if (!(this instanceof Socket)) return new Socket(secure, host, port, key);
+function Socket(secure, host, port, path, key) {
+  if (!(this instanceof Socket)) return new Socket(secure, host, port, path, key);
 
   EventEmitter.call(this);
 
@@ -13,8 +13,10 @@ function Socket(secure, host, port, key) {
 
   var httpProtocol = secure ? 'https://' : 'http://';
   var wsProtocol = secure ? 'wss://' : 'ws://';
-  this._httpUrl = httpProtocol + host + ':' + port + '/' + key;
-  this._wsUrl = wsProtocol + host + ':' + port + '/peerjs?key=' + key;
+  this._httpUrl = httpProtocol + host + ':' + port + '/'
+    + (path ? path + '/' : '') + key;
+  this._wsUrl = wsProtocol + host + ':' + port + '/'
+    + (path ? path + '/' : '') + 'peerjs?key=' + key;
 }
 
 util.inherits(Socket, EventEmitter);