index.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. var express = require('express');
  2. var mixin = require('utils-merge');
  3. var proto = require('./server');
  4. var util = require('./util');
  5. var http = require('http');
  6. function createPeerServer(options, callback) {
  7. var app = express();
  8. mixin(app, proto);
  9. app.options = {
  10. host: '0.0.0.0',
  11. port: null,
  12. server: null,
  13. debug: false,
  14. timeout: 5000,
  15. key: 'peerjs',
  16. ip_limit: 5000,
  17. concurrent_limit: 5000,
  18. ssl: {},
  19. path: '/',
  20. allow_discovery: false
  21. };
  22. mixin(app.options, options);
  23. // Print warning if only one of the two is given.
  24. if (Object.keys(app.options.ssl).length === 1) {
  25. util.prettyError('Warning: PeerServer will not run on an HTTPS server' +
  26. ' because either the key or the certificate has not been provided.');
  27. }
  28. app.options.ssl.name = 'PeerServer';
  29. if (app.options.path[0] !== '/') {
  30. app.options.path = '/' + app.options.path;
  31. }
  32. if (app.options.path[app.options.path.length - 1] !== '/') {
  33. app.options.path += '/';
  34. }
  35. // Connected clients
  36. app._clients = {};
  37. // Messages waiting for another peer.
  38. app._outstanding = {};
  39. // Initialize HTTP routes. This is only used for the first few milliseconds
  40. // before a socket is opened for a Peer.
  41. app._initializeHTTP();
  42. // Mark concurrent users per ip
  43. app._ips = {};
  44. app._setCleanupIntervals();
  45. app._server = options.server;
  46. if (!app._server) {
  47. throw new Error('Server is not passed to constructor - can\'t start PeerServer');
  48. }
  49. app._initializeWSS();
  50. return app;
  51. }
  52. exports = module.exports = createPeerServer;