Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
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.

index.js 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.Adapter = void 0;
  4. const events_1 = require("events");
  5. class Adapter extends events_1.EventEmitter {
  6. /**
  7. * In-memory adapter constructor.
  8. *
  9. * @param {Namespace} nsp
  10. */
  11. constructor(nsp) {
  12. super();
  13. this.nsp = nsp;
  14. this.rooms = new Map();
  15. this.sids = new Map();
  16. this.encoder = nsp.server.encoder;
  17. }
  18. /**
  19. * To be overridden
  20. */
  21. init() { }
  22. /**
  23. * To be overridden
  24. */
  25. close() { }
  26. /**
  27. * Adds a socket to a list of room.
  28. *
  29. * @param {SocketId} id the socket id
  30. * @param {Set<Room>} rooms a set of rooms
  31. * @public
  32. */
  33. addAll(id, rooms) {
  34. if (!this.sids.has(id)) {
  35. this.sids.set(id, new Set());
  36. }
  37. for (const room of rooms) {
  38. this.sids.get(id).add(room);
  39. if (!this.rooms.has(room)) {
  40. this.rooms.set(room, new Set());
  41. this.emit("create-room", room);
  42. }
  43. if (!this.rooms.get(room).has(id)) {
  44. this.rooms.get(room).add(id);
  45. this.emit("join-room", room, id);
  46. }
  47. }
  48. }
  49. /**
  50. * Removes a socket from a room.
  51. *
  52. * @param {SocketId} id the socket id
  53. * @param {Room} room the room name
  54. */
  55. del(id, room) {
  56. if (this.sids.has(id)) {
  57. this.sids.get(id).delete(room);
  58. }
  59. this._del(room, id);
  60. }
  61. _del(room, id) {
  62. const _room = this.rooms.get(room);
  63. if (_room != null) {
  64. const deleted = _room.delete(id);
  65. if (deleted) {
  66. this.emit("leave-room", room, id);
  67. }
  68. if (_room.size === 0 && this.rooms.delete(room)) {
  69. this.emit("delete-room", room);
  70. }
  71. }
  72. }
  73. /**
  74. * Removes a socket from all rooms it's joined.
  75. *
  76. * @param {SocketId} id the socket id
  77. */
  78. delAll(id) {
  79. if (!this.sids.has(id)) {
  80. return;
  81. }
  82. for (const room of this.sids.get(id)) {
  83. this._del(room, id);
  84. }
  85. this.sids.delete(id);
  86. }
  87. /**
  88. * Broadcasts a packet.
  89. *
  90. * Options:
  91. * - `flags` {Object} flags for this packet
  92. * - `except` {Array} sids that should be excluded
  93. * - `rooms` {Array} list of rooms to broadcast to
  94. *
  95. * @param {Object} packet the packet object
  96. * @param {Object} opts the options
  97. * @public
  98. */
  99. broadcast(packet, opts) {
  100. const flags = opts.flags || {};
  101. const basePacketOpts = {
  102. preEncoded: true,
  103. volatile: flags.volatile,
  104. compress: flags.compress
  105. };
  106. packet.nsp = this.nsp.name;
  107. const encodedPackets = this.encoder.encode(packet);
  108. const packetOpts = encodedPackets.map(encodedPacket => {
  109. if (typeof encodedPacket === "string") {
  110. return Object.assign(Object.assign({}, basePacketOpts), { wsPreEncoded: "4" + encodedPacket // "4" being the "message" packet type in Engine.IO
  111. });
  112. }
  113. else {
  114. return basePacketOpts;
  115. }
  116. });
  117. this.apply(opts, socket => {
  118. for (let i = 0; i < encodedPackets.length; i++) {
  119. socket.client.writeToEngine(encodedPackets[i], packetOpts[i]);
  120. }
  121. });
  122. }
  123. /**
  124. * Gets a list of sockets by sid.
  125. *
  126. * @param {Set<Room>} rooms the explicit set of rooms to check.
  127. */
  128. sockets(rooms) {
  129. const sids = new Set();
  130. this.apply({ rooms }, socket => {
  131. sids.add(socket.id);
  132. });
  133. return Promise.resolve(sids);
  134. }
  135. /**
  136. * Gets the list of rooms a given socket has joined.
  137. *
  138. * @param {SocketId} id the socket id
  139. */
  140. socketRooms(id) {
  141. return this.sids.get(id);
  142. }
  143. /**
  144. * Returns the matching socket instances
  145. *
  146. * @param opts - the filters to apply
  147. */
  148. fetchSockets(opts) {
  149. const sockets = [];
  150. this.apply(opts, socket => {
  151. sockets.push(socket);
  152. });
  153. return Promise.resolve(sockets);
  154. }
  155. /**
  156. * Makes the matching socket instances join the specified rooms
  157. *
  158. * @param opts - the filters to apply
  159. * @param rooms - the rooms to join
  160. */
  161. addSockets(opts, rooms) {
  162. this.apply(opts, socket => {
  163. socket.join(rooms);
  164. });
  165. }
  166. /**
  167. * Makes the matching socket instances leave the specified rooms
  168. *
  169. * @param opts - the filters to apply
  170. * @param rooms - the rooms to leave
  171. */
  172. delSockets(opts, rooms) {
  173. this.apply(opts, socket => {
  174. rooms.forEach(room => socket.leave(room));
  175. });
  176. }
  177. /**
  178. * Makes the matching socket instances disconnect
  179. *
  180. * @param opts - the filters to apply
  181. * @param close - whether to close the underlying connection
  182. */
  183. disconnectSockets(opts, close) {
  184. this.apply(opts, socket => {
  185. socket.disconnect(close);
  186. });
  187. }
  188. apply(opts, callback) {
  189. const rooms = opts.rooms;
  190. const except = this.computeExceptSids(opts.except);
  191. if (rooms.size) {
  192. const ids = new Set();
  193. for (const room of rooms) {
  194. if (!this.rooms.has(room))
  195. continue;
  196. for (const id of this.rooms.get(room)) {
  197. if (ids.has(id) || except.has(id))
  198. continue;
  199. const socket = this.nsp.sockets.get(id);
  200. if (socket) {
  201. callback(socket);
  202. ids.add(id);
  203. }
  204. }
  205. }
  206. }
  207. else {
  208. for (const [id] of this.sids) {
  209. if (except.has(id))
  210. continue;
  211. const socket = this.nsp.sockets.get(id);
  212. if (socket)
  213. callback(socket);
  214. }
  215. }
  216. }
  217. computeExceptSids(exceptRooms) {
  218. const exceptSids = new Set();
  219. if (exceptRooms && exceptRooms.size > 0) {
  220. for (const room of exceptRooms) {
  221. if (this.rooms.has(room)) {
  222. this.rooms.get(room).forEach(sid => exceptSids.add(sid));
  223. }
  224. }
  225. }
  226. return exceptSids;
  227. }
  228. /**
  229. * Send a packet to the other Socket.IO servers in the cluster
  230. * @param packet - an array of arguments, which may include an acknowledgement callback at the end
  231. */
  232. serverSideEmit(packet) {
  233. throw new Error("this adapter does not support the serverSideEmit() functionality");
  234. }
  235. }
  236. exports.Adapter = Adapter;