topsyde-utils 1.0.173 → 1.0.175
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.
- package/dist/server/bun/websocket/Channel.d.ts +1 -1
- package/dist/server/bun/websocket/Channel.js +8 -1
- package/dist/server/bun/websocket/Channel.js.map +1 -1
- package/dist/server/bun/websocket/Websocket.d.ts +85 -0
- package/dist/server/bun/websocket/Websocket.js +79 -0
- package/dist/server/bun/websocket/Websocket.js.map +1 -1
- package/package.json +1 -1
- package/src/server/bun/websocket/Channel.ts +8 -1
- package/src/server/bun/websocket/Websocket.ts +86 -0
@@ -11,7 +11,7 @@ export default class Channel<T extends Websocket = Websocket> implements I_Webso
|
|
11
11
|
private messageTemplate;
|
12
12
|
private message;
|
13
13
|
constructor(id: string, name: string, ws: T, limit?: number, members?: Map<string, I_WebsocketClient>, metadata?: Record<string, string>);
|
14
|
-
broadcast(message: WebsocketMessage, options?: BroadcastOptions): void;
|
14
|
+
broadcast(message: WebsocketMessage | string, options?: BroadcastOptions): void;
|
15
15
|
private getFilteredMetadata;
|
16
16
|
hasMember(client: I_WebsocketEntity | string): boolean;
|
17
17
|
addMember(client: I_WebsocketClient): false | I_WebsocketClient;
|
@@ -1,4 +1,4 @@
|
|
1
|
-
import { Lib } from "../../../utils";
|
1
|
+
import { Guards, Lib } from "../../../utils";
|
2
2
|
import Message from "./Message";
|
3
3
|
export default class Channel {
|
4
4
|
constructor(id, name, ws, limit, members, metadata) {
|
@@ -18,6 +18,13 @@ export default class Channel {
|
|
18
18
|
};
|
19
19
|
}
|
20
20
|
broadcast(message, options) {
|
21
|
+
if (Guards.IsString(message)) {
|
22
|
+
const msg = {
|
23
|
+
type: "message",
|
24
|
+
content: { message },
|
25
|
+
};
|
26
|
+
message = msg;
|
27
|
+
}
|
21
28
|
const output = this.message.create(message, { ...options, channel: this.id });
|
22
29
|
if (options) {
|
23
30
|
// Include channel metadata if requested
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"Channel.js","sourceRoot":"","sources":["../../../../src/server/bun/websocket/Channel.ts"],"names":[],"mappings":"AAAA,OAAO,
|
1
|
+
{"version":3,"file":"Channel.js","sourceRoot":"","sources":["../../../../src/server/bun/websocket/Channel.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,gBAAgB,CAAC;AAC7C,OAAO,OAAO,MAAM,WAAW,CAAC;AAYhC,MAAM,CAAC,OAAO,OAAO,OAAO;IAY3B,YAAY,EAAU,EAAE,IAAY,EAAE,EAAK,EAAE,KAAc,EAAE,OAAwC,EAAE,QAAiC;QAXjI,cAAS,GAAS,IAAI,IAAI,EAAE,CAAC;QAYnC,IAAI,CAAC,EAAE,GAAG,EAAE,CAAC;QACb,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,KAAK,GAAG,KAAK,IAAI,CAAC,CAAC;QACxB,IAAI,CAAC,OAAO,GAAG,OAAO,IAAI,IAAI,GAAG,EAAE,CAAC;QACpC,IAAI,CAAC,QAAQ,GAAG,QAAQ,IAAI,EAAE,CAAC;QAC/B,IAAI,CAAC,EAAE,GAAG,EAAE,CAAC;QACb,IAAI,CAAC,OAAO,GAAG,IAAI,OAAO,EAAE,CAAC;QAC7B,IAAI,CAAC,eAAe,GAAG;YACtB,IAAI,EAAE,EAAE;YACR,OAAO,EAAE,EAAE;YACX,OAAO,EAAE,IAAI,CAAC,EAAE;YAChB,SAAS,EAAE,EAAE;SACb,CAAC;IACH,CAAC;IAEM,SAAS,CAAC,OAAkC,EAAE,OAA0B;QAC9E,IAAI,MAAM,CAAC,QAAQ,CAAC,OAAO,CAAC,EAAE,CAAC;YAC9B,MAAM,GAAG,GAAqB;gBAC7B,IAAI,EAAE,SAAS;gBACf,OAAO,EAAE,EAAE,OAAO,EAAE;aACpB,CAAC;YACF,OAAO,GAAG,GAAG,CAAC;QACf,CAAC;QACD,MAAM,MAAM,GAAG,IAAI,CAAC,OA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AAC;YACvB,MAAM,YAAY,GAAG,QAAQ,CAAC,MAAM,EAAE,CAAC,IAAI,EAAE,CAAC,KAAK,CAAC;YACpD,IAAI,YAAY,EAAE,CAAC;gBAClB,OAAO,YAAY,CAAC,WAA6B,CAAC;YACnD,CAAC;iBAAM,CAAC;gBACP,OAAO,OAAO,CAAC;YAChB,CAAC;QACF,CAAC;aAAM,CAAC;YACP,GAAG,CAAC,IAAI,CAAC,iDAAiD,CAAC,CAAC;YAC5D,OAAO,OAAO,CAAC;QAChB,CAAC;IACF,CAAC;CACD","sourcesContent":["import { Guards, Lib } from \"../../../utils\";\nimport Message from \"./Message\";\nimport Websocket from \"./Websocket\";\nimport type {\n\tBroadcastOptions,\n\tI_WebsocketChannel,\n\tI_WebsocketClient,\n\tI_WebsocketEntity,\n\tWebsocketChannel,\n\tWebsocketMessage,\n\tWebsocketStructuredMessage,\n} from \"./websocket.types\";\n\nexport default class Channel<T extends Websocket = Websocket> implements I_WebsocketChannel<T> {\n\tpublic createdAt: Date = new Date();\n\tpublic id: string;\n\tpublic name: string;\n\tpublic limit: number;\n\tpublic members: Map<string, I_WebsocketClient>;\n\tpublic metadata: Record<string, string>;\n\tpublic ws: T;\n\t// Message template for reuse\n\tprivate messageTemplate: WebsocketStructuredMessage<any>;\n\tprivate message: Message;\n\n\tconstructor(id: string, name: string, ws: T, limit?: number, members?: Map<string, I_WebsocketClient>, metadata?: Record<string, string>) {\n\t\tthis.id = id;\n\t\tthis.name = name;\n\t\tthis.limit = limit ?? 5;\n\t\tthis.members = members ?? new Map();\n\t\tthis.metadata = metadata ?? {};\n\t\tthis.ws = ws;\n\t\tthis.message = new Message();\n\t\tthis.messageTemplate = {\n\t\t\ttype: \"\",\n\t\t\tcontent: {},\n\t\t\tchannel: this.id,\n\t\t\ttimestamp: \"\",\n\t\t};\n\t}\n\n\tpublic broadcast(message: WebsocketMessage | string, options?: BroadcastOptions) {\n\t\tif (Guards.IsString(message)) {\n\t\t\tconst msg: WebsocketMessage = {\n\t\t\t\ttype: \"message\",\n\t\t\t\tcontent: { message },\n\t\t\t};\n\t\t\tmessage = msg;\n\t\t}\n\t\tconst output = this.message.create(message, { ...options, channel: this.id });\n\t\tif (options) {\n\t\t\t// Include channel metadata if requested\n\t\t\tif (options.includeMetadata) {\n\t\t\t\toutput.metadata = options.includeMetadata === true ? this.getMetadata() : this.getFilteredMetadata(options.includeMetadata);\n\t\t\t}\n\n\t\t\t// Handle excluded clients if needed\n\t\t\tif (options.excludeClients && options.excludeClients.length > 0) {\n\t\t\t\t// For large channels with many excluded clients, it might be more efficient\n\t\t\t\t// to send directly to each client instead of using channel publish\n\t\t\t\tif (this.members.size > 10 && options.excludeClients.length > this.members.size / 3) {\n\t\t\t\t\tconst serializedMessage = this.message.serialize(output);\n\t\t\t\t\tfor (const [clientId, client] of this.members) {\n\t\t\t\t\t\tif (!options.excludeClients.includes(clientId)) {\n\t\t\t\t\t\t\tclient.ws.send(serializedMessage);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\t// Publish to the channel\n\t\tthis.ws.server.publish(this.id, this.message.serialize(output));\n\t}\n\n\t// Helper method for filtered metadata\n\tprivate getFilteredMetadata(keys: string[]) {\n\t\tconst metadata = this.getMetadata();\n\t\tconst filtered: Record<string, string> = {};\n\n\t\tfor (const key of keys) {\n\t\t\tif (metadata[key] !== undefined) {\n\t\t\t\tfiltered[key] = metadata[key];\n\t\t\t}\n\t\t}\n\n\t\treturn filtered;\n\t}\n\n\tpublic hasMember(client: I_WebsocketEntity | string) {\n\t\tif (typeof client === \"string\") return this.members.has(client);\n\t\treturn this.members.has(client.id);\n\t}\n\n\tpublic addMember(client: I_WebsocketClient) {\n\t\tif (!this.canAddMember()) return false;\n\t\tthis.members.set(client.id, client);\n\t\tclient.joinChannel(this);\n\t\treturn client;\n\t}\n\n\tpublic removeMember(entity: I_WebsocketEntity) {\n\t\tif (!this.members.has(entity.id)) return false;\n\t\tconst client = this.members.get(entity.id);\n\t\tif (!client) return false;\n\t\tclient.leaveChannel(this);\n\t\tthis.members.delete(entity.id);\n\t\treturn client;\n\t}\n\n\tpublic getMember(client: I_WebsocketEntity | string) {\n\t\tif (typeof client === \"string\") return this.members.get(client);\n\t\treturn this.members.get(client.id);\n\t}\n\n\tpublic getMembers(clients?: string[] | I_WebsocketEntity[]): I_WebsocketClient[] {\n\t\tif (!clients) return Array.from(this.members.values());\n\t\treturn clients.map((client) => this.getMember(client)).filter((client) => client !== undefined) as I_WebsocketClient[];\n\t}\n\n\tpublic getMetadata() {\n\t\treturn this.metadata;\n\t}\n\n\tpublic getCreatedAt() {\n\t\treturn this.createdAt;\n\t}\n\n\tpublic getId() {\n\t\treturn this.id;\n\t}\n\n\tpublic getName() {\n\t\treturn this.name;\n\t}\n\n\tpublic getLimit() {\n\t\treturn this.limit;\n\t}\n\n\tpublic getSize() {\n\t\treturn this.members.size;\n\t}\n\n\tpublic canAddMember() {\n\t\tconst size = this.getSize();\n\t\treturn size < this.limit;\n\t}\n\n\tpublic static GetChannelType(channels: WebsocketChannel<I_WebsocketChannel> | undefined) {\n\t\tif (!channels) return Channel;\n\t\tif (channels.size > 0) {\n\t\t\tconst firstChannel = channels.values().next().value;\n\t\t\tif (firstChannel) {\n\t\t\t\treturn firstChannel.constructor as typeof Channel;\n\t\t\t} else {\n\t\t\t\treturn Channel;\n\t\t\t}\n\t\t} else {\n\t\t\tLib.Warn(\"Channels are empty, using default channel class\");\n\t\t\treturn Channel;\n\t\t}\n\t}\n}\n"]}
|
@@ -26,8 +26,26 @@ export default class Websocket extends Singleton {
|
|
26
26
|
protected set server(value: Server);
|
27
27
|
get server(): Server;
|
28
28
|
set(server: Server): void;
|
29
|
+
/**
|
30
|
+
* Create a new channel
|
31
|
+
* @param id - The id of the channel
|
32
|
+
* @param name - The name of the channel
|
33
|
+
* @param limit - The limit of the channel
|
34
|
+
* @returns The created channel
|
35
|
+
*/
|
29
36
|
createChannel(id: string, name: string, limit?: number): I_WebsocketChannel;
|
37
|
+
/**
|
38
|
+
* Remove a channel
|
39
|
+
* @param id - The id of the channel
|
40
|
+
*/
|
30
41
|
removeChannel(id: string): void;
|
42
|
+
/**
|
43
|
+
* Create a new channel
|
44
|
+
* @param id - The id of the channel
|
45
|
+
* @param name - The name of the channel
|
46
|
+
* @param limit - The limit of the channel
|
47
|
+
* @returns The created channel
|
48
|
+
*/
|
31
49
|
static CreateChannel(id: string, name: string, limit?: number): I_WebsocketChannel<Websocket>;
|
32
50
|
handlers(): WebSocketHandler<WebsocketEntityData>;
|
33
51
|
private clientMessageReceived;
|
@@ -35,19 +53,86 @@ export default class Websocket extends Singleton {
|
|
35
53
|
private clientDisconnected;
|
36
54
|
private handleHeartbeat;
|
37
55
|
protected createClient(entity: I_WebsocketEntity): I_WebsocketClient;
|
56
|
+
/**
|
57
|
+
* Handle the heartbeat
|
58
|
+
* @param ws - The websocket
|
59
|
+
* @param message - The message
|
60
|
+
* @returns True if the heartbeat was handled, false otherwise
|
61
|
+
*/
|
38
62
|
static Heartbeat(ws: ServerWebSocket<WebsocketEntityData>, message: BunWebsocketMessage): boolean;
|
63
|
+
/**
|
64
|
+
* Get the server
|
65
|
+
* @returns The server
|
66
|
+
*/
|
39
67
|
static Server(): Server;
|
68
|
+
/**
|
69
|
+
* Broadcast a message to a channel
|
70
|
+
* @param channel - The channel
|
71
|
+
* @param message - The message
|
72
|
+
* @param args - The arguments
|
73
|
+
*/
|
40
74
|
static Broadcast(channel: string, message: WebsocketStructuredMessage, ...args: any[]): void;
|
75
|
+
/**
|
76
|
+
* Broadcast a message to all channels
|
77
|
+
* @param message - The message
|
78
|
+
* @param args - The arguments
|
79
|
+
*/
|
41
80
|
static BraodcastAll(message: WebsocketStructuredMessage, ...args: any[]): void;
|
81
|
+
/**
|
82
|
+
* Join a channel
|
83
|
+
* @param channel - The channel
|
84
|
+
* @param entity - The entity
|
85
|
+
*/
|
42
86
|
static Join(channel: string, entity: I_WebsocketEntity): void;
|
87
|
+
/**
|
88
|
+
* Leave a channel
|
89
|
+
* @param channel - The channel
|
90
|
+
* @param entity - The entity
|
91
|
+
*/
|
43
92
|
static Leave(channel: string, entity: I_WebsocketEntity): void;
|
93
|
+
/**
|
94
|
+
* Get a client
|
95
|
+
* @param id - The id of the client
|
96
|
+
* @param throw_if_nil - Whether to throw an error if the client is not found
|
97
|
+
* @returns The client
|
98
|
+
*/
|
44
99
|
static GetClient(id: string, throw_if_nil?: true): I_WebsocketClient;
|
45
100
|
static GetClient(id: string, throw_if_nil?: false): I_WebsocketClient | undefined;
|
101
|
+
/**
|
102
|
+
* Get a channel
|
103
|
+
* @param id - The id of the channel
|
104
|
+
* @returns The channel
|
105
|
+
*/
|
46
106
|
static GetChannel(id: string): Channel<Websocket> | undefined;
|
107
|
+
/**
|
108
|
+
* Get all channels
|
109
|
+
* @returns The channels
|
110
|
+
*/
|
47
111
|
static GetChannels(): Channel<Websocket>[];
|
112
|
+
/**
|
113
|
+
* Get all clients
|
114
|
+
* @returns The clients
|
115
|
+
*/
|
48
116
|
static GetClients(): I_WebsocketClient[];
|
117
|
+
/**
|
118
|
+
* Get the number of clients
|
119
|
+
* @returns The number of clients
|
120
|
+
*/
|
49
121
|
static GetClientCount(): number;
|
122
|
+
/**
|
123
|
+
* Get the number of channels
|
124
|
+
* @returns The number of channels
|
125
|
+
*/
|
50
126
|
static GetChannelCount(): number;
|
127
|
+
/**
|
128
|
+
* Create a client
|
129
|
+
* @param entity - The entity
|
130
|
+
* @returns The created client
|
131
|
+
*/
|
51
132
|
static CreateClient(entity: I_WebsocketEntity): I_WebsocketClient;
|
133
|
+
/**
|
134
|
+
* Generate a message
|
135
|
+
* @returns The generated message
|
136
|
+
*/
|
52
137
|
static GenerateMessage(): WebsocketStructuredMessage;
|
53
138
|
}
|
@@ -69,6 +69,13 @@ export default class Websocket extends Singleton {
|
|
69
69
|
Console.blank();
|
70
70
|
Console.success("Websocket server set");
|
71
71
|
}
|
72
|
+
/**
|
73
|
+
* Create a new channel
|
74
|
+
* @param id - The id of the channel
|
75
|
+
* @param name - The name of the channel
|
76
|
+
* @param limit - The limit of the channel
|
77
|
+
* @returns The created channel
|
78
|
+
*/
|
72
79
|
createChannel(id, name, limit) {
|
73
80
|
if (this._channels.has(id))
|
74
81
|
return this._channels.get(id);
|
@@ -76,9 +83,20 @@ export default class Websocket extends Singleton {
|
|
76
83
|
this._channels.set(id, channel);
|
77
84
|
return channel;
|
78
85
|
}
|
86
|
+
/**
|
87
|
+
* Remove a channel
|
88
|
+
* @param id - The id of the channel
|
89
|
+
*/
|
79
90
|
removeChannel(id) {
|
80
91
|
this._channels.delete(id);
|
81
92
|
}
|
93
|
+
/**
|
94
|
+
* Create a new channel
|
95
|
+
* @param id - The id of the channel
|
96
|
+
* @param name - The name of the channel
|
97
|
+
* @param limit - The limit of the channel
|
98
|
+
* @returns The created channel
|
99
|
+
*/
|
82
100
|
static CreateChannel(id, name, limit) {
|
83
101
|
const ws = this.GetInstance();
|
84
102
|
return ws.createChannel(id, name, limit);
|
@@ -93,13 +111,29 @@ export default class Websocket extends Singleton {
|
|
93
111
|
createClient(entity) {
|
94
112
|
return new this._clientClass(entity);
|
95
113
|
}
|
114
|
+
/**
|
115
|
+
* Handle the heartbeat
|
116
|
+
* @param ws - The websocket
|
117
|
+
* @param message - The message
|
118
|
+
* @returns True if the heartbeat was handled, false otherwise
|
119
|
+
*/
|
96
120
|
static Heartbeat(ws, message) {
|
97
121
|
const self = this.GetInstance();
|
98
122
|
return self.handleHeartbeat(ws, message);
|
99
123
|
}
|
124
|
+
/**
|
125
|
+
* Get the server
|
126
|
+
* @returns The server
|
127
|
+
*/
|
100
128
|
static Server() {
|
101
129
|
return this.GetInstance().server;
|
102
130
|
}
|
131
|
+
/**
|
132
|
+
* Broadcast a message to a channel
|
133
|
+
* @param channel - The channel
|
134
|
+
* @param message - The message
|
135
|
+
* @param args - The arguments
|
136
|
+
*/
|
103
137
|
static Broadcast(channel, message, ...args) {
|
104
138
|
// Get the server from the singleton instance
|
105
139
|
const ws = this.GetInstance();
|
@@ -108,10 +142,20 @@ export default class Websocket extends Singleton {
|
|
108
142
|
}
|
109
143
|
ws.server.publish(channel, JSON.stringify({ message, args }));
|
110
144
|
}
|
145
|
+
/**
|
146
|
+
* Broadcast a message to all channels
|
147
|
+
* @param message - The message
|
148
|
+
* @param args - The arguments
|
149
|
+
*/
|
111
150
|
static BraodcastAll(message, ...args) {
|
112
151
|
const ws = this.GetInstance();
|
113
152
|
ws._channels.forEach((channel) => channel.broadcast(message, ...args));
|
114
153
|
}
|
154
|
+
/**
|
155
|
+
* Join a channel
|
156
|
+
* @param channel - The channel
|
157
|
+
* @param entity - The entity
|
158
|
+
*/
|
115
159
|
static Join(channel, entity) {
|
116
160
|
const ws = this.GetInstance();
|
117
161
|
const client = ws._clients.get(entity.id);
|
@@ -119,6 +163,11 @@ export default class Websocket extends Singleton {
|
|
119
163
|
return;
|
120
164
|
ws._channels.get(channel)?.addMember(client);
|
121
165
|
}
|
166
|
+
/**
|
167
|
+
* Leave a channel
|
168
|
+
* @param channel - The channel
|
169
|
+
* @param entity - The entity
|
170
|
+
*/
|
122
171
|
static Leave(channel, entity) {
|
123
172
|
const ws = this.GetInstance();
|
124
173
|
const client = ws._clients.get(entity.id);
|
@@ -133,30 +182,60 @@ export default class Websocket extends Singleton {
|
|
133
182
|
throw new Error(`Client with id ${id} not found`);
|
134
183
|
return client;
|
135
184
|
}
|
185
|
+
/**
|
186
|
+
* Get a channel
|
187
|
+
* @param id - The id of the channel
|
188
|
+
* @returns The channel
|
189
|
+
*/
|
136
190
|
static GetChannel(id) {
|
137
191
|
const ws = this.GetInstance();
|
138
192
|
return ws._channels.get(id);
|
139
193
|
}
|
194
|
+
/**
|
195
|
+
* Get all channels
|
196
|
+
* @returns The channels
|
197
|
+
*/
|
140
198
|
static GetChannels() {
|
141
199
|
const ws = this.GetInstance();
|
142
200
|
return Array.from(ws._channels.values());
|
143
201
|
}
|
202
|
+
/**
|
203
|
+
* Get all clients
|
204
|
+
* @returns The clients
|
205
|
+
*/
|
144
206
|
static GetClients() {
|
145
207
|
const ws = this.GetInstance();
|
146
208
|
return Array.from(ws._clients.values());
|
147
209
|
}
|
210
|
+
/**
|
211
|
+
* Get the number of clients
|
212
|
+
* @returns The number of clients
|
213
|
+
*/
|
148
214
|
static GetClientCount() {
|
149
215
|
const ws = this.GetInstance();
|
150
216
|
return ws._clients.size;
|
151
217
|
}
|
218
|
+
/**
|
219
|
+
* Get the number of channels
|
220
|
+
* @returns The number of channels
|
221
|
+
*/
|
152
222
|
static GetChannelCount() {
|
153
223
|
const ws = this.GetInstance();
|
154
224
|
return ws._channels.size;
|
155
225
|
}
|
226
|
+
/**
|
227
|
+
* Create a client
|
228
|
+
* @param entity - The entity
|
229
|
+
* @returns The created client
|
230
|
+
*/
|
156
231
|
static CreateClient(entity) {
|
157
232
|
const ws = this.GetInstance();
|
158
233
|
return ws.createClient(entity);
|
159
234
|
}
|
235
|
+
/**
|
236
|
+
* Generate a message
|
237
|
+
* @returns The generated message
|
238
|
+
*/
|
160
239
|
static GenerateMessage() {
|
161
240
|
const msg = {
|
162
241
|
type: "",
|
@@ -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,EAAE,sBAAsB,EAAE,MAAM,mBAAmB,CAAC;AAc3D,MAAM,CAAC,OAAO,OAAO,SAAU,SAAQ,SAAS;IAS/C,YAAsB,OAAgC;QACrD,KAAK,EAAE,CAAC;QARC,aAAQ,GAAmC,IAAI,GA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,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\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\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\tpublic removeChannel(id: string) {\n\t\tthis._channels.delete(id);\n\t}\n\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\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\tpublic static Server() {\n\t\treturn this.GetInstance<Websocket>().server;\n\t}\n\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\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\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\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\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\tpublic static GetChannel(id: string) {\n\t\tconst ws = this.GetInstance<Websocket>();\n\t\treturn ws._channels.get(id);\n\t}\n\n\tpublic static GetChannels() {\n\t\tconst ws = this.GetInstance<Websocket>();\n\t\treturn Array.from(ws._channels.values());\n\t}\n\n\tpublic static GetClients() {\n\t\tconst ws = this.GetInstance<Websocket>();\n\t\treturn Array.from(ws._clients.values());\n\t}\n\n\tpublic static GetClientCount() {\n\t\tconst ws = this.GetInstance<Websocket>();\n\t\treturn ws._clients.size;\n\t}\n\n\tpublic static GetChannelCount() {\n\t\tconst ws = this.GetInstance<Websocket>();\n\t\treturn ws._channels.size;\n\t}\n\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\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,"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;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,EAAa,CAAC;QACzC,OAAO,EAAE,CAAC,QAAQ,CAAC,IAAI,CAAC;IACzB,CAAC;IAED;;;OAGG;IACI,MAAM,CAAC,eAAe;QAC5B,MAAM,EAAE,GAAG,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"]}
|
package/package.json
CHANGED
@@ -39,7 +39,14 @@ export default class Channel<T extends Websocket = Websocket> implements I_Webso
|
|
39
39
|
};
|
40
40
|
}
|
41
41
|
|
42
|
-
public broadcast(message: WebsocketMessage, options?: BroadcastOptions) {
|
42
|
+
public broadcast(message: WebsocketMessage | string, options?: BroadcastOptions) {
|
43
|
+
if (Guards.IsString(message)) {
|
44
|
+
const msg: WebsocketMessage = {
|
45
|
+
type: "message",
|
46
|
+
content: { message },
|
47
|
+
};
|
48
|
+
message = msg;
|
49
|
+
}
|
43
50
|
const output = this.message.create(message, { ...options, channel: this.id });
|
44
51
|
if (options) {
|
45
52
|
// Include channel metadata if requested
|
@@ -37,6 +37,7 @@ export default class Websocket extends Singleton {
|
|
37
37
|
protected _ws_interface?: I_WebsocketInterface;
|
38
38
|
protected _options: WebsocketConstructorOptions;
|
39
39
|
protected _ws_interface_handlers: Partial<WebSocketHandler<WebsocketEntityData>>;
|
40
|
+
|
40
41
|
protected constructor(options?: I_WebsocketConstructor) {
|
41
42
|
super();
|
42
43
|
this._ws_interface = options?.ws_interface;
|
@@ -62,6 +63,13 @@ export default class Websocket extends Singleton {
|
|
62
63
|
Console.success("Websocket server set");
|
63
64
|
}
|
64
65
|
|
66
|
+
/**
|
67
|
+
* Create a new channel
|
68
|
+
* @param id - The id of the channel
|
69
|
+
* @param name - The name of the channel
|
70
|
+
* @param limit - The limit of the channel
|
71
|
+
* @returns The created channel
|
72
|
+
*/
|
65
73
|
public createChannel(id: string, name: string, limit?: number): I_WebsocketChannel {
|
66
74
|
if (this._channels.has(id)) return this._channels.get(id) as I_WebsocketChannel;
|
67
75
|
const channel = new this._channelClass(id, name, this, limit);
|
@@ -69,10 +77,21 @@ export default class Websocket extends Singleton {
|
|
69
77
|
return channel;
|
70
78
|
}
|
71
79
|
|
80
|
+
/**
|
81
|
+
* Remove a channel
|
82
|
+
* @param id - The id of the channel
|
83
|
+
*/
|
72
84
|
public removeChannel(id: string) {
|
73
85
|
this._channels.delete(id);
|
74
86
|
}
|
75
87
|
|
88
|
+
/**
|
89
|
+
* Create a new channel
|
90
|
+
* @param id - The id of the channel
|
91
|
+
* @param name - The name of the channel
|
92
|
+
* @param limit - The limit of the channel
|
93
|
+
* @returns The created channel
|
94
|
+
*/
|
76
95
|
public static CreateChannel(id: string, name: string, limit?: number) {
|
77
96
|
const ws = this.GetInstance<Websocket>();
|
78
97
|
return ws.createChannel(id, name, limit);
|
@@ -138,15 +157,31 @@ export default class Websocket extends Singleton {
|
|
138
157
|
return new this._clientClass(entity);
|
139
158
|
}
|
140
159
|
|
160
|
+
/**
|
161
|
+
* Handle the heartbeat
|
162
|
+
* @param ws - The websocket
|
163
|
+
* @param message - The message
|
164
|
+
* @returns True if the heartbeat was handled, false otherwise
|
165
|
+
*/
|
141
166
|
public static Heartbeat(ws: ServerWebSocket<WebsocketEntityData>, message: BunWebsocketMessage) {
|
142
167
|
const self = this.GetInstance<Websocket>();
|
143
168
|
return self.handleHeartbeat(ws, message);
|
144
169
|
}
|
145
170
|
|
171
|
+
/**
|
172
|
+
* Get the server
|
173
|
+
* @returns The server
|
174
|
+
*/
|
146
175
|
public static Server() {
|
147
176
|
return this.GetInstance<Websocket>().server;
|
148
177
|
}
|
149
178
|
|
179
|
+
/**
|
180
|
+
* Broadcast a message to a channel
|
181
|
+
* @param channel - The channel
|
182
|
+
* @param message - The message
|
183
|
+
* @param args - The arguments
|
184
|
+
*/
|
150
185
|
public static Broadcast(channel: string, message: WebsocketStructuredMessage, ...args: any[]) {
|
151
186
|
// Get the server from the singleton instance
|
152
187
|
const ws = this.GetInstance<Websocket>();
|
@@ -156,11 +191,21 @@ export default class Websocket extends Singleton {
|
|
156
191
|
ws.server.publish(channel, JSON.stringify({ message, args }));
|
157
192
|
}
|
158
193
|
|
194
|
+
/**
|
195
|
+
* Broadcast a message to all channels
|
196
|
+
* @param message - The message
|
197
|
+
* @param args - The arguments
|
198
|
+
*/
|
159
199
|
public static BraodcastAll(message: WebsocketStructuredMessage, ...args: any[]) {
|
160
200
|
const ws = this.GetInstance<Websocket>();
|
161
201
|
ws._channels.forEach((channel) => channel.broadcast(message, ...args));
|
162
202
|
}
|
163
203
|
|
204
|
+
/**
|
205
|
+
* Join a channel
|
206
|
+
* @param channel - The channel
|
207
|
+
* @param entity - The entity
|
208
|
+
*/
|
164
209
|
public static Join(channel: string, entity: I_WebsocketEntity) {
|
165
210
|
const ws = this.GetInstance<Websocket>();
|
166
211
|
const client = ws._clients.get(entity.id);
|
@@ -168,6 +213,11 @@ export default class Websocket extends Singleton {
|
|
168
213
|
ws._channels.get(channel)?.addMember(client);
|
169
214
|
}
|
170
215
|
|
216
|
+
/**
|
217
|
+
* Leave a channel
|
218
|
+
* @param channel - The channel
|
219
|
+
* @param entity - The entity
|
220
|
+
*/
|
171
221
|
public static Leave(channel: string, entity: I_WebsocketEntity) {
|
172
222
|
const ws = this.GetInstance<Websocket>();
|
173
223
|
const client = ws._clients.get(entity.id);
|
@@ -175,6 +225,12 @@ export default class Websocket extends Singleton {
|
|
175
225
|
ws._channels.get(channel)?.removeMember(client);
|
176
226
|
}
|
177
227
|
|
228
|
+
/**
|
229
|
+
* Get a client
|
230
|
+
* @param id - The id of the client
|
231
|
+
* @param throw_if_nil - Whether to throw an error if the client is not found
|
232
|
+
* @returns The client
|
233
|
+
*/
|
178
234
|
public static GetClient(id: string, throw_if_nil?: true): I_WebsocketClient;
|
179
235
|
public static GetClient(id: string, throw_if_nil?: false): I_WebsocketClient | undefined;
|
180
236
|
public static GetClient(id: string, throw_if_nil: boolean = true): I_WebsocketClient | undefined {
|
@@ -184,36 +240,66 @@ export default class Websocket extends Singleton {
|
|
184
240
|
return client;
|
185
241
|
}
|
186
242
|
|
243
|
+
/**
|
244
|
+
* Get a channel
|
245
|
+
* @param id - The id of the channel
|
246
|
+
* @returns The channel
|
247
|
+
*/
|
187
248
|
public static GetChannel(id: string) {
|
188
249
|
const ws = this.GetInstance<Websocket>();
|
189
250
|
return ws._channels.get(id);
|
190
251
|
}
|
191
252
|
|
253
|
+
/**
|
254
|
+
* Get all channels
|
255
|
+
* @returns The channels
|
256
|
+
*/
|
192
257
|
public static GetChannels() {
|
193
258
|
const ws = this.GetInstance<Websocket>();
|
194
259
|
return Array.from(ws._channels.values());
|
195
260
|
}
|
196
261
|
|
262
|
+
/**
|
263
|
+
* Get all clients
|
264
|
+
* @returns The clients
|
265
|
+
*/
|
197
266
|
public static GetClients() {
|
198
267
|
const ws = this.GetInstance<Websocket>();
|
199
268
|
return Array.from(ws._clients.values());
|
200
269
|
}
|
201
270
|
|
271
|
+
/**
|
272
|
+
* Get the number of clients
|
273
|
+
* @returns The number of clients
|
274
|
+
*/
|
202
275
|
public static GetClientCount() {
|
203
276
|
const ws = this.GetInstance<Websocket>();
|
204
277
|
return ws._clients.size;
|
205
278
|
}
|
206
279
|
|
280
|
+
/**
|
281
|
+
* Get the number of channels
|
282
|
+
* @returns The number of channels
|
283
|
+
*/
|
207
284
|
public static GetChannelCount() {
|
208
285
|
const ws = this.GetInstance<Websocket>();
|
209
286
|
return ws._channels.size;
|
210
287
|
}
|
211
288
|
|
289
|
+
/**
|
290
|
+
* Create a client
|
291
|
+
* @param entity - The entity
|
292
|
+
* @returns The created client
|
293
|
+
*/
|
212
294
|
public static CreateClient(entity: I_WebsocketEntity): I_WebsocketClient {
|
213
295
|
const ws = this.GetInstance<Websocket>();
|
214
296
|
return ws.createClient(entity);
|
215
297
|
}
|
216
298
|
|
299
|
+
/**
|
300
|
+
* Generate a message
|
301
|
+
* @returns The generated message
|
302
|
+
*/
|
217
303
|
public static GenerateMessage(): WebsocketStructuredMessage {
|
218
304
|
const msg: WebsocketStructuredMessage = {
|
219
305
|
type: "",
|