|
@@ -5,95 +5,95 @@ var http = require('http');
|
|
var https = require('https');
|
|
var https = require('https');
|
|
|
|
|
|
function ExpressPeerServer(server, options) {
|
|
function ExpressPeerServer(server, options) {
|
|
- var app = express();
|
|
|
|
|
|
+ var app = express();
|
|
|
|
|
|
- util.extend(app, proto);
|
|
|
|
|
|
+ util.extend(app, proto);
|
|
|
|
|
|
- options = app._options = util.extend({
|
|
|
|
- debug: false,
|
|
|
|
- timeout: 5000,
|
|
|
|
- key: 'peerjs',
|
|
|
|
- ip_limit: 5000,
|
|
|
|
- concurrent_limit: 5000,
|
|
|
|
- allow_discovery: false,
|
|
|
|
- proxied: false
|
|
|
|
- }, options);
|
|
|
|
|
|
+ options = app._options = util.extend({
|
|
|
|
+ debug: false,
|
|
|
|
+ timeout: 5000,
|
|
|
|
+ key: 'peerjs',
|
|
|
|
+ ip_limit: 5000,
|
|
|
|
+ concurrent_limit: 5000,
|
|
|
|
+ allow_discovery: false,
|
|
|
|
+ proxied: false
|
|
|
|
+ }, options);
|
|
|
|
|
|
- // Connected clients
|
|
|
|
- app._clients = {};
|
|
|
|
|
|
+ // Connected clients
|
|
|
|
+ app._clients = {};
|
|
|
|
|
|
- // Messages waiting for another peer.
|
|
|
|
- app._outstanding = {};
|
|
|
|
|
|
+ // Messages waiting for another peer.
|
|
|
|
+ app._outstanding = {};
|
|
|
|
|
|
- // Mark concurrent users per ip
|
|
|
|
- app._ips = {};
|
|
|
|
|
|
+ // Mark concurrent users per ip
|
|
|
|
+ app._ips = {};
|
|
|
|
|
|
- if (options.proxied) {
|
|
|
|
- app.set('trust proxy', options.proxied);
|
|
|
|
|
|
+ if (options.proxied) {
|
|
|
|
+ app.set('trust proxy', options.proxied);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ app.on('mount', function() {
|
|
|
|
+ if (!server) {
|
|
|
|
+ throw new Error('Server is not passed to constructor - '+
|
|
|
|
+ 'can\'t start PeerServer');
|
|
}
|
|
}
|
|
|
|
|
|
- app.on('mount', function() {
|
|
|
|
- if (!server) {
|
|
|
|
- throw new Error('Server is not passed to constructor - '+
|
|
|
|
- 'can\'t start PeerServer');
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- // Initialize HTTP routes. This is only used for the first few milliseconds
|
|
|
|
- // before a socket is opened for a Peer.
|
|
|
|
- app._initializeHTTP();
|
|
|
|
- app._setCleanupIntervals();
|
|
|
|
- app._initializeWSS(server);
|
|
|
|
- });
|
|
|
|
|
|
+ // Initialize HTTP routes. This is only used for the first few milliseconds
|
|
|
|
+ // before a socket is opened for a Peer.
|
|
|
|
+ app._initializeHTTP();
|
|
|
|
+ app._setCleanupIntervals();
|
|
|
|
+ app._initializeWSS(server);
|
|
|
|
+ });
|
|
|
|
|
|
- return app;
|
|
|
|
|
|
+ return app;
|
|
}
|
|
}
|
|
|
|
|
|
function PeerServer(options, callback) {
|
|
function PeerServer(options, callback) {
|
|
- var app = express();
|
|
|
|
|
|
+ var app = express();
|
|
|
|
|
|
- options = options || {};
|
|
|
|
- var path = options.path || '/';
|
|
|
|
- var port = options.port || 80;
|
|
|
|
|
|
+ options = options || {};
|
|
|
|
+ var path = options.path || '/';
|
|
|
|
+ var port = options.port || 80;
|
|
|
|
|
|
- delete options.path;
|
|
|
|
|
|
+ delete options.path;
|
|
|
|
|
|
- if (path[0] !== '/') {
|
|
|
|
- path = '/' + path;
|
|
|
|
- }
|
|
|
|
|
|
+ if (path[0] !== '/') {
|
|
|
|
+ path = '/' + path;
|
|
|
|
+ }
|
|
|
|
|
|
- if (path[path.length - 1] !== '/') {
|
|
|
|
- path += '/';
|
|
|
|
- }
|
|
|
|
|
|
+ if (path[path.length - 1] !== '/') {
|
|
|
|
+ path += '/';
|
|
|
|
+ }
|
|
|
|
|
|
- var server;
|
|
|
|
- if (options.ssl) {
|
|
|
|
- if (options.ssl.certificate) {
|
|
|
|
- // Preserve compatibility with 0.2.7 API
|
|
|
|
- options.ssl.cert = options.ssl.certificate;
|
|
|
|
- delete options.ssl.certificate;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- server = https.createServer(options.ssl, app);
|
|
|
|
- delete options.ssl;
|
|
|
|
- } else {
|
|
|
|
- server = http.createServer(app);
|
|
|
|
|
|
+ var server;
|
|
|
|
+ if (options.ssl) {
|
|
|
|
+ if (options.ssl.certificate) {
|
|
|
|
+ // Preserve compatibility with 0.2.7 API
|
|
|
|
+ options.ssl.cert = options.ssl.certificate;
|
|
|
|
+ delete options.ssl.certificate;
|
|
}
|
|
}
|
|
|
|
|
|
- var peerjs = ExpressPeerServer(server, options);
|
|
|
|
- app.use(path, peerjs);
|
|
|
|
|
|
+ server = https.createServer(options.ssl, app);
|
|
|
|
+ delete options.ssl;
|
|
|
|
+ } else {
|
|
|
|
+ server = http.createServer(app);
|
|
|
|
+ }
|
|
|
|
|
|
- if (callback) {
|
|
|
|
- server.listen(port, function() {
|
|
|
|
- callback(server);
|
|
|
|
- });
|
|
|
|
- } else {
|
|
|
|
- server.listen(port);
|
|
|
|
- }
|
|
|
|
|
|
+ var peerjs = ExpressPeerServer(server, options);
|
|
|
|
+ app.use(path, peerjs);
|
|
|
|
+
|
|
|
|
+ if (callback) {
|
|
|
|
+ server.listen(port, function() {
|
|
|
|
+ callback(server);
|
|
|
|
+ });
|
|
|
|
+ } else {
|
|
|
|
+ server.listen(port);
|
|
|
|
+ }
|
|
|
|
|
|
- return peerjs;
|
|
|
|
|
|
+ return peerjs;
|
|
}
|
|
}
|
|
|
|
|
|
exports = module.exports = {
|
|
exports = module.exports = {
|
|
- ExpressPeerServer: ExpressPeerServer,
|
|
|
|
- PeerServer: PeerServer
|
|
|
|
|
|
+ ExpressPeerServer: ExpressPeerServer,
|
|
|
|
+ PeerServer: PeerServer
|
|
};
|
|
};
|