peerjs-server/lib/index.js
Vsevolod Strukchinsky 2ce55f8e33 Pass server or port option to PeerServer
This allows to initialize websocket server properly
2014-10-18 12:34:14 +01:00

78 lines
1.8 KiB
JavaScript

var express = require('express');
var mixin = require('utils-merge');
var proto = require('./server');
var util = require('./util');
var http = require('http');
exports = module.exports = {
PeerServer: createPeerServer
};
function createPeerServer(options, callback) {
var app = express();
mixin(app, proto);
app.options = {
host: '0.0.0.0',
port: null,
server: null,
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 = {};
// 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();
app._server = options.server;
if (app.options.port) {
app._server = http.createServer(app);
app._server.listen(options.port, options.host, callback);
}
if (!app._server) {
throw new Error('Neither port or server is passed to constructor - can\'t start PeerServer');
}
app._initializeWSS();
return app;
}