Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

sockets.js 5.0 KiB

há 2 anos
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /**
  2. * emits an event to all clients filtered by the given options
  3. * @param {string} namespace - only to the specified namespace
  4. * @param {string} room - emit only to the specified room (can be combined with namespace)
  5. * @param {string} eventName - the name of the event
  6. * @param {object} params - any parameters/data to send with the event
  7. */
  8. exports.emit = function(options) {
  9. if (this.io) {
  10. options = this.parse(options);
  11. if (options.namespace) {
  12. if (options.room) {
  13. this.io.of(options.namespace).to(options.room).emit(options.eventName, options.params);
  14. } else {
  15. this.io.of(options.namespace).emit(options.eventName, options.params);
  16. }
  17. } else {
  18. if (options.room) {
  19. this.io.in(options.room).emit(options.eventName, options.params);
  20. } else {
  21. this.io.emit(options.eventName, options.params);
  22. }
  23. }
  24. }
  25. };
  26. /**
  27. * emit an event to all clients except the sender
  28. * @param {string} room - broadcast in a specific room
  29. * @param {string} eventName - the name of the event
  30. * @param {object} params - the parameters/data to send with the event
  31. */
  32. exports.broadcast = function(options) {
  33. if (this.socket) {
  34. options = this.parse(options);
  35. if (options.room) {
  36. this.socket.to(options.room).emit(options.eventName, options.params);
  37. } else {
  38. this.socket.broadcast.emit(options.eventName, options.params);
  39. }
  40. }
  41. };
  42. /**
  43. * emit an event to client and wait for an answer
  44. * @param {string} room - broadcast in a specific room
  45. * @param {string} eventName - the name of the event
  46. * @param {object} params - the parameters/data to send with the event
  47. */
  48. exports.request = function(options) {
  49. if (this.socket) {
  50. options = this.parse(options);
  51. return new Promise((resolve) => {
  52. this.socket.emit(options.eventName, options.params, resolve);
  53. });
  54. }
  55. return null;
  56. };
  57. /**
  58. * send a private meessage to a socket
  59. * @param {string} socketId - the socket id to send the message to
  60. * @param {string} eventName - the name of the event
  61. * @param {object} params - the parameters/data to send with the event
  62. */
  63. exports.message = function(options) {
  64. if (this.io) {
  65. options = this.parse(options);
  66. this.io.to(options.socketId).emit(options.eventName, options.params);
  67. }
  68. };
  69. /**
  70. * special serverconnect refresh broadcast
  71. * @param {string} action - the action that require refresh
  72. */
  73. exports.refresh = async function(options) {
  74. if (this.io) {
  75. options = this.parse(options);
  76. // Do we have a global redis client?
  77. if (global.redisClient) {
  78. try { // ignore any errors here
  79. let wsKeys = await global.redisClient.keys('ws:' + options.action + ':*');
  80. if (wsKeys.length) await global.redisClient.del(wsKeys);
  81. let scKeys = await global.redisClient.keys('erc:' + '/api/' + options.action + '*');
  82. if (scKeys.length) await global.redisClient.del(scKeys);
  83. } catch (e) {
  84. console.error(e);
  85. }
  86. }
  87. this.io.of('/api').emit(options.action, options.params);
  88. }
  89. };
  90. /**
  91. * let current client join a room
  92. * @param {string} room - the room to join
  93. */
  94. exports.join = function(options) {
  95. if (this.socket) {
  96. this.socket.join(this.parse(options.room));
  97. }
  98. };
  99. /**
  100. * let current client leave a room
  101. * @param {string} room - the room to leave
  102. */
  103. exports.leave = function(options) {
  104. if (this.socket) {
  105. this.socket.leave(this.parse(options.room));
  106. }
  107. };
  108. /**
  109. * get the socket id of the current client
  110. */
  111. exports.identify = function(options) {
  112. return this.socket ? this.socket.id : null;
  113. };
  114. /**
  115. * get the rooms the current client has joined
  116. */
  117. exports.rooms = function(options) {
  118. return this.socket ? Array.from(this.socket.rooms) : [];
  119. };
  120. /**
  121. * get all the rooms
  122. * @param {string} namespace - the namespace
  123. */
  124. exports.allRooms = async function(options) {
  125. if (this.io) {
  126. let adapter = io.of(options.namespace || '/').adapter;
  127. if (typeof adapter.allRooms == 'function') {
  128. return Array.from(await adapter.allRooms());
  129. } else if (adapter.rooms) {
  130. return Array.from(adapter.rooms.keys());
  131. }
  132. }
  133. return [];
  134. };
  135. /**
  136. * get all the connected sockets
  137. * @param {string} namespace - the namespace
  138. * @param {string} room - return only clients in a specific room
  139. */
  140. exports.allSockets = async function(options) {
  141. if (this.io) {
  142. options = this.parse(options);
  143. if (options.room) {
  144. return Array.from(await this.io.of(options.namespace || '/').in(options.room).allSockets());
  145. } else {
  146. return Array.from(await this.io.of(options.namespace || '/').allSockets());
  147. }
  148. }
  149. return [];
  150. };