You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

server.js 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. 'use strict';
  2. const assert = require('bsert');
  3. const EventEmitter = require('events');
  4. const Packet = require('./packet');
  5. const WebSocket = require('./backend');
  6. const Socket = require('./socket');
  7. class Server extends EventEmitter {
  8. constructor(options = {}) {
  9. super();
  10. assert(!options.protocols || Array.isArray(options.protocols));
  11. this.protocols = options.protocols || undefined;
  12. this.sockets = new Set();
  13. this.channels = new Map();
  14. this.mounts = [];
  15. this.mounted = false;
  16. }
  17. handleSocket(socket) {
  18. this.add(socket);
  19. socket.on('close', () => {
  20. this.remove(socket);
  21. });
  22. this.emit('socket', socket);
  23. for (const server of this.mounts)
  24. server.emit('socket', socket);
  25. }
  26. mount(server) {
  27. assert(!server.mounted);
  28. server.mounted = true;
  29. server.sockets = this.sockets;
  30. server.channels = this.channels;
  31. this.mounts.push(server);
  32. }
  33. async open() {
  34. ;
  35. }
  36. async close() {
  37. if (this.mounted)
  38. return;
  39. for (const socket of this.sockets)
  40. socket.destroy();
  41. }
  42. attach(server) {
  43. const onUpgrade = (req, socket, body) => {
  44. if (!socket.remoteAddress) {
  45. socket.destroy();
  46. return;
  47. }
  48. if (!WebSocket.isWebSocket(req)) {
  49. socket.destroy();
  50. return;
  51. }
  52. const ws = new WebSocket(req, socket, body, this.protocols);
  53. const sock = Socket.accept(this, req, socket, ws);
  54. this.handleSocket(sock);
  55. };
  56. server.on('upgrade', (req, socket, body) => {
  57. try {
  58. onUpgrade(req, socket, body);
  59. } catch (e) {
  60. this.emit('error', e);
  61. }
  62. });
  63. return this;
  64. }
  65. add(socket) {
  66. this.sockets.add(socket);
  67. }
  68. remove(socket) {
  69. for (const name of socket.channels)
  70. this.leave(socket, name);
  71. assert(this.sockets.delete(socket));
  72. }
  73. join(socket, name) {
  74. if (socket.channels.has(name))
  75. return false;
  76. if (!this.channels.has(name))
  77. this.channels.set(name, new Set());
  78. const sockets = this.channels.get(name);
  79. sockets.add(socket);
  80. socket.channels.add(name);
  81. return true;
  82. }
  83. leave(socket, name) {
  84. if (!socket.channels.has(name))
  85. return false;
  86. const sockets = this.channels.get(name);
  87. assert(sockets);
  88. assert(sockets.delete(socket));
  89. if (sockets.size === 0)
  90. this.channels.delete(name);
  91. socket.channels.delete(name);
  92. return true;
  93. }
  94. channel(name) {
  95. const sockets = this.channels.get(name);
  96. if (!sockets)
  97. return null;
  98. assert(sockets.size > 0);
  99. return sockets;
  100. }
  101. event(args) {
  102. assert(args.length > 0, 'Event must be present.');
  103. assert(typeof args[0] === 'string', 'Event must be a string.');
  104. const packet = new Packet();
  105. packet.type = Packet.types.EVENT;
  106. packet.setData(args);
  107. return packet;
  108. }
  109. to(name, ...args) {
  110. const sockets = this.channels.get(name);
  111. if (!sockets)
  112. return;
  113. assert(sockets.size > 0);
  114. // Pre-serialize for speed.
  115. const packet = this.event(args);
  116. for (const socket of sockets)
  117. socket.sendPacket(packet);
  118. }
  119. all(...args) {
  120. // Pre-serialize for speed.
  121. const packet = this.event(args);
  122. for (const socket of this.sockets)
  123. socket.sendPacket(packet);
  124. }
  125. static attach(parent, options) {
  126. const server = new this(options);
  127. return server.attach(parent);
  128. }
  129. static createServer(options) {
  130. return new this(options);
  131. }
  132. }
  133. module.exports = Server;