Node-Red configuration
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

cluster-adapter.d.ts 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. import { Adapter } from "./in-memory-adapter";
  2. import type { BroadcastFlags, BroadcastOptions, Room } from "./in-memory-adapter";
  3. type DistributiveOmit<T, K extends keyof any> = T extends any ? Omit<T, K> : never;
  4. /**
  5. * The unique ID of a server
  6. */
  7. export type ServerId = string;
  8. /**
  9. * The unique ID of a message (for the connection state recovery feature)
  10. */
  11. export type Offset = string;
  12. export interface ClusterAdapterOptions {
  13. /**
  14. * The number of ms between two heartbeats.
  15. * @default 5_000
  16. */
  17. heartbeatInterval?: number;
  18. /**
  19. * The number of ms without heartbeat before we consider a node down.
  20. * @default 10_000
  21. */
  22. heartbeatTimeout?: number;
  23. }
  24. export declare enum MessageType {
  25. INITIAL_HEARTBEAT = 1,
  26. HEARTBEAT = 2,
  27. BROADCAST = 3,
  28. SOCKETS_JOIN = 4,
  29. SOCKETS_LEAVE = 5,
  30. DISCONNECT_SOCKETS = 6,
  31. FETCH_SOCKETS = 7,
  32. FETCH_SOCKETS_RESPONSE = 8,
  33. SERVER_SIDE_EMIT = 9,
  34. SERVER_SIDE_EMIT_RESPONSE = 10,
  35. BROADCAST_CLIENT_COUNT = 11,
  36. BROADCAST_ACK = 12,
  37. ADAPTER_CLOSE = 13
  38. }
  39. export type ClusterMessage = {
  40. uid: ServerId;
  41. nsp: string;
  42. } & ({
  43. type: MessageType.INITIAL_HEARTBEAT | MessageType.HEARTBEAT | MessageType.ADAPTER_CLOSE;
  44. } | {
  45. type: MessageType.BROADCAST;
  46. data: {
  47. opts: {
  48. rooms: string[];
  49. except: string[];
  50. flags: BroadcastFlags;
  51. };
  52. packet: unknown;
  53. requestId?: string;
  54. };
  55. } | {
  56. type: MessageType.SOCKETS_JOIN | MessageType.SOCKETS_LEAVE;
  57. data: {
  58. opts: {
  59. rooms: string[];
  60. except: string[];
  61. flags: BroadcastFlags;
  62. };
  63. rooms: string[];
  64. };
  65. } | {
  66. type: MessageType.DISCONNECT_SOCKETS;
  67. data: {
  68. opts: {
  69. rooms: string[];
  70. except: string[];
  71. flags: BroadcastFlags;
  72. };
  73. close?: boolean;
  74. };
  75. } | {
  76. type: MessageType.FETCH_SOCKETS;
  77. data: {
  78. opts: {
  79. rooms: string[];
  80. except: string[];
  81. flags: BroadcastFlags;
  82. };
  83. requestId: string;
  84. };
  85. } | {
  86. type: MessageType.SERVER_SIDE_EMIT;
  87. data: {
  88. requestId?: string;
  89. packet: any[];
  90. };
  91. });
  92. export type ClusterResponse = {
  93. uid: ServerId;
  94. nsp: string;
  95. } & ({
  96. type: MessageType.FETCH_SOCKETS_RESPONSE;
  97. data: {
  98. requestId: string;
  99. sockets: unknown[];
  100. };
  101. } | {
  102. type: MessageType.SERVER_SIDE_EMIT_RESPONSE;
  103. data: {
  104. requestId: string;
  105. packet: unknown;
  106. };
  107. } | {
  108. type: MessageType.BROADCAST_CLIENT_COUNT;
  109. data: {
  110. requestId: string;
  111. clientCount: number;
  112. };
  113. } | {
  114. type: MessageType.BROADCAST_ACK;
  115. data: {
  116. requestId: string;
  117. packet: unknown;
  118. };
  119. });
  120. /**
  121. * A cluster-ready adapter. Any extending class must:
  122. *
  123. * - implement {@link ClusterAdapter#doPublish} and {@link ClusterAdapter#doPublishResponse}
  124. * - call {@link ClusterAdapter#onMessage} and {@link ClusterAdapter#onResponse}
  125. */
  126. export declare abstract class ClusterAdapter extends Adapter {
  127. protected readonly uid: ServerId;
  128. private requests;
  129. private ackRequests;
  130. protected constructor(nsp: any);
  131. /**
  132. * Called when receiving a message from another member of the cluster.
  133. *
  134. * @param message
  135. * @param offset
  136. * @protected
  137. */
  138. protected onMessage(message: ClusterMessage, offset?: string): void;
  139. /**
  140. * Called when receiving a response from another member of the cluster.
  141. *
  142. * @param response
  143. * @protected
  144. */
  145. protected onResponse(response: ClusterResponse): void;
  146. broadcast(packet: any, opts: BroadcastOptions): Promise<void>;
  147. /**
  148. * Adds an offset at the end of the data array in order to allow the client to receive any missed packets when it
  149. * reconnects after a temporary disconnection.
  150. *
  151. * @param packet
  152. * @param opts
  153. * @param offset
  154. * @private
  155. */
  156. private addOffsetIfNecessary;
  157. broadcastWithAck(packet: any, opts: BroadcastOptions, clientCountCallback: (clientCount: number) => void, ack: (...args: any[]) => void): void;
  158. addSockets(opts: BroadcastOptions, rooms: Room[]): Promise<void>;
  159. delSockets(opts: BroadcastOptions, rooms: Room[]): Promise<void>;
  160. disconnectSockets(opts: BroadcastOptions, close: boolean): Promise<void>;
  161. fetchSockets(opts: BroadcastOptions): Promise<any[]>;
  162. serverSideEmit(packet: any[]): Promise<any>;
  163. protected publish(message: DistributiveOmit<ClusterMessage, "nsp" | "uid">): void;
  164. protected publishAndReturnOffset(message: DistributiveOmit<ClusterMessage, "nsp" | "uid">): Promise<string>;
  165. /**
  166. * Send a message to the other members of the cluster.
  167. *
  168. * @param message
  169. * @protected
  170. * @return an offset, if applicable
  171. */
  172. protected abstract doPublish(message: ClusterMessage): Promise<Offset>;
  173. protected publishResponse(requesterUid: ServerId, response: Omit<ClusterResponse, "nsp" | "uid">): void;
  174. /**
  175. * Send a response to the given member of the cluster.
  176. *
  177. * @param requesterUid
  178. * @param response
  179. * @protected
  180. */
  181. protected abstract doPublishResponse(requesterUid: ServerId, response: ClusterResponse): Promise<void>;
  182. }
  183. export declare abstract class ClusterAdapterWithHeartbeat extends ClusterAdapter {
  184. private readonly _opts;
  185. private heartbeatTimer;
  186. private nodesMap;
  187. private readonly cleanupTimer;
  188. private customRequests;
  189. protected constructor(nsp: any, opts: ClusterAdapterOptions);
  190. init(): void;
  191. private scheduleHeartbeat;
  192. close(): void;
  193. onMessage(message: ClusterMessage, offset?: string): void;
  194. serverCount(): Promise<number>;
  195. publish(message: DistributiveOmit<ClusterMessage, "nsp" | "uid">): void;
  196. serverSideEmit(packet: any[]): Promise<any>;
  197. fetchSockets(opts: BroadcastOptions): Promise<any[]>;
  198. onResponse(response: ClusterResponse): void;
  199. private removeNode;
  200. }
  201. export {};