peerjs 2.3 KB

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