@nsshunt/stssocketioutils 2.0.6 → 2.0.7
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/index-CbgYj2D4.js +57 -0
- package/dist/index-CbgYj2D4.js.map +1 -0
- package/dist/index-snr1zbog.mjs +58 -0
- package/dist/index-snr1zbog.mjs.map +1 -0
- package/dist/index.cjs +237 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.mjs +237 -0
- package/dist/index.mjs.map +1 -0
- package/dist/node.cjs +325 -0
- package/dist/node.cjs.map +1 -0
- package/dist/{stssocketioutils.mjs → node.mjs} +2 -288
- package/dist/node.mjs.map +1 -0
- package/package.json +12 -5
- package/types/index.d.ts +0 -1
- package/types/index.d.ts.map +1 -1
- package/types/node.d.ts +2 -0
- package/types/node.d.ts.map +1 -0
- package/dist/stssocketioutils.mjs.map +0 -1
- package/dist/stssocketioutils.umd.js +0 -609
- package/dist/stssocketioutils.umd.js.map +0 -1
|
@@ -1,609 +0,0 @@
|
|
|
1
|
-
(function(global, factory) {
|
|
2
|
-
typeof exports === "object" && typeof module !== "undefined" ? factory(exports, require("socket.io-client"), require("@nsshunt/stsutils"), require("socket.io"), require("ioredis"), require("@socket.io/cluster-adapter"), require("@socket.io/redis-streams-adapter")) : typeof define === "function" && define.amd ? define(["exports", "socket.io-client", "@nsshunt/stsutils", "socket.io", "ioredis", "@socket.io/cluster-adapter", "@socket.io/redis-streams-adapter"], factory) : (global = typeof globalThis !== "undefined" ? globalThis : global || self, factory(global["@nsshunt/stssocketioutils"] = {}, global.socket["io-client"], global.stsutils, global.socket.io, global.ioredis, global["@socket"]["io/cluster-adapter"], global["@socket"]["io/redis-streams-adapter"]));
|
|
3
|
-
})(this, (function(exports2, socket_ioClient, stsutils, socket_io, ioredis, clusterAdapter, redisStreamsAdapter) {
|
|
4
|
-
"use strict";
|
|
5
|
-
var STSNamespace = /* @__PURE__ */ ((STSNamespace2) => {
|
|
6
|
-
STSNamespace2["STSMonitor"] = "stsinstrumentmanager/stsmonitor";
|
|
7
|
-
STSNamespace2["STSControl"] = "stsinstrumentmanager/stscontrol";
|
|
8
|
-
STSNamespace2["STSTestingNamespace"] = "ststestingnamespace/ststesting";
|
|
9
|
-
return STSNamespace2;
|
|
10
|
-
})(STSNamespace || {});
|
|
11
|
-
var STSRoom = /* @__PURE__ */ ((STSRoom2) => {
|
|
12
|
-
STSRoom2["STSInstrumentDataRoom"] = "stsinstrumentdataroom";
|
|
13
|
-
STSRoom2["STSInstrumentDataSubscriptionRoom"] = "stsinstrumentdatasubscriptionroom";
|
|
14
|
-
STSRoom2["STSRunnerRoom"] = "stsrunnerroom";
|
|
15
|
-
STSRoom2["STSNodeRunnerRoom"] = "stsnoderunnerroom";
|
|
16
|
-
return STSRoom2;
|
|
17
|
-
})(STSRoom || {});
|
|
18
|
-
var STSEvent = /* @__PURE__ */ ((STSEvent2) => {
|
|
19
|
-
STSEvent2["STSInstrumentData"] = "stsinstrumentdata";
|
|
20
|
-
STSEvent2["STSSubInstrumentData"] = "stssubinstrumentdata";
|
|
21
|
-
STSEvent2["STSResetInstrumentData"] = "stsresetinstrumentdata";
|
|
22
|
-
STSEvent2["STSStartLoggingInstrumentData"] = "stsStartLoggingInstrumentData";
|
|
23
|
-
STSEvent2["STSStopLoggingInstrumentData"] = "stsStopLoggingInstrumentData";
|
|
24
|
-
STSEvent2["STSStart"] = "stsstart";
|
|
25
|
-
STSEvent2["STSStop"] = "stsstop";
|
|
26
|
-
STSEvent2["STSPause"] = "stspause";
|
|
27
|
-
STSEvent2["STSResume"] = "stsresume";
|
|
28
|
-
STSEvent2["STSTerminate"] = "ststerminate";
|
|
29
|
-
STSEvent2["STSUpdateStaticConfig"] = "stsupdatestaticconfig";
|
|
30
|
-
STSEvent2["STSUpdateDynamicConfig"] = "stsupdatedynamicconfig";
|
|
31
|
-
STSEvent2["STSSendToRoom"] = "sendToRoom";
|
|
32
|
-
STSEvent2["connect"] = "connect";
|
|
33
|
-
return STSEvent2;
|
|
34
|
-
})(STSEvent || {});
|
|
35
|
-
const isNode = Object.prototype.toString.call(typeof process !== "undefined" ? process : 0) === "[object process]";
|
|
36
|
-
var tinyEmitter = { exports: {} };
|
|
37
|
-
var hasRequiredTinyEmitter;
|
|
38
|
-
function requireTinyEmitter() {
|
|
39
|
-
if (hasRequiredTinyEmitter) return tinyEmitter.exports;
|
|
40
|
-
hasRequiredTinyEmitter = 1;
|
|
41
|
-
function E() {
|
|
42
|
-
}
|
|
43
|
-
E.prototype = {
|
|
44
|
-
on: function(name, callback, ctx) {
|
|
45
|
-
var e = this.e || (this.e = {});
|
|
46
|
-
(e[name] || (e[name] = [])).push({
|
|
47
|
-
fn: callback,
|
|
48
|
-
ctx
|
|
49
|
-
});
|
|
50
|
-
return this;
|
|
51
|
-
},
|
|
52
|
-
once: function(name, callback, ctx) {
|
|
53
|
-
var self2 = this;
|
|
54
|
-
function listener() {
|
|
55
|
-
self2.off(name, listener);
|
|
56
|
-
callback.apply(ctx, arguments);
|
|
57
|
-
}
|
|
58
|
-
listener._ = callback;
|
|
59
|
-
return this.on(name, listener, ctx);
|
|
60
|
-
},
|
|
61
|
-
emit: function(name) {
|
|
62
|
-
var data = [].slice.call(arguments, 1);
|
|
63
|
-
var evtArr = ((this.e || (this.e = {}))[name] || []).slice();
|
|
64
|
-
var i = 0;
|
|
65
|
-
var len = evtArr.length;
|
|
66
|
-
for (i; i < len; i++) {
|
|
67
|
-
evtArr[i].fn.apply(evtArr[i].ctx, data);
|
|
68
|
-
}
|
|
69
|
-
return this;
|
|
70
|
-
},
|
|
71
|
-
off: function(name, callback) {
|
|
72
|
-
var e = this.e || (this.e = {});
|
|
73
|
-
var evts = e[name];
|
|
74
|
-
var liveEvents = [];
|
|
75
|
-
if (evts && callback) {
|
|
76
|
-
for (var i = 0, len = evts.length; i < len; i++) {
|
|
77
|
-
if (evts[i].fn !== callback && evts[i].fn._ !== callback)
|
|
78
|
-
liveEvents.push(evts[i]);
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
liveEvents.length ? e[name] = liveEvents : delete e[name];
|
|
82
|
-
return this;
|
|
83
|
-
}
|
|
84
|
-
};
|
|
85
|
-
tinyEmitter.exports = E;
|
|
86
|
-
tinyEmitter.exports.TinyEmitter = E;
|
|
87
|
-
return tinyEmitter.exports;
|
|
88
|
-
}
|
|
89
|
-
var tinyEmitterExports = requireTinyEmitter();
|
|
90
|
-
class SocketIoClient extends tinyEmitterExports.TinyEmitter {
|
|
91
|
-
#agentManager;
|
|
92
|
-
#logger;
|
|
93
|
-
#name;
|
|
94
|
-
#address;
|
|
95
|
-
#socketIoCustomPath;
|
|
96
|
-
#authToken;
|
|
97
|
-
#socket;
|
|
98
|
-
constructor(name) {
|
|
99
|
-
super();
|
|
100
|
-
this.#name = name;
|
|
101
|
-
}
|
|
102
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
103
|
-
LogDebugMessage(message) {
|
|
104
|
-
if (this.#logger) this.#logger.debug(message);
|
|
105
|
-
}
|
|
106
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
107
|
-
LogErrorMessage(message) {
|
|
108
|
-
if (this.#logger) this.#logger.error(message);
|
|
109
|
-
}
|
|
110
|
-
get name() {
|
|
111
|
-
return this.#name;
|
|
112
|
-
}
|
|
113
|
-
get agentManager() {
|
|
114
|
-
return this.#agentManager;
|
|
115
|
-
}
|
|
116
|
-
get logger() {
|
|
117
|
-
return this.#logger;
|
|
118
|
-
}
|
|
119
|
-
get address() {
|
|
120
|
-
return this.#address;
|
|
121
|
-
}
|
|
122
|
-
get authToken() {
|
|
123
|
-
return this.#authToken;
|
|
124
|
-
}
|
|
125
|
-
get socketIoCustomPath() {
|
|
126
|
-
return this.#socketIoCustomPath;
|
|
127
|
-
}
|
|
128
|
-
get socket() {
|
|
129
|
-
return this.#socket;
|
|
130
|
-
}
|
|
131
|
-
WithAddress(address) {
|
|
132
|
-
this.#address = address;
|
|
133
|
-
return this;
|
|
134
|
-
}
|
|
135
|
-
WithAuthToken(authToken) {
|
|
136
|
-
this.#authToken = authToken;
|
|
137
|
-
return this;
|
|
138
|
-
}
|
|
139
|
-
WithSocketIoCustomPath(socketIoCustomPath) {
|
|
140
|
-
this.#socketIoCustomPath = socketIoCustomPath;
|
|
141
|
-
return this;
|
|
142
|
-
}
|
|
143
|
-
WithLogger(logger) {
|
|
144
|
-
this.#logger = logger;
|
|
145
|
-
return this;
|
|
146
|
-
}
|
|
147
|
-
WithAgentManager(agentManager) {
|
|
148
|
-
this.#agentManager = agentManager;
|
|
149
|
-
return this;
|
|
150
|
-
}
|
|
151
|
-
SetupSocket() {
|
|
152
|
-
if (!this.#address) {
|
|
153
|
-
throw new Error(`SocketIoClientHelper:SetupSocket(): Error: [address not provided]`);
|
|
154
|
-
}
|
|
155
|
-
this.#EstablishSocketConnect();
|
|
156
|
-
return this;
|
|
157
|
-
}
|
|
158
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
159
|
-
ConnectCallBack(socket) {
|
|
160
|
-
}
|
|
161
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
162
|
-
SocketEventsCallBack(socket) {
|
|
163
|
-
}
|
|
164
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
165
|
-
ErrorCallBack(error) {
|
|
166
|
-
}
|
|
167
|
-
/*
|
|
168
|
-
emit<Ev extends EventNames<ClientToServerEvents>>(ev: Ev, ...args: EventParams<ClientToServerEvents, Ev>): Socket<ServerToClientEvents, ClientToServerEvents> {
|
|
169
|
-
if (this.#socket) {
|
|
170
|
-
return this.#socket!.emit(ev, ...args);
|
|
171
|
-
} else {
|
|
172
|
-
throw new Error('SocketIoClient:emit(): Error: [socket instance not defined.]');
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
*/
|
|
176
|
-
#EstablishSocketConnect() {
|
|
177
|
-
if (this.#socket !== void 0) {
|
|
178
|
-
if (this.#socket.connected === true) {
|
|
179
|
-
this.#socket.disconnect();
|
|
180
|
-
}
|
|
181
|
-
this.#socket = void 0;
|
|
182
|
-
if (isNode) {
|
|
183
|
-
setTimeout(() => this.#EstablishSocketConnect(), 100).unref();
|
|
184
|
-
} else {
|
|
185
|
-
setTimeout(() => this.#EstablishSocketConnect(), 100);
|
|
186
|
-
}
|
|
187
|
-
return;
|
|
188
|
-
}
|
|
189
|
-
let socketOptions;
|
|
190
|
-
if (isNode) {
|
|
191
|
-
socketOptions = {
|
|
192
|
-
transports: ["websocket"]
|
|
193
|
-
};
|
|
194
|
-
if (this.#agentManager) {
|
|
195
|
-
if (!this.#address) {
|
|
196
|
-
throw new Error(`SocketIoClientHelper:SetupSocket(): Error: [address not provided when using agentManager]`);
|
|
197
|
-
}
|
|
198
|
-
socketOptions.agent = this.#agentManager.GetAgent(this.#address);
|
|
199
|
-
}
|
|
200
|
-
} else {
|
|
201
|
-
socketOptions = {
|
|
202
|
-
transports: ["websocket"]
|
|
203
|
-
};
|
|
204
|
-
}
|
|
205
|
-
if (this.#authToken) {
|
|
206
|
-
socketOptions.auth = (cb) => {
|
|
207
|
-
cb({
|
|
208
|
-
token: this.#authToken
|
|
209
|
-
});
|
|
210
|
-
};
|
|
211
|
-
}
|
|
212
|
-
if (this.#socketIoCustomPath && this.#socketIoCustomPath.localeCompare("") !== 0) {
|
|
213
|
-
socketOptions.path = this.#socketIoCustomPath;
|
|
214
|
-
}
|
|
215
|
-
this.#socket = socket_ioClient.io(this.#address, socketOptions);
|
|
216
|
-
this.#socket.io.on("error", (err) => {
|
|
217
|
-
this.LogErrorMessage(`SocketIoClientHelper(): socketDetail.socket.io.on('error'): [${err}] Address: [${this.#address}]`);
|
|
218
|
-
});
|
|
219
|
-
this.#socket.io.on("reconnect_error", (err) => {
|
|
220
|
-
this.LogErrorMessage(`SocketIoClientHelper(): socketDetail.socket.io.on('reconnect_error'): [${err}] Address: [${this.#address}]`);
|
|
221
|
-
});
|
|
222
|
-
this.#socket.on("connect_error", (err) => {
|
|
223
|
-
this.LogErrorMessage(`SocketIoClientHelper(): socketDetail.socket.on('connect_error'): [${err}] Address: [${this.#address}]`);
|
|
224
|
-
});
|
|
225
|
-
this.#socket.io.on("reconnect", (attempt) => {
|
|
226
|
-
this.LogErrorMessage(`SocketIoClientHelper(): socketDetail.socket.io.on('reconnect'): Number: [${attempt}] Address: [${this.#address}]`);
|
|
227
|
-
});
|
|
228
|
-
this.#socket.on("connect", () => {
|
|
229
|
-
if (this.#socket) {
|
|
230
|
-
this.LogDebugMessage(`SocketIoClientHelper(): Socket: [${this.#socket.id}]: connected, Address: [${this.#address}]`);
|
|
231
|
-
setTimeout(() => {
|
|
232
|
-
this.ConnectCallBack(this.#socket);
|
|
233
|
-
}, 0);
|
|
234
|
-
this.SocketEventsCallBack(this.#socket);
|
|
235
|
-
} else {
|
|
236
|
-
const errorMessage = "SocketIoClientHelper(): Could not get socket object from socket.io, Address: [${socketDetail.address}]";
|
|
237
|
-
this.LogErrorMessage(errorMessage);
|
|
238
|
-
this.ErrorCallBack(new Error(errorMessage));
|
|
239
|
-
}
|
|
240
|
-
});
|
|
241
|
-
this.#socket.on("disconnect", (reason) => {
|
|
242
|
-
this.LogDebugMessage("SocketIoClientHelper(): socket disconnect: " + reason);
|
|
243
|
-
switch (reason) {
|
|
244
|
-
case "io server disconnect":
|
|
245
|
-
{
|
|
246
|
-
this.LogDebugMessage("SocketIoClientHelper(): The server disconnected using disconnectSockets, i.e. normal safe shutdown from explicit disconnection by the server.");
|
|
247
|
-
this.LogDebugMessage("SocketIoClientHelper(): The connection will be re-established when the server becomes available.");
|
|
248
|
-
this.#socket = void 0;
|
|
249
|
-
if (isNode) {
|
|
250
|
-
if (this.#agentManager) {
|
|
251
|
-
this.#agentManager.ResetAgent();
|
|
252
|
-
}
|
|
253
|
-
setTimeout(() => this.#EstablishSocketConnect(), 100).unref();
|
|
254
|
-
} else {
|
|
255
|
-
setTimeout(() => this.#EstablishSocketConnect(), 100);
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
break;
|
|
259
|
-
case "io client disconnect":
|
|
260
|
-
this.LogDebugMessage("SocketIoClientHelper(): The client disconnected using disconnectSockets, i.e. normal safe disconnection from explicit disconnection by the client.");
|
|
261
|
-
this.LogDebugMessage("SocketIoClientHelper(): The connection will not be re-established automatically.");
|
|
262
|
-
break;
|
|
263
|
-
case "transport close":
|
|
264
|
-
case "ping timeout":
|
|
265
|
-
case "transport error":
|
|
266
|
-
{
|
|
267
|
-
this.LogDebugMessage(`SocketIoClientHelper(): Server unexpectedly disconnected. Reason: [${reason}]`);
|
|
268
|
-
this.LogDebugMessage("SocketIoClientHelper(): The connection will be re-established when the server becomes available.");
|
|
269
|
-
if (this.#socket) {
|
|
270
|
-
this.#socket.disconnect();
|
|
271
|
-
}
|
|
272
|
-
this.#socket = void 0;
|
|
273
|
-
if (isNode) {
|
|
274
|
-
if (this.#agentManager) {
|
|
275
|
-
this.#agentManager?.ResetAgent();
|
|
276
|
-
}
|
|
277
|
-
setTimeout(() => this.#EstablishSocketConnect(), 100).unref();
|
|
278
|
-
} else {
|
|
279
|
-
setTimeout(() => this.#EstablishSocketConnect(), 100);
|
|
280
|
-
}
|
|
281
|
-
}
|
|
282
|
-
break;
|
|
283
|
-
}
|
|
284
|
-
});
|
|
285
|
-
}
|
|
286
|
-
}
|
|
287
|
-
class SocketIoServer extends tinyEmitterExports.TinyEmitter {
|
|
288
|
-
namespace;
|
|
289
|
-
socketionamespace;
|
|
290
|
-
//protected socketionamespace?: Namespace<ClientToServerEvents, ServerToClientEvents, InterServerEvents>
|
|
291
|
-
logger;
|
|
292
|
-
io;
|
|
293
|
-
//#redisClient: RedisClientType | Redis | null = null;
|
|
294
|
-
redisClient;
|
|
295
|
-
listenPort;
|
|
296
|
-
socketIoCustomPath;
|
|
297
|
-
ioRedisMessageProcessorUrl;
|
|
298
|
-
serverClusterMode;
|
|
299
|
-
rooms;
|
|
300
|
-
constructor() {
|
|
301
|
-
super();
|
|
302
|
-
}
|
|
303
|
-
LogErrorMessage(message) {
|
|
304
|
-
if (this.logger) {
|
|
305
|
-
this.logger.error(`${this.namespace}: ${message}`);
|
|
306
|
-
}
|
|
307
|
-
}
|
|
308
|
-
LogDebugMessage(message) {
|
|
309
|
-
if (this.logger) {
|
|
310
|
-
this.logger.debug(`${this.namespace}: ${message}`);
|
|
311
|
-
}
|
|
312
|
-
}
|
|
313
|
-
LogInfoMessage(message) {
|
|
314
|
-
if (this.logger) {
|
|
315
|
-
this.logger.info(`${this.namespace}: ${message}`);
|
|
316
|
-
}
|
|
317
|
-
}
|
|
318
|
-
LogWarnMessage(message) {
|
|
319
|
-
if (this.logger) {
|
|
320
|
-
this.logger.warn(`${this.namespace}: ${message}`);
|
|
321
|
-
}
|
|
322
|
-
}
|
|
323
|
-
// https://socket.io/docs/v4/middlewares/
|
|
324
|
-
// https://socket.io/docs/v4/server-socket-instance/#sockethandshake
|
|
325
|
-
// https://socket.io/docs/v4/server-socket-instance/#socket-middlewares
|
|
326
|
-
// Use this middleward to check every incomming connection
|
|
327
|
-
SetupConnectionMiddleware = () => {
|
|
328
|
-
};
|
|
329
|
-
// Use this middleware to check every packet being received
|
|
330
|
-
SetupMessageMiddleware = (socket) => {
|
|
331
|
-
};
|
|
332
|
-
SocketConnectCallBack = (socket) => {
|
|
333
|
-
};
|
|
334
|
-
SocketEventsCallBack = (socket) => {
|
|
335
|
-
};
|
|
336
|
-
GetConfig = (socketIoServeroptions) => {
|
|
337
|
-
const options = {
|
|
338
|
-
transports: ["websocket"]
|
|
339
|
-
// or [ "websocket", "polling" ] (to use long-poolling. Note that the order matters)
|
|
340
|
-
};
|
|
341
|
-
if (socketIoServeroptions.ioRedisMessageProcessorUrl && socketIoServeroptions.ioRedisMessageProcessorUrl !== "") {
|
|
342
|
-
this.redisClient = new ioredis.Redis(socketIoServeroptions.ioRedisMessageProcessorUrl);
|
|
343
|
-
options.adapter = redisStreamsAdapter.createAdapter(this.redisClient);
|
|
344
|
-
} else if (socketIoServeroptions.serverClusterMode && socketIoServeroptions.serverClusterMode === true) {
|
|
345
|
-
options.adapter = clusterAdapter.createAdapter();
|
|
346
|
-
}
|
|
347
|
-
if (socketIoServeroptions.wssCustomPath && socketIoServeroptions.wssCustomPath.localeCompare("") !== 0) {
|
|
348
|
-
options.path = socketIoServeroptions.wssCustomPath;
|
|
349
|
-
}
|
|
350
|
-
return options;
|
|
351
|
-
};
|
|
352
|
-
SetEngineEvents = () => {
|
|
353
|
-
if (this.io) {
|
|
354
|
-
this.io.engine.on("connection_error", (err) => {
|
|
355
|
-
this.LogInfoMessage(err.req);
|
|
356
|
-
this.LogInfoMessage(err.code);
|
|
357
|
-
this.LogInfoMessage(err.message);
|
|
358
|
-
this.LogInfoMessage(err.context);
|
|
359
|
-
});
|
|
360
|
-
}
|
|
361
|
-
};
|
|
362
|
-
WithLogger = (logger) => {
|
|
363
|
-
this.logger = logger;
|
|
364
|
-
return this;
|
|
365
|
-
};
|
|
366
|
-
WithExistingServer = (server) => {
|
|
367
|
-
if (this.io) {
|
|
368
|
-
throw new Error(`SocketIoServer:AttachServer(): Error: [Server already attached]`);
|
|
369
|
-
}
|
|
370
|
-
this.io = server;
|
|
371
|
-
return this;
|
|
372
|
-
};
|
|
373
|
-
WithListenPort = (listenPort) => {
|
|
374
|
-
this.listenPort = listenPort;
|
|
375
|
-
return this;
|
|
376
|
-
};
|
|
377
|
-
WithSocketIoCustomPath = (socketIoCustomPath) => {
|
|
378
|
-
this.socketIoCustomPath = socketIoCustomPath;
|
|
379
|
-
return this;
|
|
380
|
-
};
|
|
381
|
-
WithIoRedisMessageProcessorUrl = (ioRedisMessageProcessorUrl) => {
|
|
382
|
-
this.ioRedisMessageProcessorUrl = ioRedisMessageProcessorUrl;
|
|
383
|
-
return this;
|
|
384
|
-
};
|
|
385
|
-
WithClusterMode = (serverClusterMode) => {
|
|
386
|
-
this.serverClusterMode = serverClusterMode;
|
|
387
|
-
return this;
|
|
388
|
-
};
|
|
389
|
-
WithRooms = (rooms) => {
|
|
390
|
-
this.rooms = rooms;
|
|
391
|
-
return this;
|
|
392
|
-
};
|
|
393
|
-
WithNamespace = (namespace) => {
|
|
394
|
-
this.namespace = namespace;
|
|
395
|
-
return this;
|
|
396
|
-
};
|
|
397
|
-
StartServer = async () => {
|
|
398
|
-
if (!this.namespace) {
|
|
399
|
-
throw new Error(`SocketIoServer:StartServer(): Error: [namespace not specified]`);
|
|
400
|
-
}
|
|
401
|
-
if (!this.io) {
|
|
402
|
-
if (!this.listenPort) {
|
|
403
|
-
throw new Error(`SocketIoServer:StartServer(): Error: [listenPort not specified]`);
|
|
404
|
-
}
|
|
405
|
-
const options = {
|
|
406
|
-
transports: ["websocket"]
|
|
407
|
-
// or [ "websocket", "polling" ] (to use long-poolling. Note that the order matters)
|
|
408
|
-
};
|
|
409
|
-
if (this.ioRedisMessageProcessorUrl && this.ioRedisMessageProcessorUrl !== "") {
|
|
410
|
-
this.redisClient = new ioredis.Redis(this.ioRedisMessageProcessorUrl);
|
|
411
|
-
options.adapter = redisStreamsAdapter.createAdapter(this.redisClient);
|
|
412
|
-
} else if (this.serverClusterMode && this.serverClusterMode === true) {
|
|
413
|
-
options.adapter = clusterAdapter.createAdapter();
|
|
414
|
-
}
|
|
415
|
-
if (this.socketIoCustomPath && this.socketIoCustomPath.localeCompare("") !== 0) {
|
|
416
|
-
options.path = this.socketIoCustomPath;
|
|
417
|
-
}
|
|
418
|
-
this.io = new socket_io.Server(this.listenPort, options);
|
|
419
|
-
this.SetEngineEvents();
|
|
420
|
-
await stsutils.Sleep(500);
|
|
421
|
-
}
|
|
422
|
-
let autoJoinRooms;
|
|
423
|
-
let rooms;
|
|
424
|
-
if (this.rooms && this.rooms.length > 0) {
|
|
425
|
-
autoJoinRooms = true;
|
|
426
|
-
rooms = [...this.rooms];
|
|
427
|
-
} else {
|
|
428
|
-
autoJoinRooms = false;
|
|
429
|
-
rooms = [];
|
|
430
|
-
}
|
|
431
|
-
this.SetupNamespace(rooms, autoJoinRooms);
|
|
432
|
-
return this;
|
|
433
|
-
};
|
|
434
|
-
AttachServer = async (server) => {
|
|
435
|
-
if (this.io) {
|
|
436
|
-
throw new Error(`SocketIoServer:AttachServer(): Error: [Server already attached]`);
|
|
437
|
-
}
|
|
438
|
-
this.io = server;
|
|
439
|
-
};
|
|
440
|
-
StopServer = async () => {
|
|
441
|
-
if (this.io) {
|
|
442
|
-
await this.CloseNamespaceAdaptors();
|
|
443
|
-
this.DisconnectNamespaceSockets();
|
|
444
|
-
await this.io.of("/").adapter.close();
|
|
445
|
-
if (this.redisClient) {
|
|
446
|
-
await this.redisClient.disconnect();
|
|
447
|
-
await stsutils.Sleep(50);
|
|
448
|
-
}
|
|
449
|
-
this.io.disconnectSockets();
|
|
450
|
-
this.io = void 0;
|
|
451
|
-
}
|
|
452
|
-
};
|
|
453
|
-
LeaveRoom = (socket, room) => {
|
|
454
|
-
this.LogDebugMessage(`Leaving room [${room}]`);
|
|
455
|
-
socket.leave(room);
|
|
456
|
-
};
|
|
457
|
-
JoinRoom = (socket, room) => {
|
|
458
|
-
this.LogDebugMessage(`Socket joining room [${room}], ID: [${socket.id}]`);
|
|
459
|
-
socket.join(room);
|
|
460
|
-
};
|
|
461
|
-
SetupStandardEvents = (socket) => {
|
|
462
|
-
socket.on("disconnect", (reason) => {
|
|
463
|
-
this.LogDebugMessage(`socket disconnect, ID: [${socket.id}] [${reason}]`);
|
|
464
|
-
});
|
|
465
|
-
socket.on("disconnecting", (reason) => {
|
|
466
|
-
this.LogDebugMessage(`socket disconnecting, ID: [${socket.id}] [${reason}]`);
|
|
467
|
-
});
|
|
468
|
-
socket.on("error", (error) => {
|
|
469
|
-
this.LogDebugMessage(`socket error, ID: [${socket.id}] [${error}]`);
|
|
470
|
-
});
|
|
471
|
-
socket.on("__STSdisconnect", (reason) => {
|
|
472
|
-
this.LogDebugMessage(`__STSdisconnect: socket disconnect, ID: [${socket.id}] [${reason}]`);
|
|
473
|
-
});
|
|
474
|
-
socket.on("__STSdisconnecting", (reason, callBackResult) => {
|
|
475
|
-
this.LogDebugMessage(`__STSdisconnecting: socket disconnecting, ID: [${socket.id}] [${reason}]`);
|
|
476
|
-
callBackResult("__STSdisconnecting accepted by server.");
|
|
477
|
-
});
|
|
478
|
-
socket.on("__STSjoinRoom", (rooms) => {
|
|
479
|
-
rooms.forEach((room) => {
|
|
480
|
-
this.JoinRoom(socket, room);
|
|
481
|
-
});
|
|
482
|
-
});
|
|
483
|
-
socket.on("__STSleaveRoom", (rooms) => {
|
|
484
|
-
rooms.forEach((room) => {
|
|
485
|
-
this.LeaveRoom(socket, room);
|
|
486
|
-
});
|
|
487
|
-
});
|
|
488
|
-
socket.on("__STSsendToRoom", (rooms, payload) => {
|
|
489
|
-
rooms.forEach((room) => {
|
|
490
|
-
if (this.socketionamespace) {
|
|
491
|
-
this.LogDebugMessage(`socket.on: __STSsendToRoom: Sending to room [${room}], ID: [${socket.id}]`);
|
|
492
|
-
this.socketionamespace.to(room).emit(payload.command, payload);
|
|
493
|
-
}
|
|
494
|
-
});
|
|
495
|
-
});
|
|
496
|
-
socket.on("__STSsendToRoomWithCallback", (room, timeout, payload, cb) => {
|
|
497
|
-
if (this.socketionamespace) {
|
|
498
|
-
this.socketionamespace.to(room).timeout(timeout).emit(payload.command, payload, (err, dataResponse) => {
|
|
499
|
-
if (err) {
|
|
500
|
-
console.error(err);
|
|
501
|
-
const errorResponse = {
|
|
502
|
-
error: true,
|
|
503
|
-
errorName: err.name,
|
|
504
|
-
errorMessage: err.message
|
|
505
|
-
//errorCause: err.cause,
|
|
506
|
-
//errorStack: err.stack
|
|
507
|
-
};
|
|
508
|
-
this.LogErrorMessage(`__STSsendToRoomWithCallback broadcast (error): [${JSON.stringify(errorResponse)}]`);
|
|
509
|
-
cb(errorResponse);
|
|
510
|
-
} else {
|
|
511
|
-
this.LogDebugMessage(`__STSsendToRoomWithCallback broadcast: [${JSON.stringify(dataResponse)}]`);
|
|
512
|
-
cb(dataResponse);
|
|
513
|
-
}
|
|
514
|
-
});
|
|
515
|
-
}
|
|
516
|
-
});
|
|
517
|
-
socket.on("__STSsendToRoomsWithCallback", (rooms, timeout, payload, cb) => {
|
|
518
|
-
const responses = [];
|
|
519
|
-
const timeoutForComplete = setTimeout(() => {
|
|
520
|
-
responses.push({
|
|
521
|
-
room: "",
|
|
522
|
-
responses: {
|
|
523
|
-
error: true,
|
|
524
|
-
errorName: "timeout",
|
|
525
|
-
errorMessage: `__STSsendToRoomsWithCallback timeout: [${timeout}] before all responses received`
|
|
526
|
-
}
|
|
527
|
-
});
|
|
528
|
-
cb(responses);
|
|
529
|
-
}, timeout).unref();
|
|
530
|
-
rooms.forEach((room) => {
|
|
531
|
-
if (this.socketionamespace) {
|
|
532
|
-
this.socketionamespace.to(room).timeout(timeout).emit(payload.command, payload, (err, dataResponse) => {
|
|
533
|
-
if (err) {
|
|
534
|
-
console.error(err);
|
|
535
|
-
const errorResponse = {
|
|
536
|
-
error: true,
|
|
537
|
-
errorName: err.name,
|
|
538
|
-
errorMessage: err.message
|
|
539
|
-
//errorCause: err.cause,
|
|
540
|
-
//errorStack: err.stack
|
|
541
|
-
};
|
|
542
|
-
this.LogErrorMessage(`__STSsendToRoomWithCallback broadcast (error): [${JSON.stringify(errorResponse)}]`);
|
|
543
|
-
responses.push({
|
|
544
|
-
room,
|
|
545
|
-
responses: errorResponse
|
|
546
|
-
});
|
|
547
|
-
} else {
|
|
548
|
-
this.LogDebugMessage(`__STSsendToRoomWithCallback broadcast: [${JSON.stringify(dataResponse)}]`);
|
|
549
|
-
responses.push({
|
|
550
|
-
room,
|
|
551
|
-
responses: dataResponse
|
|
552
|
-
});
|
|
553
|
-
}
|
|
554
|
-
if (responses.length === rooms.length) {
|
|
555
|
-
clearTimeout(timeoutForComplete);
|
|
556
|
-
cb(responses);
|
|
557
|
-
}
|
|
558
|
-
});
|
|
559
|
-
}
|
|
560
|
-
});
|
|
561
|
-
});
|
|
562
|
-
};
|
|
563
|
-
SetupNamespace = (rooms, autoJoinRooms) => {
|
|
564
|
-
if (this.io) {
|
|
565
|
-
this.socketionamespace = this.io.of(`/${this.namespace}/`);
|
|
566
|
-
this.SetupConnectionMiddleware();
|
|
567
|
-
this.socketionamespace.on("connection", (socket) => {
|
|
568
|
-
this.LogDebugMessage(`Socket connected, ID: [${socket.id}]`);
|
|
569
|
-
this.LogDebugMessage(`Authentication Handshake (auth): [${JSON.stringify(socket.handshake.auth)}]`);
|
|
570
|
-
this.LogDebugMessage(`Authentication Handshake (host): [${JSON.stringify(socket.handshake.headers.host)}]`);
|
|
571
|
-
this.LogDebugMessage(`Authentication Handshake (url): [${JSON.stringify(socket.handshake.url)}]`);
|
|
572
|
-
this.LogDebugMessage(`Authentication Handshake: [${JSON.stringify(socket.handshake)}]`);
|
|
573
|
-
this.SetupMessageMiddleware(socket);
|
|
574
|
-
if (autoJoinRooms) {
|
|
575
|
-
rooms.map((room) => {
|
|
576
|
-
this.JoinRoom(socket, room);
|
|
577
|
-
});
|
|
578
|
-
}
|
|
579
|
-
this.SetupStandardEvents(socket);
|
|
580
|
-
setTimeout(() => {
|
|
581
|
-
this.SocketConnectCallBack(socket);
|
|
582
|
-
}, 0);
|
|
583
|
-
this.SocketEventsCallBack(socket);
|
|
584
|
-
});
|
|
585
|
-
} else {
|
|
586
|
-
throw new Error("SocketIoServer:SetupNamespace(): Error: [No server attached");
|
|
587
|
-
}
|
|
588
|
-
return this;
|
|
589
|
-
};
|
|
590
|
-
CloseNamespaceAdaptors = async () => {
|
|
591
|
-
if (this.socketionamespace) {
|
|
592
|
-
await this.socketionamespace.adapter.close();
|
|
593
|
-
}
|
|
594
|
-
};
|
|
595
|
-
DisconnectNamespaceSockets = () => {
|
|
596
|
-
if (this.socketionamespace) {
|
|
597
|
-
this.socketionamespace.disconnectSockets();
|
|
598
|
-
this.socketionamespace = void 0;
|
|
599
|
-
}
|
|
600
|
-
};
|
|
601
|
-
}
|
|
602
|
-
exports2.STSEvent = STSEvent;
|
|
603
|
-
exports2.STSNamespace = STSNamespace;
|
|
604
|
-
exports2.STSRoom = STSRoom;
|
|
605
|
-
exports2.SocketIoClient = SocketIoClient;
|
|
606
|
-
exports2.SocketIoServer = SocketIoServer;
|
|
607
|
-
Object.defineProperty(exports2, Symbol.toStringTag, { value: "Module" });
|
|
608
|
-
}));
|
|
609
|
-
//# sourceMappingURL=stssocketioutils.umd.js.map
|