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