peerjs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. debug: {
  11. demand: false,
  12. alias: 'd',
  13. description: 'debug',
  14. default: false
  15. },
  16. timeout: {
  17. demand: false,
  18. alias: 't',
  19. description: 'timeout (milliseconds)',
  20. default: 5000
  21. },
  22. ip_limit: {
  23. demand: false,
  24. alias: 'i',
  25. description: 'IP limit',
  26. default: 5000
  27. },
  28. concurrent_limit: {
  29. demand: false,
  30. alias: 'c',
  31. description: 'concurrent limit',
  32. default: 5000
  33. },
  34. key: {
  35. demand: false,
  36. alias: 'k',
  37. description: 'connection key',
  38. default: 'peerjs'
  39. },
  40. sslkey: {
  41. demand: false,
  42. description: 'path to SSL key'
  43. },
  44. sslcert: {
  45. demand: false,
  46. description: 'path to SSL certificate'
  47. },
  48. port: {
  49. demand: true,
  50. alias: 'p',
  51. description: 'port'
  52. },
  53. path: {
  54. demand: false,
  55. description: 'custom path',
  56. default: '/'
  57. },
  58. allow_discovery: {
  59. demand: false,
  60. description: 'allow discovery of peers'
  61. }
  62. })
  63. .boolean('allow_discovery')
  64. .argv;
  65. process.on('uncaughtException', function (e) {
  66. console.error('Error: ' + e);
  67. });
  68. if (opts.sslkey || opts.sslcert) {
  69. if (opts.sslkey && opts.sslcert) {
  70. opts.ssl = {
  71. key: fs.readFileSync(path.resolve(opts.sslkey)),
  72. cert: fs.readFileSync(path.resolve(opts.sslcert))
  73. };
  74. delete opts.sslkey;
  75. delete opts.sslcert;
  76. } else {
  77. console.error('Warning: PeerServer will not run because either ' +
  78. 'the key or the certificate has not been provided.');
  79. process.exit(1);
  80. }
  81. }
  82. const userPath = opts.path;
  83. const server = PeerServer(opts, server => {
  84. var host = server.address().address;
  85. var port = server.address().port;
  86. console.log(
  87. 'Started PeerServer on %s, port: %s, path: %s (v. %s)',
  88. host, port, userPath || '/', version
  89. );
  90. });
  91. server.on('connection', client => {
  92. console.log(`Client connected: ${client.getId()}`);
  93. });
  94. server.on('disconnect', client => {
  95. console.log(`Client disconnected: ${client.getId()}`);
  96. });