diff --git a/README.md b/README.md index dc15ba1..9fc4515 100644 --- a/README.md +++ b/README.md @@ -10,22 +10,35 @@ PeerServer helps broker connections between PeerJS clients. Data is not proxied ### Run PeerServer -Install the library +Install the library: - npm install peer + npm install peer -Run the server +Run the server: var PeerServer = require('peer').PeerServer; var server = new PeerServer({ port: 9000 }); - -Connecting to the server from PeerJS - + +Connecting to the server from PeerJS: + +Using HTTPS: Simply pass in PEM-encoded certificate and key. + + var fs = require('fs'); + var PeerServer = require('peer').PeerServer; + + var server = new PeerServer({ + port: 9000, + ssl: { + key: fs.readFileSync('/path/to/your/ssl/key/here.key'), + certificate: fs.readFileSync('/path/to/your/ssl/certificate/here.crt') + } + }); + ## Problems? Discuss PeerJS on our Google Group: diff --git a/lib/server.js b/lib/server.js index 4f3483a..5cf2572 100644 --- a/lib/server.js +++ b/lib/server.js @@ -9,20 +9,29 @@ function PeerServer(options) { if (!(this instanceof PeerServer)) return new PeerServer(options); EventEmitter.call(this); - this._app = restify.createServer(); - this._httpServer = this._app; - this._options = util.extend({ port: 80, debug: false, timeout: 5000, key: 'peerjs', ip_limit: 5000, - concurrent_limit: 5000 + concurrent_limit: 5000, + ssl: {} }, options); util.debug = this._options.debug; + // Set up HTTPS server if key and certificate are provided. + var secure = this._options.ssl.key && this._options.ssl.certificate; + // Print warning if only one of the two is given. + if (Object.keys(this._options.ssl).length === 1) { + util.prettyError('Warning: PeerServer will not run on an HTTPS server' + + ' because either the key or the certificate has not been provided.'); + } + + this._options.ssl['name'] = 'PeerServer'; + this._app = restify.createServer(this._options.ssl); + // Connected clients this._clients = {}; @@ -50,7 +59,7 @@ PeerServer.prototype._initializeWSS = function() { var self = this; // Create WebSocket server as well. - this._wss = new WebSocketServer({ path: '/peerjs', server: this._httpServer }); + this._wss = new WebSocketServer({ path: '/peerjs', server: this._app}); this._wss.on('connection', function(socket) { var query = url.parse(socket.upgradeReq.url, true).query; @@ -253,7 +262,7 @@ PeerServer.prototype._initializeHTTP = function() { this._app.post('/:key/:id/:token/leave', handle); // Listen on user-specified port. - this._httpServer.listen(this._options.port); + this._app.listen(this._options.port); }; /** Saves a streaming response and takes care of timeouts and headers. */