index.d.ts 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /// <reference types="node" />
  2. import { EventEmitter } from "events";
  3. export declare type SocketId = string;
  4. export declare type Room = string;
  5. export interface BroadcastFlags {
  6. volatile?: boolean;
  7. compress?: boolean;
  8. local?: boolean;
  9. broadcast?: boolean;
  10. binary?: boolean;
  11. }
  12. export interface BroadcastOptions {
  13. rooms: Set<Room>;
  14. except?: Set<SocketId>;
  15. flags?: BroadcastFlags;
  16. }
  17. export declare class Adapter extends EventEmitter {
  18. readonly nsp: any;
  19. protected rooms: Map<Room, Set<SocketId>>;
  20. protected sids: Map<SocketId, Set<Room>>;
  21. private readonly encoder;
  22. /**
  23. * In-memory adapter constructor.
  24. *
  25. * @param {Namespace} nsp
  26. */
  27. constructor(nsp: any);
  28. /**
  29. * To be overridden
  30. */
  31. init(): Promise<void> | void;
  32. /**
  33. * To be overridden
  34. */
  35. close(): Promise<void> | void;
  36. /**
  37. * Adds a socket to a list of room.
  38. *
  39. * @param {SocketId} id the socket id
  40. * @param {Set<Room>} rooms a set of rooms
  41. * @public
  42. */
  43. addAll(id: SocketId, rooms: Set<Room>): Promise<void> | void;
  44. /**
  45. * Removes a socket from a room.
  46. *
  47. * @param {SocketId} id the socket id
  48. * @param {Room} room the room name
  49. */
  50. del(id: SocketId, room: Room): Promise<void> | void;
  51. /**
  52. * Removes a socket from all rooms it's joined.
  53. *
  54. * @param {SocketId} id the socket id
  55. */
  56. delAll(id: SocketId): void;
  57. /**
  58. * Broadcasts a packet.
  59. *
  60. * Options:
  61. * - `flags` {Object} flags for this packet
  62. * - `except` {Array} sids that should be excluded
  63. * - `rooms` {Array} list of rooms to broadcast to
  64. *
  65. * @param {Object} packet the packet object
  66. * @param {Object} opts the options
  67. * @public
  68. */
  69. broadcast(packet: any, opts: BroadcastOptions): void;
  70. /**
  71. * Gets a list of sockets by sid.
  72. *
  73. * @param {Set<Room>} rooms the explicit set of rooms to check.
  74. */
  75. sockets(rooms: Set<Room>): Promise<Set<SocketId>>;
  76. /**
  77. * Gets the list of rooms a given socket has joined.
  78. *
  79. * @param {SocketId} id the socket id
  80. */
  81. socketRooms(id: SocketId): Set<Room> | undefined;
  82. }