peerjs 2.6 KB

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