peerjs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #!/usr/bin/env node
  2. const path = require('path');
  3. const pkg = require('../package.json');
  4. const fs = require('fs');
  5. const version = pkg.version;
  6. const PeerServer = require('../src').PeerServer;
  7. const opts = require('optimist')
  8. .usage('Usage: $0')
  9. .options({
  10. expire_timeout: {
  11. demand: false,
  12. alias: 't',
  13. description: 'timeout (milliseconds)',
  14. default: 5000
  15. },
  16. concurrent_limit: {
  17. demand: false,
  18. alias: 'c',
  19. description: 'concurrent limit',
  20. default: 5000
  21. },
  22. key: {
  23. demand: false,
  24. alias: 'k',
  25. description: 'connection key',
  26. default: 'peerjs'
  27. },
  28. sslkey: {
  29. demand: false,
  30. description: 'path to SSL key'
  31. },
  32. sslcert: {
  33. demand: false,
  34. description: 'path to SSL certificate'
  35. },
  36. host: {
  37. demand: false,
  38. description: 'ip',
  39. default: '0.0.0.0'
  40. },
  41. port: {
  42. demand: true,
  43. alias: 'p',
  44. description: 'port'
  45. },
  46. path: {
  47. demand: false,
  48. description: 'custom path',
  49. default: '/'
  50. },
  51. allow_discovery: {
  52. demand: false,
  53. description: 'allow discovery of peers'
  54. },
  55. proxied: {
  56. demand: false,
  57. description: 'Set true if PeerServer stays behind a reverse proxy',
  58. default: false
  59. }
  60. })
  61. .boolean('allow_discovery')
  62. .argv;
  63. process.on('uncaughtException', function (e) {
  64. console.error('Error: ' + e);
  65. });
  66. if (opts.sslkey || opts.sslcert) {
  67. if (opts.sslkey && opts.sslcert) {
  68. opts.ssl = {
  69. key: fs.readFileSync(path.resolve(opts.sslkey)),
  70. cert: fs.readFileSync(path.resolve(opts.sslcert))
  71. };
  72. delete opts.sslkey;
  73. delete opts.sslcert;
  74. } else {
  75. console.error('Warning: PeerServer will not run because either ' +
  76. 'the key or the certificate has not been provided.');
  77. process.exit(1);
  78. }
  79. }
  80. const userPath = opts.path;
  81. const server = PeerServer(opts, server => {
  82. var host = server.address().address;
  83. var port = server.address().port;
  84. console.log(
  85. 'Started PeerServer on %s, port: %s, path: %s (v. %s)',
  86. host, port, userPath || '/', version
  87. );
  88. });
  89. server.on('connection', client => {
  90. console.log(`Client connected: ${client.getId()}`);
  91. });
  92. server.on('disconnect', client => {
  93. console.log(`Client disconnected: ${client.getId()}`);
  94. });