Advertisement
Guest User

Untitled

a guest
Mar 24th, 2023
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.50 KB | None | 0 0
  1. `
  2. @@ -60,15 +61,16 @@ class RedisAdapter extends socket_io_adapter_1.Adapter {
  3. + const subEvent = opts.subEvent || 'messageBuffer';
  4. const onError = (err) => {
  5. if (err) {
  6. this.emit("error", err);
  7. }
  8. };
  9. - this.subClient.psubscribe(this.channel + "*", onError);
  10. - this.subClient.on("pmessageBuffer", this.onmessage.bind(this));
  11. - this.subClient.subscribe([this.requestChannel, this.responseChannel], onError);
  12. - this.subClient.on("messageBuffer", this.onrequest.bind(this));
  13. + this.subClient.subscribe([this.channel, this.requestChannel, this.responseChannel], onError);
  14. + this.subClient.on(subEvent, this.onmessage.bind(this));
  15. this.pubClient.on("error", onError);
  16. this.subClient.on("error", onError);
  17. }
  18. @@ -77,11 +79,19 @@ class RedisAdapter extends socket_io_adapter_1.Adapter {
  19. *
  20. * @private
  21. */
  22. - onmessage(pattern, channel, msg) {
  23. + onmessage(channel, msg) {
  24. channel = channel.toString();
  25. - const channelMatches = channel.startsWith(this.channel);
  26. - if (!channelMatches) {
  27. - return debug("ignore different channel");
  28. + // const channelMatches = channel.startsWith(this.channel);
  29. + // if (!channelMatches) {
  30. + // return debug("ignore different channel");
  31. + // }
  32. +
  33. + if (channel.startsWith(this.requestChannel)) {
  34. + return this.onrequest(channel, msg);
  35. + } else if (channel.startsWith(this.responseChannel)) {
  36. + return this.onresponse(channel, msg);
  37. + } else if (!channel.startsWith(this.channel)) {
  38. + return debug('ignore different channel');
  39. }
  40. const room = channel.slice(this.channel.length, -1);
  41. if (room !== "" && !this.rooms.has(room)) {
  42. @@ -111,12 +121,12 @@ class RedisAdapter extends socket_io_adapter_1.Adapter {
  43. if (channel.startsWith(this.responseChannel)) {
  44. return this.onresponse(channel, msg);
  45. }
  46. - else if (!channel.startsWith(this.requestChannel)) {
  47. - return debug("ignore different channel");
  48. - }
  49. + // else if (!channel.startsWith(this.requestChannel)) {
  50. + // return debug("ignore different channel");
  51. + // }
  52. let request;
  53. try {
  54. - request = JSON.parse(msg);
  55. + request = JSON.parse(msg.toString());
  56. }
  57. catch (err) {
  58. this.emit("error", err);
  59. @@ -607,5 +617,32 @@ class RedisAdapter extends socket_io_adapter_1.Adapter {
  60. });
  61. }
  62. }
  63. +
  64. + addAll(id, rooms) {
  65. + const roomsArr = Array.from(rooms);
  66. + roomsArr.forEach((room) => {
  67. + const roomName = this.channel + room + '#';
  68. + this.subClient.subscribe(roomName);
  69. + });
  70. + return super.addAll(id, rooms);
  71. + }
  72. +
  73. + del(id, room) {
  74. + const unsubRoom = this.channel + room + '#';
  75. + this.subClient.unsubscribe(unsubRoom);
  76. + return super.del(id, room)
  77. + }
  78. +
  79. + delAll (id) {
  80. + const roomsSet = this.sids.get(id);
  81. + if (roomsSet !== undefined) {
  82. + const roomsArr = Array.from(roomsSet);
  83. + roomsArr.forEach((room) => {
  84. + const roomName = this.channel + room + '#';
  85. + this.subClient.unsubscribe(roomName);
  86. + });
  87. + }
  88. + return super.delAll(id);
  89. + }
  90. }
  91. exports.RedisAdapter = RedisAdapter;
  92. `
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement