Skip to content

Commit

Permalink
Migrate to express from restify
Browse files Browse the repository at this point in the history
PeerServer is now instance of express application - this allows to combine it with other express applications.
Listen is deferred - because it now method from express (you can call it later and supply callback for start event).
Constructor now not using `new` (because we mixing in methods to express app)

Closes #36
  • Loading branch information
floatdrop authored and lmb committed Oct 18, 2014
1 parent 5c9d1a0 commit 9e0d861
Show file tree
Hide file tree
Showing 6 changed files with 632 additions and 677 deletions.
5 changes: 3 additions & 2 deletions README.md
Expand Up @@ -46,13 +46,14 @@ 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,
var server = PeerServer({
ssl: {
key: fs.readFileSync('/path/to/your/ssl/key/here.key'),
certificate: fs.readFileSync('/path/to/your/ssl/certificate/here.crt')
}
});

server.listen(9000);
```

### Events
Expand Down
125 changes: 63 additions & 62 deletions bin/peerjs
@@ -1,81 +1,82 @@
#!/usr/bin/env node

var path = require('path')
, pkg = require('../package.json')
, fs = require('fs')
, version = pkg.version
, PeerServer = require('../lib/server').PeerServer
, opts = require('optimist')
.usage('Usage: $0')
.options({
debug: {
demand: false,
alias: 'd',
description: 'debug',
default: false
},
timeout: {
demand: false,
alias: 't',
description: 'timeout (milliseconds)',
, pkg = require('../package.json')
, fs = require('fs')
, version = pkg.version
, PeerServer = require('../lib')()
, opts = require('optimist')
.usage('Usage: $0')
.options({
debug: {
demand: false,
alias: 'd',
description: 'debug',
default: false
},
timeout: {
demand: false,
alias: 't',
description: 'timeout (milliseconds)',
default: 5000
},
ip_limit: {
demand: false,
alias: 'i',
description: 'IP limit',
},
ip_limit: {
demand: false,
alias: 'i',
description: 'IP limit',
default: 5000
},
concurrent_limit: {
demand: false,
alias: 'c',
description: 'concurrent limit',
},
concurrent_limit: {
demand: false,
alias: 'c',
description: 'concurrent limit',
default: 5000
},
key: {
demand: false,
alias: 'k',
description: 'connection key',
},
key: {
demand: false,
alias: 'k',
description: 'connection key',
default: 'peerjs'
},
sslkey: {
demand: false,
description: 'path to SSL key'
},
sslcert: {
demand: false,
description: 'path to SSL certificate'
},
port: {
demand: true,
alias: 'p',
description: 'port'
},
path: {
demand: false,
description: 'custom path'
},
allow_discovery: {
demand: false,
description: 'allow discovery of peers'
}
})
.boolean('allow_discovery')
.argv;
},
sslkey: {
demand: false,
description: 'path to SSL key'
},
sslcert: {
demand: false,
description: 'path to SSL certificate'
},
port: {
demand: true,
alias: 'p',
description: 'port'
},
path: {
demand: false,
description: 'custom path'
},
allow_discovery: {
demand: false,
description: 'allow discovery of peers'
}
})
.boolean('allow_discovery')
.argv;

opts.version = version;

if (opts.sslkey && opts.sslcert) {
opts['ssl'] = {};
opts.ssl['key'] = fs.readFileSync(path.resolve(opts.sslkey));
opts.ssl['certificate'] = fs.readFileSync(path.resolve(opts.sslcert));
opts['ssl'] = {};
opts.ssl['key'] = fs.readFileSync(path.resolve(opts.sslkey));
opts.ssl['certificate'] = fs.readFileSync(path.resolve(opts.sslcert));
}

process.on('uncaughtException', function(e) {
console.error('Error: ' + e);
console.error('Error: ' + e);
});

var server = new PeerServer(opts);
server.listen(opts.port);
console.log(
'Started PeerServer, port: ' + opts.port + ', path: ' + (opts.path || '/') + (" (v. %s)"), version
'Started PeerServer, port: ' + opts.port + ', path: ' + (opts.path || '/') + (" (v. %s)"), version
);
62 changes: 62 additions & 0 deletions lib/index.js
@@ -0,0 +1,62 @@
var express = require('express');
var mixin = require('utils-merge');
var proto = require('./server');

exports = module.exports = {
PeerServer: createPeerServer
};

function createPeerServer(options) {

var app = express();

mixin(app, proto);

app.options = {
debug: false,
timeout: 5000,
key: 'peerjs',
ip_limit: 5000,
concurrent_limit: 5000,
ssl: {},
path: '/',
allow_discovery: false
};

mixin(app.options, options);

// Print warning if only one of the two is given.
if (Object.keys(app.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.');
}

app.options.ssl.name = 'PeerServer';

if (app.options.path[0] !== '/') {
app.options.path = '/' + app.options.path;
}
if (app.options.path[app.options.path.length - 1] !== '/') {
app.options.path += '/';
}

// Connected clients
app._clients = {};

// Messages waiting for another peer.
app._outstanding = {};

// Initailize WebSocket server handlers.
app._initializeWSS();

// Initialize HTTP routes. This is only used for the first few milliseconds
// before a socket is opened for a Peer.
app._initializeHTTP();

// Mark concurrent users per ip
app._ips = {};

app._setCleanupIntervals();

return app;
}

0 comments on commit 9e0d861

Please sign in to comment.