forked from peers/peerjs-server
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 peers#36
- Loading branch information
Showing
7 changed files
with
670 additions
and
699 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
Oops, something went wrong.