topsyde-utils 1.0.205 → 1.0.206

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (35) hide show
  1. package/dist/index.d.ts +3 -3
  2. package/dist/index.js +2 -2
  3. package/dist/index.js.map +1 -1
  4. package/dist/server/bun/websocket/Channel.d.ts +25 -3
  5. package/dist/server/bun/websocket/Channel.js +80 -26
  6. package/dist/server/bun/websocket/Channel.js.map +1 -1
  7. package/dist/server/bun/websocket/Client.d.ts +34 -1
  8. package/dist/server/bun/websocket/Client.js +95 -18
  9. package/dist/server/bun/websocket/Client.js.map +1 -1
  10. package/dist/server/bun/websocket/Message.d.ts +6 -10
  11. package/dist/server/bun/websocket/Message.js +31 -32
  12. package/dist/server/bun/websocket/Message.js.map +1 -1
  13. package/dist/server/bun/websocket/Websocket.d.ts +35 -4
  14. package/dist/server/bun/websocket/Websocket.js +71 -12
  15. package/dist/server/bun/websocket/Websocket.js.map +1 -1
  16. package/dist/server/bun/websocket/index.d.ts +1 -1
  17. package/dist/server/bun/websocket/index.js +1 -1
  18. package/dist/server/bun/websocket/index.js.map +1 -1
  19. package/dist/server/bun/websocket/websocket.enums.d.ts +6 -0
  20. package/dist/server/bun/websocket/websocket.enums.js +7 -0
  21. package/dist/server/bun/websocket/websocket.enums.js.map +1 -1
  22. package/dist/server/bun/websocket/websocket.types.d.ts +60 -3
  23. package/dist/server/bun/websocket/websocket.types.js.map +1 -1
  24. package/package.json +1 -1
  25. package/src/__tests__/app.test.ts +1 -1
  26. package/src/__tests__/singleton.test.ts +6 -4
  27. package/src/index.ts +4 -2
  28. package/src/server/bun/websocket/Channel.ts +89 -36
  29. package/src/server/bun/websocket/Client.ts +109 -19
  30. package/src/server/bun/websocket/ISSUES.md +1175 -0
  31. package/src/server/bun/websocket/Message.ts +36 -49
  32. package/src/server/bun/websocket/Websocket.ts +72 -12
  33. package/src/server/bun/websocket/index.ts +1 -1
  34. package/src/server/bun/websocket/websocket.enums.ts +7 -0
  35. package/src/server/bun/websocket/websocket.types.ts +58 -3
@@ -1,12 +1,11 @@
1
1
  import Guards from "../../../utils/Guards.js";
2
- export default class Message {
3
- constructor() {
4
- this.messageTemplate = { type: "", content: {}, channel: "", timestamp: "" };
5
- }
6
- create(message, options) {
7
- // Clone the template (faster than creating new objects)
8
- const output = Object.assign({}, this.messageTemplate);
9
- // Set the dynamic properties in a single pass
2
+ class Message {
3
+ // Private constructor to prevent instantiation
4
+ constructor() { }
5
+ static Create(message, options) {
6
+ // Clone the template
7
+ const output = Object.assign({}, Message.MESSAGE_TEMPLATE);
8
+ // Set the dynamic properties
10
9
  output.type = message.type;
11
10
  output.channel = message.channel || options?.channel || "N/A";
12
11
  // Process message content based on type
@@ -19,7 +18,7 @@ export default class Message {
19
18
  else {
20
19
  output.content = {};
21
20
  }
22
- // Process options in a single pass if provided
21
+ // Process options if provided
23
22
  if (options) {
24
23
  // Add data if provided
25
24
  if (options.data !== undefined) {
@@ -36,14 +35,14 @@ export default class Message {
36
35
  if (options.client && Guards.IsObject(options.client) && Guards.IsString(options.client.id, true)) {
37
36
  output.client = {
38
37
  id: options.client.id,
39
- name: options.client.name,
38
+ name: options.client.name || "Unknown",
40
39
  };
41
- } /* else {
42
- delete output.client;
43
- } */
40
+ }
44
41
  // Include channel metadata if requested
45
- if (options.includeMetadata !== false)
42
+ // Include channel metadata if provided as an object
43
+ if (options.metadata && Guards.IsObject(options.metadata) && !Guards.IsArray(options.metadata)) {
46
44
  output.metadata = options.metadata;
45
+ }
47
46
  // Add timestamp if requested (default: true)
48
47
  if (options.includeTimestamp !== false) {
49
48
  output.timestamp = new Date().toISOString();
@@ -66,8 +65,7 @@ export default class Message {
66
65
  }
67
66
  // Apply custom transformation if provided
68
67
  if (options.transform) {
69
- const transformed = options.transform(output);
70
- return transformed;
68
+ return options.transform(output);
71
69
  }
72
70
  }
73
71
  else {
@@ -75,14 +73,17 @@ export default class Message {
75
73
  }
76
74
  return output;
77
75
  }
78
- createWhisper(message, options) {
79
- return this.create({ ...message, content: message.content, channel: message.channel, type: "whisper" }, options);
76
+ static CreateWhisper(message, options) {
77
+ return Message.Create({ ...message, content: message.content, channel: message.channel, type: "whisper" }, options);
78
+ }
79
+ static Serialize(message, transform) {
80
+ return transform ? transform(message) : JSON.stringify(message);
80
81
  }
81
- send(target, message, options) {
82
- target.send(this.create(message, options));
82
+ static Send(target, message, options) {
83
+ target.send(Message.Create(message, options));
83
84
  }
84
- alert(target, reason, client) {
85
- target.send(this.create({
85
+ static Alert(target, reason, client) {
86
+ target.send(Message.Create({
86
87
  content: {
87
88
  message: reason,
88
89
  },
@@ -90,15 +91,13 @@ export default class Message {
90
91
  type: "message",
91
92
  }, { client }));
92
93
  }
93
- serialize(message, transform) {
94
- return transform ? transform(message) : JSON.stringify(message);
95
- }
96
- static Serialize(message, transform) {
97
- return transform ? transform(message) : JSON.stringify(message);
98
- }
99
- static Create(message, options) {
100
- const msg = new Message();
101
- return msg.create(message, options);
102
- }
103
94
  }
95
+ // Shared template for all messages
96
+ Message.MESSAGE_TEMPLATE = {
97
+ type: "",
98
+ content: {},
99
+ channel: "",
100
+ timestamp: "",
101
+ };
102
+ export default Message;
104
103
  //# sourceMappingURL=Message.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"Message.js","sourceRoot":"","sources":["../../../../src/server/bun/websocket/Message.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,uBAAuB,CAAC;AAU3C,MAAM,CAAC,OAAO,OAAO,OAAO;IAG3B;QACC,IAAI,CAAC,eAAe,GAAG,EAAE,IAAI,EAAE,EAAE,EAAE,OAAO,EAAE,EAAE,EAAE,OAAO,EAAE,EAAE,EAAE,SAAS,EAAE,EAAE,EAAE,CAAC;IAC9E,CAAC;IAEM,MAAM,CAAC,OAAyB,EAAE,OAAiC;QACzE,wDAAwD;QACxD,MAAM,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC,EAAE,EAAE,IAAI,CAAC,eAAe,CAAC,C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","sourcesContent":["import Guards from \"../../../utils/Guards\";\nimport {\n\tWebsocketStructuredMessage,\n\tWebsocketMessage,\n\tWebsocketMessageOptions,\n\tI_WebsocketChannel,\n\tI_WebsocketClient,\n\tWebsocketEntityData,\n} from \"./websocket.types\";\n\nexport default class Message {\n\tprivate messageTemplate: WebsocketStructuredMessage<any>;\n\n\tconstructor() {\n\t\tthis.messageTemplate = { type: \"\", content: {}, channel: \"\", timestamp: \"\" };\n\t}\n\n\tpublic create(message: WebsocketMessage, options?: WebsocketMessageOptions): WebsocketStructuredMessage {\n\t\t// Clone the template (faster than creating new objects)\n\t\tconst output = Object.assign({}, this.messageTemplate);\n\t\t// Set the dynamic properties in a single pass\n\t\toutput.type = message.type;\n\t\toutput.channel = message.channel || options?.channel || \"N/A\";\n\n\t\t// Process message content based on type\n\t\tif (typeof message.content === \"string\") {\n\t\t\toutput.content = { message: message.content };\n\t\t} else if (typeof message.content === \"object\" && message.content !== null) {\n\t\t\toutput.content = { ...message.content };\n\t\t} else {\n\t\t\toutput.content = {};\n\t\t}\n\n\t\t// Process options in a single pass if provided\n\t\tif (options) {\n\t\t\t// Add data if provided\n\t\t\tif (options.data !== undefined) {\n\t\t\t\tif (typeof options.data === \"object\" && options.data !== null && !Array.isArray(options.data)) {\n\t\t\t\t\t// Merge object data with content\n\t\t\t\t\tObject.assign(output.content, options.data);\n\t\t\t\t} else {\n\t\t\t\t\t// Set as data property for other types\n\t\t\t\t\toutput.content.data = options.data;\n\t\t\t\t}\n\t\t\t}\n\t\t\t// Add client information if provided\n\t\t\tif (options.client && Guards.IsObject(options.client) && Guards.IsString(options.client.id, true)) {\n\t\t\t\toutput.client = {\n\t\t\t\t\tid: options.client.id,\n\t\t\t\t\tname: options.client.name,\n\t\t\t\t};\n\t\t\t} /* else {\n\t\t\t\tdelete output.client;\n\t\t\t} */\n\n\t\t\t// Include channel metadata if requested\n\t\t\tif (options.includeMetadata !== false) output.metadata = options.metadata;\n\n\t\t\t// Add timestamp if requested (default: true)\n\t\t\tif (options.includeTimestamp !== false) {\n\t\t\t\toutput.timestamp = new Date().toISOString();\n\t\t\t} else {\n\t\t\t\t// Remove timestamp if explicitly disabled\n\t\t\t\tdelete output.timestamp;\n\t\t\t}\n\n\t\t\t// Add priority if specified\n\t\t\tif (options.priority !== undefined) {\n\t\t\t\toutput.priority = options.priority;\n\t\t\t}\n\n\t\t\t// Add expiration if specified\n\t\t\tif (options.expiresAt !== undefined) {\n\t\t\t\toutput.expiresAt = options.expiresAt;\n\t\t\t}\n\n\t\t\t// Add any custom fields to the root of the message\n\t\t\tif (options.customFields) {\n\t\t\t\tObject.assign(output, options.customFields);\n\t\t\t}\n\n\t\t\t// Apply custom transformation if provided\n\t\t\tif (options.transform) {\n\t\t\t\tconst transformed = options.transform(output);\n\t\t\t\treturn transformed;\n\t\t\t}\n\t\t} else {\n\t\t\toutput.timestamp = new Date().toISOString();\n\t\t}\n\n\t\treturn output;\n\t}\n\n\tpublic createWhisper(message: Omit<WebsocketMessage, \"type\">, options?: WebsocketMessageOptions) {\n\t\treturn this.create({ ...message, content: message.content, channel: message.channel, type: \"whisper\" }, options);\n\t}\n\n\tpublic send(target: I_WebsocketClient, message: WebsocketStructuredMessage): void;\n\tpublic send(target: I_WebsocketClient, message: WebsocketMessage, options?: WebsocketMessageOptions): void;\n\tpublic send(target: I_WebsocketClient, message: WebsocketStructuredMessage | WebsocketMessage, options?: WebsocketMessageOptions): void {\n\t\ttarget.send(this.create(message, options));\n\t}\n\n\tpublic alert(target: I_WebsocketClient, reason: string, client?: WebsocketEntityData) {\n\t\ttarget.send(\n\t\t\tthis.create(\n\t\t\t\t{\n\t\t\t\t\tcontent: {\n\t\t\t\t\t\tmessage: reason,\n\t\t\t\t\t},\n\t\t\t\t\tchannel: \"alert\",\n\t\t\t\t\ttype: \"message\",\n\t\t\t\t},\n\t\t\t\t{ client },\n\t\t\t),\n\t\t);\n\t}\n\n\tpublic serialize<T = string>(message: WebsocketStructuredMessage, transform?: (message: WebsocketStructuredMessage) => T) {\n\t\treturn transform ? transform(message) : JSON.stringify(message);\n\t}\n\n\tpublic static Serialize<T = string>(message: WebsocketStructuredMessage, transform: (message: WebsocketStructuredMessage) => T): T;\n\tpublic static Serialize<T = string>(message: WebsocketStructuredMessage, transform?: (message: WebsocketStructuredMessage) => T): string | T;\n\tpublic static Serialize<T = string>(message: WebsocketStructuredMessage, transform?: (message: WebsocketStructuredMessage) => T): string | T {\n\t\treturn transform ? transform(message) : JSON.stringify(message);\n\t}\n\n\tpublic static Create(message: WebsocketMessage, options?: WebsocketMessageOptions): WebsocketStructuredMessage{\n\t\tconst msg = new Message();\n\t\treturn msg.create(message, options);\n\t}\n}\n"]}
1
+ {"version":3,"file":"Message.js","sourceRoot":"","sources":["../../../../src/server/bun/websocket/Message.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,uBAAuB,CAAC;AAG3C,MAAqB,OAAO;IAS3B,+CAA+C;IAC/C,gBAAuB,CAAC;IAEjB,MAAM,CAAC,MAAM,CAAC,OAAyB,EAAE,OAAiC;QAChF,qBAAqB;QACrB,MAAM,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC,EAAE,EAAE,OAAO,CAAC,gBAAgB,CAAC,CAAC;QAE3D,6BAA6B;QAC7B,MAAM,CAAC,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;QAC3B,MAAM,CAAC,OAAO,GAAG,OAAO,CAAC,OAAO,IAAI,OAAO,EAAE,OAAO,IAAI,KAAK,CAAC;QAE9D,wCAAwC;QACxC,IAAI,OAAO,OAAO,CAAC,OAAO,KAAK,QAAQ,EAAE,CAAC;YACzC,MAAM,CAAC,OAAO,GAAG,EAAE,OAAO,EAAE,OAAO,CAAC,OAAO,EAAE,CAAC;QAC/C,CAAC;aAAM,IAAI,OAAO,OAAO,CAAC,OAAO,KAAK,QAAQ,IAAI,OAAO,CAAC,OAAO,KAAK,IAAI,EAAE,CAAC;YAC5E,MAAM,CAAC,OAAO,GAAG,EAAE,GAAG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C,CAAC,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC;IACjE,CAAC;IAEM,MAAM,CAAC,IAAI,CAAC,MAAyB,EAAE,OAAyB,EAAE,OAAiC;QACzG,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC,CAAC;IAC/C,CAAC;IAEM,MAAM,CAAC,KAAK,CAAC,MAAyB,EAAE,MAAc,EAAE,MAA4B;QAC1F,MAAM,CAAC,IAAI,CACV,OAAO,CAAC,MAAM,CACb;YACC,OAAO,EAAE;gBACR,OAAO,EAAE,MAAM;aACf;YACD,OAAO,EAAE,OAAO;YAChB,IAAI,EAAE,SAAS;SACf,EACD,EAAE,MAAM,EAAE,CACV,CACD,CAAC;IACH,CAAC;;AAlHD,mCAAmC;AACX,wBAAgB,GAAoC;IAC3E,IAAI,EAAE,EAAE;IACR,OAAO,EAAE,EAAE;IACX,OAAO,EAAE,EAAE;IACX,SAAS,EAAE,EAAE;CACb,CAAC;eAPkB,OAAO","sourcesContent":["import Guards from \"../../../utils/Guards\";\nimport { WebsocketStructuredMessage, WebsocketMessage, WebsocketMessageOptions, I_WebsocketClient, WebsocketEntityData } from \"./websocket.types\";\n\nexport default class Message {\n\t// Shared template for all messages\n\tprivate static readonly MESSAGE_TEMPLATE: WebsocketStructuredMessage<any> = {\n\t\ttype: \"\",\n\t\tcontent: {},\n\t\tchannel: \"\",\n\t\ttimestamp: \"\",\n\t};\n\n\t// Private constructor to prevent instantiation\n\tprivate constructor() {}\n\n\tpublic static Create(message: WebsocketMessage, options?: WebsocketMessageOptions): WebsocketStructuredMessage {\n\t\t// Clone the template\n\t\tconst output = Object.assign({}, Message.MESSAGE_TEMPLATE);\n\n\t\t// Set the dynamic properties\n\t\toutput.type = message.type;\n\t\toutput.channel = message.channel || options?.channel || \"N/A\";\n\n\t\t// Process message content based on type\n\t\tif (typeof message.content === \"string\") {\n\t\t\toutput.content = { message: message.content };\n\t\t} else if (typeof message.content === \"object\" && message.content !== null) {\n\t\t\toutput.content = { ...message.content };\n\t\t} else {\n\t\t\toutput.content = {};\n\t\t}\n\n\t\t// Process options if provided\n\t\tif (options) {\n\t\t\t// Add data if provided\n\t\t\tif (options.data !== undefined) {\n\t\t\t\tif (typeof options.data === \"object\" && options.data !== null && !Array.isArray(options.data)) {\n\t\t\t\t\t// Merge object data with content\n\t\t\t\t\tObject.assign(output.content, options.data);\n\t\t\t\t} else {\n\t\t\t\t\t// Set as data property for other types\n\t\t\t\t\toutput.content.data = options.data;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// Add client information if provided\n\t\t\tif (options.client && Guards.IsObject(options.client) && Guards.IsString(options.client.id, true)) {\n\t\t\t\toutput.client = {\n\t\t\t\t\tid: options.client.id,\n\t\t\t\t\tname: options.client.name || \"Unknown\",\n\t\t\t\t};\n\t\t\t}\n\n\t\t\t// Include channel metadata if requested\n\t\t\t// Include channel metadata if provided as an object\n\t\t\tif (options.metadata && Guards.IsObject(options.metadata) && !Guards.IsArray(options.metadata)) {\n\t\t\t\toutput.metadata = options.metadata;\n\t\t\t}\n\n\t\t\t// Add timestamp if requested (default: true)\n\t\t\tif (options.includeTimestamp !== false) {\n\t\t\t\toutput.timestamp = new Date().toISOString();\n\t\t\t} else {\n\t\t\t\t// Remove timestamp if explicitly disabled\n\t\t\t\tdelete output.timestamp;\n\t\t\t}\n\n\t\t\t// Add priority if specified\n\t\t\tif (options.priority !== undefined) {\n\t\t\t\toutput.priority = options.priority;\n\t\t\t}\n\n\t\t\t// Add expiration if specified\n\t\t\tif (options.expiresAt !== undefined) {\n\t\t\t\toutput.expiresAt = options.expiresAt;\n\t\t\t}\n\n\t\t\t// Add any custom fields to the root of the message\n\t\t\tif (options.customFields) {\n\t\t\t\tObject.assign(output, options.customFields);\n\t\t\t}\n\n\t\t\t// Apply custom transformation if provided\n\t\t\tif (options.transform) {\n\t\t\t\treturn options.transform(output);\n\t\t\t}\n\t\t} else {\n\t\t\toutput.timestamp = new Date().toISOString();\n\t\t}\n\n\t\treturn output;\n\t}\n\n\tpublic static CreateWhisper(message: Omit<WebsocketMessage, \"type\">, options?: WebsocketMessageOptions): WebsocketStructuredMessage {\n\t\treturn Message.Create({ ...message, content: message.content, channel: message.channel, type: \"whisper\" }, options);\n\t}\n\n\tpublic static Serialize<T = string>(message: WebsocketStructuredMessage, transform?: (message: WebsocketStructuredMessage) => T): string | T {\n\t\treturn transform ? transform(message) : JSON.stringify(message);\n\t}\n\n\tpublic static Send(target: I_WebsocketClient, message: WebsocketMessage, options?: WebsocketMessageOptions): void {\n\t\ttarget.send(Message.Create(message, options));\n\t}\n\n\tpublic static Alert(target: I_WebsocketClient, reason: string, client?: WebsocketEntityData): void {\n\t\ttarget.send(\n\t\t\tMessage.Create(\n\t\t\t\t{\n\t\t\t\t\tcontent: {\n\t\t\t\t\t\tmessage: reason,\n\t\t\t\t\t},\n\t\t\t\t\tchannel: \"alert\",\n\t\t\t\t\ttype: \"message\",\n\t\t\t\t},\n\t\t\t\t{ client },\n\t\t\t),\n\t\t);\n\t}\n}\n"]}
@@ -13,6 +13,26 @@ export interface I_WebsocketConstructor {
13
13
  channelClass?: typeof Channel;
14
14
  options?: WebsocketConstructorOptions;
15
15
  }
16
+ /**
17
+ * Websocket - Singleton managing clients, channels, and message routing
18
+ *
19
+ * ## API Design: Static vs Instance
20
+ * - **Static methods**: Use in application code (e.g., `Websocket.Broadcast()`, `Websocket.GetClient()`)
21
+ * - **Instance methods**: Use when extending the class (e.g., `protected createClient()`)
22
+ *
23
+ * Static methods are facades that call the singleton instance internally.
24
+ *
25
+ * @example
26
+ * // Application code - use static methods
27
+ * Websocket.Broadcast("lobby", { type: "chat", content: { message: "Hi!" } });
28
+ *
29
+ * // Extension - override instance methods
30
+ * class GameWebsocket extends Websocket {
31
+ * protected createClient(entity: I_WebsocketEntity) {
32
+ * return new GameClient(entity);
33
+ * }
34
+ * }
35
+ */
16
36
  export default class Websocket extends Singleton {
17
37
  protected _channels: WebsocketChannel;
18
38
  protected _clients: Map<string, I_WebsocketClient>;
@@ -77,7 +97,7 @@ export default class Websocket extends Singleton {
77
97
  * @param message - The message
78
98
  * @param args - The arguments
79
99
  */
80
- static BraodcastAll(message: WebsocketStructuredMessage, ...args: any[]): void;
100
+ static BroadCastAll(message: WebsocketStructuredMessage, ...args: any[]): void;
81
101
  /**
82
102
  * Join a channel
83
103
  * @param channel - The channel
@@ -131,8 +151,19 @@ export default class Websocket extends Singleton {
131
151
  */
132
152
  static CreateClient(entity: I_WebsocketEntity): I_WebsocketClient;
133
153
  /**
134
- * Generate a message
135
- * @returns The generated message
154
+ * Get all connected clients (excluding connecting/disconnecting)
155
+ * @returns Array of connected clients
136
156
  */
137
- static GenerateMessage(): WebsocketStructuredMessage;
157
+ static GetConnectedClients(): I_WebsocketClient[];
158
+ /**
159
+ * Get client statistics by state
160
+ * @returns Object with counts by state
161
+ */
162
+ static GetClientStats(): {
163
+ total: number;
164
+ connecting: number;
165
+ connected: number;
166
+ disconnecting: number;
167
+ disconnected: number;
168
+ };
138
169
  }
@@ -4,6 +4,26 @@ import { Console } from "../../../utils/Console.js";
4
4
  import Channel from "./Channel.js";
5
5
  import Client from "./Client.js";
6
6
  import { E_WebsocketMessageType } from "./websocket.enums.js";
7
+ /**
8
+ * Websocket - Singleton managing clients, channels, and message routing
9
+ *
10
+ * ## API Design: Static vs Instance
11
+ * - **Static methods**: Use in application code (e.g., `Websocket.Broadcast()`, `Websocket.GetClient()`)
12
+ * - **Instance methods**: Use when extending the class (e.g., `protected createClient()`)
13
+ *
14
+ * Static methods are facades that call the singleton instance internally.
15
+ *
16
+ * @example
17
+ * // Application code - use static methods
18
+ * Websocket.Broadcast("lobby", { type: "chat", content: { message: "Hi!" } });
19
+ *
20
+ * // Extension - override instance methods
21
+ * class GameWebsocket extends Websocket {
22
+ * protected createClient(entity: I_WebsocketEntity) {
23
+ * return new GameClient(entity);
24
+ * }
25
+ * }
26
+ */
7
27
  export default class Websocket extends Singleton {
8
28
  constructor(options) {
9
29
  super();
@@ -14,7 +34,7 @@ export default class Websocket extends Singleton {
14
34
  if (this._ws_interface_handlers.message)
15
35
  return this._ws_interface_handlers.message(ws, message);
16
36
  ws.send("This is the message from the server: " + message);
17
- Websocket.BraodcastAll({ type: "client.message.received", content: { message } });
37
+ Websocket.BroadCastAll({ type: "client.message.received", content: { message } });
18
38
  };
19
39
  this.clientConnected = (ws) => {
20
40
  if (this._options.debug)
@@ -25,21 +45,32 @@ export default class Websocket extends Singleton {
25
45
  const client = Websocket.CreateClient({ id: ws.data.id, ws: ws, name: ws.data.name });
26
46
  this._clients.set(client.id, client);
27
47
  client.send({ type: E_WebsocketMessageType.CLIENT_CONNECTED, content: { message: "Welcome to the server", client: client.whoami() } });
28
- global.addMember(client);
48
+ // Client handles its own joining logic with rollback support
49
+ if (!client.joinChannel(global)) {
50
+ Lib.Warn(`Failed to add client ${client.id} to global channel`);
51
+ }
52
+ // Mark as fully connected
53
+ client.markConnected();
29
54
  if (this._ws_interface_handlers.open)
30
55
  this._ws_interface_handlers.open(ws);
31
56
  };
32
57
  this.clientDisconnected = (ws, code, reason) => {
33
58
  if (this._options.debug)
34
59
  Lib.Log("Client disconnected", ws.data);
35
- if (this._ws_interface_handlers.close)
36
- this._ws_interface_handlers.close(ws, code, reason);
37
60
  const client = this._clients.get(ws.data.id);
38
61
  if (!client)
39
62
  return;
63
+ // Mark as disconnecting
64
+ client.markDisconnecting();
65
+ if (this._ws_interface_handlers.close)
66
+ this._ws_interface_handlers.close(ws, code, reason);
67
+ // Remove from all channels
40
68
  this._channels.forEach((channel) => {
41
69
  channel.removeMember(client);
42
70
  });
71
+ // Mark as disconnected
72
+ client.markDisconnected();
73
+ // Remove from registry
43
74
  this._clients.delete(ws.data.id);
44
75
  };
45
76
  this.handleHeartbeat = (ws, message) => {
@@ -147,7 +178,7 @@ export default class Websocket extends Singleton {
147
178
  * @param message - The message
148
179
  * @param args - The arguments
149
180
  */
150
- static BraodcastAll(message, ...args) {
181
+ static BroadCastAll(message, ...args) {
151
182
  const ws = this.GetInstance();
152
183
  ws._channels.forEach((channel) => channel.broadcast(message, ...args));
153
184
  }
@@ -233,15 +264,43 @@ export default class Websocket extends Singleton {
233
264
  return ws.createClient(entity);
234
265
  }
235
266
  /**
236
- * Generate a message
237
- * @returns The generated message
267
+ * Get all connected clients (excluding connecting/disconnecting)
268
+ * @returns Array of connected clients
269
+ */
270
+ static GetConnectedClients() {
271
+ const ws = this.GetInstance();
272
+ return Array.from(ws._clients.values()).filter(client => client.state === "connected");
273
+ }
274
+ /**
275
+ * Get client statistics by state
276
+ * @returns Object with counts by state
238
277
  */
239
- static GenerateMessage() {
240
- const msg = {
241
- type: "",
242
- content: {},
278
+ static GetClientStats() {
279
+ const ws = this.GetInstance();
280
+ const stats = {
281
+ total: ws._clients.size,
282
+ connecting: 0,
283
+ connected: 0,
284
+ disconnecting: 0,
285
+ disconnected: 0,
243
286
  };
244
- return msg;
287
+ for (const client of ws._clients.values()) {
288
+ switch (client.state) {
289
+ case "connecting":
290
+ stats.connecting++;
291
+ break;
292
+ case "connected":
293
+ stats.connected++;
294
+ break;
295
+ case "disconnecting":
296
+ stats.disconnecting++;
297
+ break;
298
+ case "disconnected":
299
+ stats.disconnected++;
300
+ break;
301
+ }
302
+ }
303
+ return stats;
245
304
  }
246
305
  }
247
306
  //# sourceMappingURL=Websocket.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"Websocket.js","sourceRoot":"","sources":["../../../../src/server/bun/websocket/Websocket.ts"],"names":[],"mappings":"AACA,OAAO,SAAS,MAAM,oBAAoB,CAAC;AAC3C,OAAO,EAAE,GAAG,EAAE,MAAM,gBAAgB,CAAC;AACrC,OAAO,EAAE,OAAO,EAAE,MAAM,wBAAwB,CAAC;AACjD,OAAO,OAAO,MAAM,WAAW,CAAC;AAChC,OAAO,MAAM,MAAM,UAAU,CAAC;AAW9B,OAAO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,IAAI,CAAC,WAAW,EAAa,CAAC;QACzC,OAAO,EAAE,CAAC,SAAS,CAAC,IAAI,CAAC;IAC1B,CAAC;IAED;;;;OAIG;IACI,MAAM,CAAC,YAAY,CAAC,MAAyB;QACnD,MAAM,EAAE,GAAG,IAAI,CAAC,WAAW,EAAa,CAAC;QACzC,OAAO,EAAE,CAAC,YAAY,CAAC,MAAM,CAAC,CAAC;IAChC,CAAC;IAED;;;OAGG;IACI,MAAM,CAAC,eAAe;QAC5B,MAAM,GAAG,GAA+B;YACvC,IAAI,EAAE,EAAE;YACR,OAAO,EAAE,EAAE;SACX,CAAC;QACF,OAAO,GAAG,CAAC;IACZ,CAAC;CACD","sourcesContent":["import { Server, ServerWebSocket, WebSocketHandler } from \"bun\";\nimport Singleton from \"../../../singleton\";\nimport { Lib } from \"../../../utils\";\nimport { Console } from \"../../../utils/Console\";\nimport Channel from \"./Channel\";\nimport Client from \"./Client\";\nimport type {\n\tI_WebsocketChannel,\n\tI_WebsocketClient,\n\tI_WebsocketEntity,\n\tI_WebsocketInterface,\n\tWebsocketChannel,\n\tWebsocketEntityData,\n\tBunWebsocketMessage,\n\tWebsocketStructuredMessage,\n} from \"./websocket.types\";\nimport { E_WebsocketMessageType } from \"./websocket.enums\";\n\nexport type WebsocketConstructorOptions = {\n\tdebug?: boolean;\n};\n\nexport interface I_WebsocketConstructor {\n\tws_interface?: I_WebsocketInterface;\n\tchannels?: WebsocketChannel;\n\tclientClass?: typeof Client;\n\tchannelClass?: typeof Channel;\n\toptions?: WebsocketConstructorOptions;\n}\n\nexport default class Websocket extends Singleton {\n\tprotected _channels: WebsocketChannel;\n\tprotected _clients: Map<string, I_WebsocketClient> = new Map();\n\tprotected _server!: Server;\n\tprotected _channelClass: typeof Channel;\n\tprotected _clientClass: typeof Client;\n\tprotected _ws_interface?: I_WebsocketInterface;\n\tprotected _options: WebsocketConstructorOptions;\n\tprotected _ws_interface_handlers: Partial<WebSocketHandler<WebsocketEntityData>>;\n\n\tprotected constructor(options?: I_WebsocketConstructor) {\n\t\tsuper();\n\t\tthis._ws_interface = options?.ws_interface;\n\t\tthis._channels = options?.channels ?? new Map<string, Channel>();\n\t\tthis._clientClass = options?.clientClass ?? Client;\n\t\tthis._channelClass = options?.channelClass ?? Channel.GetChannelType(options?.channels);\n\t\tthis._options = options?.options ?? { debug: false };\n\t\tthis.createChannel(\"global\", \"Global\", 1000);\n\t\tthis._ws_interface_handlers = this._ws_interface?.handlers(this._channels, this._clients) ?? {};\n\t}\n\n\tprotected set server(value: Server) {\n\t\tthis._server = value;\n\t}\n\n\tpublic get server(): Server {\n\t\treturn this._server;\n\t}\n\n\tpublic set(server: Server) {\n\t\tthis.server = server;\n\t\tConsole.blank();\n\t\tConsole.success(\"Websocket server set\");\n\t}\n\n\t/**\n\t * Create a new channel\n\t * @param id - The id of the channel\n\t * @param name - The name of the channel\n\t * @param limit - The limit of the channel\n\t * @returns The created channel\n\t */\n\tpublic createChannel(id: string, name: string, limit?: number): I_WebsocketChannel {\n\t\tif (this._channels.has(id)) return this._channels.get(id) as I_WebsocketChannel;\n\t\tconst channel = new this._channelClass(id, name, this, limit);\n\t\tthis._channels.set(id, channel);\n\t\treturn channel;\n\t}\n\n\t/**\n\t * Remove a channel\n\t * @param id - The id of the channel\n\t */\n\tpublic removeChannel(id: string) {\n\t\tthis._channels.delete(id);\n\t}\n\n\t/**\n\t * Create a new channel\n\t * @param id - The id of the channel\n\t * @param name - The name of the channel\n\t * @param limit - The limit of the channel\n\t * @returns The created channel\n\t */\n\tpublic static CreateChannel(id: string, name: string, limit?: number) {\n\t\tconst ws = this.GetInstance<Websocket>();\n\t\treturn ws.createChannel(id, name, limit);\n\t}\n\n\tpublic handlers(): WebSocketHandler<WebsocketEntityData> {\n\t\treturn {\n\t\t\topen: this.clientConnected,\n\t\t\tmessage: this.clientMessageReceived,\n\t\t\tclose: this.clientDisconnected,\n\t\t};\n\t}\n\n\tprivate clientMessageReceived = (ws: ServerWebSocket<WebsocketEntityData>, message: BunWebsocketMessage) => {\n\t\tif (Websocket.Heartbeat(ws, message)) return;\n\n\t\tif (this._ws_interface_handlers.message) return this._ws_interface_handlers.message(ws, message);\n\n\t\tws.send(\"This is the message from the server: \" + message);\n\t\tWebsocket.BraodcastAll({ type: \"client.message.received\", content: { message } });\n\t};\n\n\tprivate clientConnected = (ws: ServerWebSocket<WebsocketEntityData>) => {\n\t\tif (this._options.debug) Lib.Log(\"[debug] Client connected\", ws.data);\n\n\t\tconst global = this._channels.get(\"global\");\n\t\tif (!global) throw new Error(\"Global channel not found\");\n\n\t\tconst client = Websocket.CreateClient({ id: ws.data.id, ws: ws, name: ws.data.name });\n\t\tthis._clients.set(client.id, client);\n\n\t\tclient.send({ type: E_WebsocketMessageType.CLIENT_CONNECTED, content: { message: \"Welcome to the server\", client: client.whoami() } });\n\t\tglobal.addMember(client);\n\n\t\tif (this._ws_interface_handlers.open) this._ws_interface_handlers.open(ws);\n\t};\n\n\tprivate clientDisconnected = (ws: ServerWebSocket<WebsocketEntityData>, code: number, reason: string) => {\n\t\tif (this._options.debug) Lib.Log(\"Client disconnected\", ws.data);\n\n\t\tif (this._ws_interface_handlers.close) this._ws_interface_handlers.close(ws, code, reason);\n\n\t\tconst client = this._clients.get(ws.data.id);\n\t\tif (!client) return;\n\n\t\tthis._channels.forEach((channel) => {\n\t\t\tchannel.removeMember(client);\n\t\t});\n\n\t\tthis._clients.delete(ws.data.id);\n\t};\n\n\tprivate handleHeartbeat = (ws: ServerWebSocket<WebsocketEntityData>, message: BunWebsocketMessage) => {\n\t\tif (message === \"ping\") {\n\t\t\tconst pong: WebsocketStructuredMessage = { type: \"pong\", content: { message: \"pong\" } };\n\t\t\tws.send(JSON.stringify(pong));\n\t\t\treturn true;\n\t\t}\n\t\treturn false;\n\t};\n\n\tprotected createClient(entity: I_WebsocketEntity): I_WebsocketClient {\n\t\treturn new this._clientClass(entity);\n\t}\n\n\t/**\n\t * Handle the heartbeat\n\t * @param ws - The websocket\n\t * @param message - The message\n\t * @returns True if the heartbeat was handled, false otherwise\n\t */\n\tpublic static Heartbeat(ws: ServerWebSocket<WebsocketEntityData>, message: BunWebsocketMessage) {\n\t\tconst self = this.GetInstance<Websocket>();\n\t\treturn self.handleHeartbeat(ws, message);\n\t}\n\n\t/**\n\t * Get the server\n\t * @returns The server\n\t */\n\tpublic static Server() {\n\t\treturn this.GetInstance<Websocket>().server;\n\t}\n\n\t/**\n\t * Broadcast a message to a channel\n\t * @param channel - The channel\n\t * @param message - The message\n\t * @param args - The arguments\n\t */\n\tpublic static Broadcast(channel: string, message: WebsocketStructuredMessage, ...args: any[]) {\n\t\t// Get the server from the singleton instance\n\t\tconst ws = this.GetInstance<Websocket>();\n\t\tif (!ws.server) {\n\t\t\tthrow new Error(\"Websocket server not set\");\n\t\t}\n\t\tws.server.publish(channel, JSON.stringify({ message, args }));\n\t}\n\n\t/**\n\t * Broadcast a message to all channels\n\t * @param message - The message\n\t * @param args - The arguments\n\t */\n\tpublic static BraodcastAll(message: WebsocketStructuredMessage, ...args: any[]) {\n\t\tconst ws = this.GetInstance<Websocket>();\n\t\tws._channels.forEach((channel) => channel.broadcast(message, ...args));\n\t}\n\n\t/**\n\t * Join a channel\n\t * @param channel - The channel\n\t * @param entity - The entity\n\t */\n\tpublic static Join(channel: string, entity: I_WebsocketEntity) {\n\t\tconst ws = this.GetInstance<Websocket>();\n\t\tconst client = ws._clients.get(entity.id);\n\t\tif (!client) return;\n\t\tws._channels.get(channel)?.addMember(client);\n\t}\n\n\t/**\n\t * Leave a channel\n\t * @param channel - The channel\n\t * @param entity - The entity\n\t */\n\tpublic static Leave(channel: string, entity: I_WebsocketEntity) {\n\t\tconst ws = this.GetInstance<Websocket>();\n\t\tconst client = ws._clients.get(entity.id);\n\t\tif (!client) return;\n\t\tws._channels.get(channel)?.removeMember(client);\n\t}\n\n\t/**\n\t * Get a client\n\t * @param id - The id of the client\n\t * @param throw_if_nil - Whether to throw an error if the client is not found\n\t * @returns The client\n\t */\n\tpublic static GetClient(id: string, throw_if_nil?: true): I_WebsocketClient;\n\tpublic static GetClient(id: string, throw_if_nil?: false): I_WebsocketClient | undefined;\n\tpublic static GetClient(id: string, throw_if_nil: boolean = true): I_WebsocketClient | undefined {\n\t\tconst ws = this.GetInstance<Websocket>();\n\t\tconst client = ws._clients.get(id);\n\t\tif (!client && throw_if_nil) throw new Error(`Client with id ${id} not found`);\n\t\treturn client;\n\t}\n\n\t/**\n\t * Get a channel\n\t * @param id - The id of the channel\n\t * @returns The channel\n\t */\n\tpublic static GetChannel(id: string) {\n\t\tconst ws = this.GetInstance<Websocket>();\n\t\treturn ws._channels.get(id);\n\t}\n\n\t/**\n\t * Get all channels\n\t * @returns The channels\n\t */\n\tpublic static GetChannels() {\n\t\tconst ws = this.GetInstance<Websocket>();\n\t\treturn Array.from(ws._channels.values());\n\t}\n\n\t/**\n\t * Get all clients\n\t * @returns The clients\n\t */\n\tpublic static GetClients() {\n\t\tconst ws = this.GetInstance<Websocket>();\n\t\treturn Array.from(ws._clients.values());\n\t}\n\n\t/**\n\t * Get the number of clients\n\t * @returns The number of clients\n\t */\n\tpublic static GetClientCount() {\n\t\tconst ws = this.GetInstance<Websocket>();\n\t\treturn ws._clients.size;\n\t}\n\n\t/**\n\t * Get the number of channels\n\t * @returns The number of channels\n\t */\n\tpublic static GetChannelCount() {\n\t\tconst ws = this.GetInstance<Websocket>();\n\t\treturn ws._channels.size;\n\t}\n\n\t/**\n\t * Create a client\n\t * @param entity - The entity\n\t * @returns The created client\n\t */\n\tpublic static CreateClient(entity: I_WebsocketEntity): I_WebsocketClient {\n\t\tconst ws = this.GetInstance<Websocket>();\n\t\treturn ws.createClient(entity);\n\t}\n\n\t/**\n\t * Generate a message\n\t * @returns The generated message\n\t */\n\tpublic static GenerateMessage(): WebsocketStructuredMessage {\n\t\tconst msg: WebsocketStructuredMessage = {\n\t\t\ttype: \"\",\n\t\t\tcontent: {},\n\t\t};\n\t\treturn msg;\n\t}\n}\n"]}
1
+ {"version":3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AG,IAAI,CAAC,WAAW,EAAa,CAAC;QACzC,MAAM,KAAK,GAAG;YACb,KAAK,EAAE,EAAE,CAAC,QAAQ,CAAC,IAAI;YACvB,UAAU,EAAE,CAAC;YACb,SAAS,EAAE,CAAC;YACZ,aAAa,EAAE,CAAC;YAChB,YAAY,EAAE,CAAC;SACf,CAAC;QAEF,KAAK,MAAM,MAAM,IAAI,EAAE,CAAC,QAAQ,CAAC,MAAM,EAAE,EAAE,CAAC;YAC3C,QAAQ,MAAM,CAAC,KAAK,EAAE,CAAC;gBACtB,KAAK,YAAY;oBAAE,KAAK,CAAC,UAAU,EAAE,CAAC;oBAAC,MAAM;gBAC7C,KAAK,WAAW;oBAAE,KAAK,CAAC,SAAS,EAAE,CAAC;oBAAC,MAAM;gBAC3C,KAAK,eAAe;oBAAE,KAAK,CAAC,aAAa,EAAE,CAAC;oBAAC,MAAM;gBACnD,KAAK,cAAc;oBAAE,KAAK,CAAC,YAAY,EAAE,CAAC;oBAAC,MAAM;YAClD,CAAC;QACF,CAAC;QAED,OAAO,KAAK,CAAC;IACd,CAAC;CACD","sourcesContent":["import { Server, ServerWebSocket, WebSocketHandler } from \"bun\";\nimport Singleton from \"../../../singleton\";\nimport { Lib } from \"../../../utils\";\nimport { Console } from \"../../../utils/Console\";\nimport Channel from \"./Channel\";\nimport Client from \"./Client\";\nimport type {\n\tI_WebsocketChannel,\n\tI_WebsocketClient,\n\tI_WebsocketEntity,\n\tI_WebsocketInterface,\n\tWebsocketChannel,\n\tWebsocketEntityData,\n\tBunWebsocketMessage,\n\tWebsocketStructuredMessage,\n} from \"./websocket.types\";\nimport { E_WebsocketMessageType } from \"./websocket.enums\";\n\nexport type WebsocketConstructorOptions = {\n\tdebug?: boolean;\n};\n\nexport interface I_WebsocketConstructor {\n\tws_interface?: I_WebsocketInterface;\n\tchannels?: WebsocketChannel;\n\tclientClass?: typeof Client;\n\tchannelClass?: typeof Channel;\n\toptions?: WebsocketConstructorOptions;\n}\n\n/**\n * Websocket - Singleton managing clients, channels, and message routing\n *\n * ## API Design: Static vs Instance\n * - **Static methods**: Use in application code (e.g., `Websocket.Broadcast()`, `Websocket.GetClient()`)\n * - **Instance methods**: Use when extending the class (e.g., `protected createClient()`)\n *\n * Static methods are facades that call the singleton instance internally.\n *\n * @example\n * // Application code - use static methods\n * Websocket.Broadcast(\"lobby\", { type: \"chat\", content: { message: \"Hi!\" } });\n *\n * // Extension - override instance methods\n * class GameWebsocket extends Websocket {\n * protected createClient(entity: I_WebsocketEntity) {\n * return new GameClient(entity);\n * }\n * }\n */\nexport default class Websocket extends Singleton {\n\tprotected _channels: WebsocketChannel;\n\tprotected _clients: Map<string, I_WebsocketClient> = new Map();\n\tprotected _server!: Server;\n\tprotected _channelClass: typeof Channel;\n\tprotected _clientClass: typeof Client;\n\tprotected _ws_interface?: I_WebsocketInterface;\n\tprotected _options: WebsocketConstructorOptions;\n\tprotected _ws_interface_handlers: Partial<WebSocketHandler<WebsocketEntityData>>;\n\n\tprotected constructor(options?: I_WebsocketConstructor) {\n\t\tsuper();\n\t\tthis._ws_interface = options?.ws_interface;\n\t\tthis._channels = options?.channels ?? new Map<string, Channel>();\n\t\tthis._clientClass = options?.clientClass ?? Client;\n\t\tthis._channelClass = options?.channelClass ?? Channel.GetChannelType(options?.channels);\n\t\tthis._options = options?.options ?? { debug: false };\n\t\tthis.createChannel(\"global\", \"Global\", 1000);\n\t\tthis._ws_interface_handlers = this._ws_interface?.handlers(this._channels, this._clients) ?? {};\n\t}\n\n\tprotected set server(value: Server) {\n\t\tthis._server = value;\n\t}\n\n\tpublic get server(): Server {\n\t\treturn this._server;\n\t}\n\n\tpublic set(server: Server) {\n\t\tthis.server = server;\n\t\tConsole.blank();\n\t\tConsole.success(\"Websocket server set\");\n\t}\n\n\t/**\n\t * Create a new channel\n\t * @param id - The id of the channel\n\t * @param name - The name of the channel\n\t * @param limit - The limit of the channel\n\t * @returns The created channel\n\t */\n\tpublic createChannel(id: string, name: string, limit?: number): I_WebsocketChannel {\n\t\tif (this._channels.has(id)) return this._channels.get(id) as I_WebsocketChannel;\n\t\tconst channel = new this._channelClass(id, name, this, limit);\n\t\tthis._channels.set(id, channel);\n\t\treturn channel;\n\t}\n\n\t/**\n\t * Remove a channel\n\t * @param id - The id of the channel\n\t */\n\tpublic removeChannel(id: string) {\n\t\tthis._channels.delete(id);\n\t}\n\n\t/**\n\t * Create a new channel\n\t * @param id - The id of the channel\n\t * @param name - The name of the channel\n\t * @param limit - The limit of the channel\n\t * @returns The created channel\n\t */\n\tpublic static CreateChannel(id: string, name: string, limit?: number) {\n\t\tconst ws = this.GetInstance<Websocket>();\n\t\treturn ws.createChannel(id, name, limit);\n\t}\n\n\tpublic handlers(): WebSocketHandler<WebsocketEntityData> {\n\t\treturn {\n\t\t\topen: this.clientConnected,\n\t\t\tmessage: this.clientMessageReceived,\n\t\t\tclose: this.clientDisconnected,\n\t\t};\n\t}\n\n\tprivate clientMessageReceived = (ws: ServerWebSocket<WebsocketEntityData>, message: BunWebsocketMessage) => {\n\t\tif (Websocket.Heartbeat(ws, message)) return;\n\n\t\tif (this._ws_interface_handlers.message) return this._ws_interface_handlers.message(ws, message);\n\n\t\tws.send(\"This is the message from the server: \" + message);\n\t\tWebsocket.BroadCastAll({ type: \"client.message.received\", content: { message } });\n\t};\n\n\tprivate clientConnected = (ws: ServerWebSocket<WebsocketEntityData>) => {\n\t\tif (this._options.debug) Lib.Log(\"[debug] Client connected\", ws.data);\n\n\t\tconst global = this._channels.get(\"global\");\n\t\tif (!global) throw new Error(\"Global channel not found\");\n\n\t\tconst client = Websocket.CreateClient({ id: ws.data.id, ws: ws, name: ws.data.name });\n\t\tthis._clients.set(client.id, client);\n\n\t\tclient.send({ type: E_WebsocketMessageType.CLIENT_CONNECTED, content: { message: \"Welcome to the server\", client: client.whoami() } });\n\n\t\t// Client handles its own joining logic with rollback support\n\t\tif (!client.joinChannel(global)) {\n\t\t\tLib.Warn(`Failed to add client ${client.id} to global channel`);\n\t\t}\n\n\t\t// Mark as fully connected\n\t\tclient.markConnected();\n\n\t\tif (this._ws_interface_handlers.open) this._ws_interface_handlers.open(ws);\n\t};\n\n\tprivate clientDisconnected = (ws: ServerWebSocket<WebsocketEntityData>, code: number, reason: string) => {\n\t\tif (this._options.debug) Lib.Log(\"Client disconnected\", ws.data);\n\n\t\tconst client = this._clients.get(ws.data.id);\n\t\tif (!client) return;\n\n\t\t// Mark as disconnecting\n\t\tclient.markDisconnecting();\n\n\t\tif (this._ws_interface_handlers.close) this._ws_interface_handlers.close(ws, code, reason);\n\n\t\t// Remove from all channels\n\t\tthis._channels.forEach((channel) => {\n\t\t\tchannel.removeMember(client);\n\t\t});\n\n\t\t// Mark as disconnected\n\t\tclient.markDisconnected();\n\n\t\t// Remove from registry\n\t\tthis._clients.delete(ws.data.id);\n\t};\n\n\tprivate handleHeartbeat = (ws: ServerWebSocket<WebsocketEntityData>, message: BunWebsocketMessage) => {\n\t\tif (message === \"ping\") {\n\t\t\tconst pong: WebsocketStructuredMessage = { type: \"pong\", content: { message: \"pong\" } };\n\t\t\tws.send(JSON.stringify(pong));\n\t\t\treturn true;\n\t\t}\n\t\treturn false;\n\t};\n\n\tprotected createClient(entity: I_WebsocketEntity): I_WebsocketClient {\n\t\treturn new this._clientClass(entity);\n\t}\n\n\t/**\n\t * Handle the heartbeat\n\t * @param ws - The websocket\n\t * @param message - The message\n\t * @returns True if the heartbeat was handled, false otherwise\n\t */\n\tpublic static Heartbeat(ws: ServerWebSocket<WebsocketEntityData>, message: BunWebsocketMessage) {\n\t\tconst self = this.GetInstance<Websocket>();\n\t\treturn self.handleHeartbeat(ws, message);\n\t}\n\n\t/**\n\t * Get the server\n\t * @returns The server\n\t */\n\tpublic static Server() {\n\t\treturn this.GetInstance<Websocket>().server;\n\t}\n\n\t/**\n\t * Broadcast a message to a channel\n\t * @param channel - The channel\n\t * @param message - The message\n\t * @param args - The arguments\n\t */\n\tpublic static Broadcast(channel: string, message: WebsocketStructuredMessage, ...args: any[]) {\n\t\t// Get the server from the singleton instance\n\t\tconst ws = this.GetInstance<Websocket>();\n\t\tif (!ws.server) {\n\t\t\tthrow new Error(\"Websocket server not set\");\n\t\t}\n\t\tws.server.publish(channel, JSON.stringify({ message, args }));\n\t}\n\n\t/**\n\t * Broadcast a message to all channels\n\t * @param message - The message\n\t * @param args - The arguments\n\t */\n\tpublic static BroadCastAll(message: WebsocketStructuredMessage, ...args: any[]) {\n\t\tconst ws = this.GetInstance<Websocket>();\n\t\tws._channels.forEach((channel) => channel.broadcast(message, ...args));\n\t}\n\n\t/**\n\t * Join a channel\n\t * @param channel - The channel\n\t * @param entity - The entity\n\t */\n\tpublic static Join(channel: string, entity: I_WebsocketEntity) {\n\t\tconst ws = this.GetInstance<Websocket>();\n\t\tconst client = ws._clients.get(entity.id);\n\t\tif (!client) return;\n\t\tws._channels.get(channel)?.addMember(client);\n\t}\n\n\t/**\n\t * Leave a channel\n\t * @param channel - The channel\n\t * @param entity - The entity\n\t */\n\tpublic static Leave(channel: string, entity: I_WebsocketEntity) {\n\t\tconst ws = this.GetInstance<Websocket>();\n\t\tconst client = ws._clients.get(entity.id);\n\t\tif (!client) return;\n\t\tws._channels.get(channel)?.removeMember(client);\n\t}\n\n\t/**\n\t * Get a client\n\t * @param id - The id of the client\n\t * @param throw_if_nil - Whether to throw an error if the client is not found\n\t * @returns The client\n\t */\n\tpublic static GetClient(id: string, throw_if_nil?: true): I_WebsocketClient;\n\tpublic static GetClient(id: string, throw_if_nil?: false): I_WebsocketClient | undefined;\n\tpublic static GetClient(id: string, throw_if_nil: boolean = true): I_WebsocketClient | undefined {\n\t\tconst ws = this.GetInstance<Websocket>();\n\t\tconst client = ws._clients.get(id);\n\t\tif (!client && throw_if_nil) throw new Error(`Client with id ${id} not found`);\n\t\treturn client;\n\t}\n\n\t/**\n\t * Get a channel\n\t * @param id - The id of the channel\n\t * @returns The channel\n\t */\n\tpublic static GetChannel(id: string) {\n\t\tconst ws = this.GetInstance<Websocket>();\n\t\treturn ws._channels.get(id);\n\t}\n\n\t/**\n\t * Get all channels\n\t * @returns The channels\n\t */\n\tpublic static GetChannels() {\n\t\tconst ws = this.GetInstance<Websocket>();\n\t\treturn Array.from(ws._channels.values());\n\t}\n\n\t/**\n\t * Get all clients\n\t * @returns The clients\n\t */\n\tpublic static GetClients() {\n\t\tconst ws = this.GetInstance<Websocket>();\n\t\treturn Array.from(ws._clients.values());\n\t}\n\n\t/**\n\t * Get the number of clients\n\t * @returns The number of clients\n\t */\n\tpublic static GetClientCount() {\n\t\tconst ws = this.GetInstance<Websocket>();\n\t\treturn ws._clients.size;\n\t}\n\n\t/**\n\t * Get the number of channels\n\t * @returns The number of channels\n\t */\n\tpublic static GetChannelCount() {\n\t\tconst ws = this.GetInstance<Websocket>();\n\t\treturn ws._channels.size;\n\t}\n\n\t/**\n\t * Create a client\n\t * @param entity - The entity\n\t * @returns The created client\n\t */\n\tpublic static CreateClient(entity: I_WebsocketEntity): I_WebsocketClient {\n\t\tconst ws = this.GetInstance<Websocket>();\n\t\treturn ws.createClient(entity);\n\t}\n\n\t/**\n\t * Get all connected clients (excluding connecting/disconnecting)\n\t * @returns Array of connected clients\n\t */\n\tpublic static GetConnectedClients(): I_WebsocketClient[] {\n\t\tconst ws = this.GetInstance<Websocket>();\n\t\treturn Array.from(ws._clients.values()).filter(\n\t\t\tclient => client.state === \"connected\"\n\t\t);\n\t}\n\n\t/**\n\t * Get client statistics by state\n\t * @returns Object with counts by state\n\t */\n\tpublic static GetClientStats() {\n\t\tconst ws = this.GetInstance<Websocket>();\n\t\tconst stats = {\n\t\t\ttotal: ws._clients.size,\n\t\t\tconnecting: 0,\n\t\t\tconnected: 0,\n\t\t\tdisconnecting: 0,\n\t\t\tdisconnected: 0,\n\t\t};\n\n\t\tfor (const client of ws._clients.values()) {\n\t\t\tswitch (client.state) {\n\t\t\t\tcase \"connecting\": stats.connecting++; break;\n\t\t\t\tcase \"connected\": stats.connected++; break;\n\t\t\t\tcase \"disconnecting\": stats.disconnecting++; break;\n\t\t\t\tcase \"disconnected\": stats.disconnected++; break;\n\t\t\t}\n\t\t}\n\n\t\treturn stats;\n\t}\n}\n"]}
@@ -5,7 +5,7 @@ export * from './Channel';
5
5
  export * from './Client';
6
6
  export * from './websocket.enums';
7
7
  export * from './websocket.types';
8
- export { default as Websocket } from './Websocket';
8
+ export { default as GameWebsocket } from './Websocket';
9
9
  export { default as Message } from './Message';
10
10
  export { default as Channel } from './Channel';
11
11
  export { default as Client } from './Client';
@@ -7,7 +7,7 @@ export * from './Channel.js';
7
7
  export * from './Client.js';
8
8
  export * from './websocket.enums.js';
9
9
  export * from './websocket.types.js';
10
- export { default as Websocket } from './Websocket.js';
10
+ export { default as GameWebsocket } from './Websocket.js';
11
11
  export { default as Message } from './Message.js';
12
12
  export { default as Channel } from './Channel.js';
13
13
  export { default as Client } from './Client.js';
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/server/bun/websocket/index.ts"],"names":[],"mappings":"AAAA,6DAA6D;AAC7D,iCAAiC;AAEjC,cAAc,aAAa,CAAC;AAC5B,cAAc,oBAAoB,CAAC;AACnC,cAAc,WAAW,CAAC;AAC1B,cAAc,WAAW,CAAC;AAC1B,cAAc,UAAU,CAAC;AACzB,cAAc,mBAAmB,CAAC;AAClC,cAAc,mBAAmB,CAAC;AAClC,OAAO,EAAE,OAAO,IAAI,SAAS,EAAE,MAAM,aAAa,CAAC;AACnD,OAAO,EAAE,OAAO,IAAI,OAAO,EAAE,MAAM,WAAW,CAAC;AAC/C,OAAO,EAAE,OAAO,IAAI,OAAO,EAAE,MAAM,WAAW,CAAC;AAC/C,OAAO,EAAE,OAAO,IAAI,MAAM,EAAE,MAAM,UAAU,CAAC","sourcesContent":["// This file is auto-generated by scripts/generate-indexes.ts\n// Do not edit this file directly\n\nexport * from './Websocket';\nexport * from './websocket.guards';\nexport * from './Message';\nexport * from './Channel';\nexport * from './Client';\nexport * from './websocket.enums';\nexport * from './websocket.types';\nexport { default as Websocket } from './Websocket';\nexport { default as Message } from './Message';\nexport { default as Channel } from './Channel';\nexport { default as Client } from './Client';\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/server/bun/websocket/index.ts"],"names":[],"mappings":"AAAA,6DAA6D;AAC7D,iCAAiC;AAEjC,cAAc,aAAa,CAAC;AAC5B,cAAc,oBAAoB,CAAC;AACnC,cAAc,WAAW,CAAC;AAC1B,cAAc,WAAW,CAAC;AAC1B,cAAc,UAAU,CAAC;AACzB,cAAc,mBAAmB,CAAC;AAClC,cAAc,mBAAmB,CAAC;AAClC,OAAO,EAAE,OAAO,IAAI,aAAa,EAAE,MAAM,aAAa,CAAC;AACvD,OAAO,EAAE,OAAO,IAAI,OAAO,EAAE,MAAM,WAAW,CAAC;AAC/C,OAAO,EAAE,OAAO,IAAI,OAAO,EAAE,MAAM,WAAW,CAAC;AAC/C,OAAO,EAAE,OAAO,IAAI,MAAM,EAAE,MAAM,UAAU,CAAC","sourcesContent":["// This file is auto-generated by scripts/generate-indexes.ts\n// Do not edit this file directly\n\nexport * from './Websocket';\nexport * from './websocket.guards';\nexport * from './Message';\nexport * from './Channel';\nexport * from './Client';\nexport * from './websocket.enums';\nexport * from './websocket.types';\nexport { default as GameWebsocket } from './Websocket';\nexport { default as Message } from './Message';\nexport { default as Channel } from './Channel';\nexport { default as Client } from './Client';\n"]}
@@ -19,3 +19,9 @@ export declare enum E_WebsocketMessagePriority {
19
19
  MEDIUM = 1,
20
20
  HIGH = 2
21
21
  }
22
+ export declare enum E_ClientState {
23
+ CONNECTING = "connecting",
24
+ CONNECTED = "connected",
25
+ DISCONNECTING = "disconnecting",
26
+ DISCONNECTED = "disconnected"
27
+ }
@@ -21,4 +21,11 @@ export var E_WebsocketMessagePriority;
21
21
  E_WebsocketMessagePriority[E_WebsocketMessagePriority["MEDIUM"] = 1] = "MEDIUM";
22
22
  E_WebsocketMessagePriority[E_WebsocketMessagePriority["HIGH"] = 2] = "HIGH";
23
23
  })(E_WebsocketMessagePriority || (E_WebsocketMessagePriority = {}));
24
+ export var E_ClientState;
25
+ (function (E_ClientState) {
26
+ E_ClientState["CONNECTING"] = "connecting";
27
+ E_ClientState["CONNECTED"] = "connected";
28
+ E_ClientState["DISCONNECTING"] = "disconnecting";
29
+ E_ClientState["DISCONNECTED"] = "disconnected";
30
+ })(E_ClientState || (E_ClientState = {}));
24
31
  //# sourceMappingURL=websocket.enums.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"websocket.enums.js","sourceRoot":"","sources":["../../../../src/server/bun/websocket/websocket.enums.ts"],"names":[],"mappings":"AAAA,MAAM,CAAN,IAAY,sBAeX;AAfD,WAAY,sBAAsB;IACjC,+DAAqC,CAAA;IACrC,qEAA2C,CAAA;IAC3C,qEAA2C,CAAA;IAC3C,uEAA6C,CAAA;IAC7C,uEAA6C,CAAA;IAC7C,yEAA+C,CAAA;IAC/C,uCAAa,CAAA;IACb,uCAAa,CAAA;IACb,6CAAmB,CAAA;IACnB,6CAAmB,CAAA;IACnB,iDAAuB,CAAA;IACvB,2CAAiB,CAAA;IACjB,yCAAe,CAAA;IACf,2CAAiB,CAAA;AAClB,CAAC,EAfW,sBAAsB,KAAtB,sBAAsB,QAejC;AAED,MAAM,CAAN,IAAY,0BAIX;AAJD,WAAY,0BAA0B;IACrC,yEAAO,CAAA;IACP,+EAAU,CAAA;IACV,2EAAQ,CAAA;AACT,CAAC,EAJW,0BAA0B,KAA1B,0BAA0B,QAIrC","sourcesContent":["export enum E_WebsocketMessageType {\n\tCLIENT_CONNECTED = \"client.connected\",\n\tCLIENT_DISCONNECTED = \"client.disconnected\",\n\tCLIENT_JOIN_CHANNEL = \"client.join.channel\",\n\tCLIENT_LEAVE_CHANNEL = \"client.leave.channel\",\n\tCLIENT_JOIN_CHANNELS = \"client.join.channels\",\n\tCLIENT_LEAVE_CHANNELS = \"client.leave.channels\",\n\tPING = \"ping\",\n\tPONG = \"pong\",\n\tMESSAGE = \"message\",\n\tWHISPER = \"whisper\",\n\tBROADCAST = \"broadcast\",\n\tPROMPT = \"prompt\",\n\tERROR = \"error\",\n\tSYSTEM = \"system\",\n}\n\nexport enum E_WebsocketMessagePriority {\n\tLOW = 0,\n\tMEDIUM = 1,\n\tHIGH = 2,\n}\n"]}
1
+ {"version":3,"file":"websocket.enums.js","sourceRoot":"","sources":["../../../../src/server/bun/websocket/websocket.enums.ts"],"names":[],"mappings":"AAAA,MAAM,CAAN,IAAY,sBAeX;AAfD,WAAY,sBAAsB;IACjC,+DAAqC,CAAA;IACrC,qEAA2C,CAAA;IAC3C,qEAA2C,CAAA;IAC3C,uEAA6C,CAAA;IAC7C,uEAA6C,CAAA;IAC7C,yEAA+C,CAAA;IAC/C,uCAAa,CAAA;IACb,uCAAa,CAAA;IACb,6CAAmB,CAAA;IACnB,6CAAmB,CAAA;IACnB,iDAAuB,CAAA;IACvB,2CAAiB,CAAA;IACjB,yCAAe,CAAA;IACf,2CAAiB,CAAA;AAClB,CAAC,EAfW,sBAAsB,KAAtB,sBAAsB,QAejC;AAED,MAAM,CAAN,IAAY,0BAIX;AAJD,WAAY,0BAA0B;IACrC,yEAAO,CAAA;IACP,+EAAU,CAAA;IACV,2EAAQ,CAAA;AACT,CAAC,EAJW,0BAA0B,KAA1B,0BAA0B,QAIrC;AAED,MAAM,CAAN,IAAY,aAKX;AALD,WAAY,aAAa;IACxB,0CAAyB,CAAA;IACzB,wCAAuB,CAAA;IACvB,gDAA+B,CAAA;IAC/B,8CAA6B,CAAA;AAC9B,CAAC,EALW,aAAa,KAAb,aAAa,QAKxB","sourcesContent":["export enum E_WebsocketMessageType {\n\tCLIENT_CONNECTED = \"client.connected\",\n\tCLIENT_DISCONNECTED = \"client.disconnected\",\n\tCLIENT_JOIN_CHANNEL = \"client.join.channel\",\n\tCLIENT_LEAVE_CHANNEL = \"client.leave.channel\",\n\tCLIENT_JOIN_CHANNELS = \"client.join.channels\",\n\tCLIENT_LEAVE_CHANNELS = \"client.leave.channels\",\n\tPING = \"ping\",\n\tPONG = \"pong\",\n\tMESSAGE = \"message\",\n\tWHISPER = \"whisper\",\n\tBROADCAST = \"broadcast\",\n\tPROMPT = \"prompt\",\n\tERROR = \"error\",\n\tSYSTEM = \"system\",\n}\n\nexport enum E_WebsocketMessagePriority {\n\tLOW = 0,\n\tMEDIUM = 1,\n\tHIGH = 2,\n}\n\nexport enum E_ClientState {\n\tCONNECTING = \"connecting\",\n\tCONNECTED = \"connected\",\n\tDISCONNECTING = \"disconnecting\",\n\tDISCONNECTED = \"disconnected\",\n}\n"]}
@@ -1,6 +1,7 @@
1
1
  import { ServerWebSocket, WebSocketHandler } from "bun";
2
2
  import Channel from "./Channel";
3
3
  import Websocket from "./Websocket";
4
+ import { E_ClientState } from "./websocket.enums";
4
5
  export type BunWebsocketMessage = string | Buffer<ArrayBufferLike>;
5
6
  export type WebsocketChannel<T extends I_WebsocketChannel = Channel> = Map<string, T>;
6
7
  export type WebsocketClients = Map<string, I_WebsocketClient>;
@@ -88,7 +89,36 @@ export type WebsocketMessage<T extends Record<string, any> = Record<string, any>
88
89
  */
89
90
  [key: string]: any;
90
91
  };
91
- export type WebsocketStructuredMessage<T extends Record<string, any> = Record<string, any>> = WebsocketMessage<T> & WebsocketMessageOptions;
92
+ /**
93
+ * Message structure sent over the wire to clients.
94
+ * This is the actual WebSocket payload format - transport options are NOT included.
95
+ */
96
+ export type WebsocketStructuredMessage<T extends Record<string, any> = Record<string, any>> = {
97
+ /** Message type identifier for client-side routing */
98
+ type: string;
99
+ /** Message payload */
100
+ content: T;
101
+ /** Channel ID where message originated */
102
+ channel?: string;
103
+ /** ISO timestamp when message was created */
104
+ timestamp?: string;
105
+ /** Client information (who sent this) */
106
+ client?: WebsocketEntityData;
107
+ /** Channel metadata (if included) */
108
+ metadata?: Record<string, string>;
109
+ /** Message priority for client-side processing */
110
+ priority?: number;
111
+ /** Expiration timestamp (milliseconds since epoch) */
112
+ expiresAt?: number;
113
+ /** Any additional custom fields */
114
+ [key: string]: any;
115
+ };
116
+ /**
117
+ * @deprecated This type incorrectly mixed transport options with wire format.
118
+ * Use WebsocketStructuredMessage for wire format and WebsocketMessageOptions for options.
119
+ * This will be removed in a future version.
120
+ */
121
+ export type deprecated_WebsocketStructuredMessage<T extends Record<string, any> = Record<string, any>> = WebsocketMessage<T> & WebsocketMessageOptions;
92
122
  export type WebsocketEntityId = string;
93
123
  export type WebsocketEntityName = string;
94
124
  export type WebsocketEntityData = {
@@ -100,15 +130,29 @@ export interface I_WebsocketEntity extends WebsocketEntityData {
100
130
  }
101
131
  export interface I_WebsocketClient extends I_WebsocketEntity {
102
132
  channels: WebsocketChannel<I_WebsocketChannel>;
133
+ state: E_ClientState;
103
134
  send(message: string, options?: WebsocketMessageOptions): void;
104
135
  send(message: WebsocketStructuredMessage): void;
105
136
  subscribe(channel: string): any;
106
- joinChannel(channel: I_WebsocketChannel, send?: boolean): void;
137
+ joinChannel(channel: I_WebsocketChannel, send?: boolean): boolean;
107
138
  leaveChannel(channel: I_WebsocketChannel, send?: boolean): void;
108
139
  joinChannels(channels: I_WebsocketChannel[], send?: boolean): void;
109
140
  leaveChannels(channels?: I_WebsocketChannel[], send?: boolean): void;
110
141
  unsubscribe(channel: string): any;
111
142
  whoami(): WebsocketEntityData;
143
+ canReceiveMessages(): boolean;
144
+ markConnected(): void;
145
+ markDisconnecting(): void;
146
+ markDisconnected(): void;
147
+ getConnectionInfo(): {
148
+ id: string;
149
+ name: string;
150
+ state: E_ClientState;
151
+ connectedAt?: Date;
152
+ disconnectedAt?: Date;
153
+ uptime: number;
154
+ channelCount: number;
155
+ };
112
156
  }
113
157
  export interface I_WebsocketChannelEntity<T extends Websocket = Websocket> extends WebsocketEntityData {
114
158
  ws: T;
@@ -116,6 +160,18 @@ export interface I_WebsocketChannelEntity<T extends Websocket = Websocket> exten
116
160
  export type BroadcastOptions = WebsocketMessageOptions & {
117
161
  debug?: boolean;
118
162
  };
163
+ export type AddMemberResult = {
164
+ success: true;
165
+ client: I_WebsocketClient;
166
+ } | {
167
+ success: false;
168
+ reason: "full" | "already_member" | "error";
169
+ error?: Error;
170
+ };
171
+ export type AddMemberOptions = {
172
+ /** Whether to notify client when channel is full (default: false) */
173
+ notify_when_full?: boolean;
174
+ };
119
175
  export interface I_WebsocketChannel<T extends Websocket = Websocket> extends I_WebsocketChannelEntity<T> {
120
176
  limit: number;
121
177
  members: Map<string, I_WebsocketClient>;
@@ -123,7 +179,8 @@ export interface I_WebsocketChannel<T extends Websocket = Websocket> extends I_W
123
179
  createdAt: Date;
124
180
  broadcast(message: WebsocketStructuredMessage | string, options?: BroadcastOptions): void;
125
181
  hasMember(client: I_WebsocketEntity | string): boolean;
126
- addMember(entity: I_WebsocketClient): I_WebsocketClient | false;
182
+ addMember(entity: I_WebsocketClient, options?: AddMemberOptions): AddMemberResult;
183
+ removeMemberInternal(entity: I_WebsocketClient): void;
127
184
  removeMember(entity: I_WebsocketEntity): I_WebsocketClient | false;
128
185
  getMember(client: I_WebsocketEntity | string): I_WebsocketClient | undefined;
129
186
  getMembers(clients?: string[] | I_WebsocketEntity[]): I_WebsocketClient[];