@futdevpro/nts-dynamo 1.7.23 → 1.7.25
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/.eslintrc.json +1 -1
- package/lib/_enums/http/socket-event-type.enum.d.ts +1 -0
- package/lib/_enums/http/socket-event-type.enum.d.ts.map +1 -1
- package/lib/_enums/http/socket-event-type.enum.js +1 -0
- package/lib/_enums/http/socket-event-type.enum.js.map +1 -1
- package/lib/_models/control-models/socket-event.control-model.d.ts.map +1 -1
- package/lib/_models/control-models/socket-event.control-model.js +4 -2
- package/lib/_models/control-models/socket-event.control-model.js.map +1 -1
- package/lib/_models/control-models/socket-presence.control-model.d.ts.map +1 -1
- package/lib/_models/control-models/socket-presence.control-model.js +4 -2
- package/lib/_models/control-models/socket-presence.control-model.js.map +1 -1
- package/lib/_services/socket/socket-server.service.d.ts +9 -0
- package/lib/_services/socket/socket-server.service.d.ts.map +1 -1
- package/lib/_services/socket/socket-server.service.js +76 -16
- package/lib/_services/socket/socket-server.service.js.map +1 -1
- package/lib/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/src/_enums/http/socket-event-type.enum.ts +1 -0
- package/src/_models/control-models/socket-event.control-model.ts +22 -8
- package/src/_models/control-models/socket-presence.control-model.ts +19 -9
- package/src/_services/socket/socket-server.service.ts +292 -80
|
@@ -1,15 +1,23 @@
|
|
|
1
1
|
|
|
2
2
|
import * as SocketIO from 'socket.io';
|
|
3
3
|
|
|
4
|
-
import {
|
|
4
|
+
import {
|
|
5
|
+
Dynamo_AnyError, Dynamo_Array, Dynamo_Error, Dynamo_Error_Settings, Dynamo_Log
|
|
6
|
+
} from '@futdevpro/fsm-dynamo';
|
|
5
7
|
|
|
6
8
|
import { DynamoNTS_SingletonService } from '../base/singleton.service';
|
|
7
9
|
import { dynamoNTS_globalSettings } from '../../_constants/global-settings.const';
|
|
8
10
|
import { DynamoNTS_SocketEventKey } from '../../_enums/http/socket-event-type.enum';
|
|
9
11
|
import { DynamoNTS_SocketSecurity } from '../../_enums/socket-security.enum';
|
|
10
|
-
import {
|
|
11
|
-
|
|
12
|
-
|
|
12
|
+
import {
|
|
13
|
+
DynamoNTS_SocketServerServiceParams
|
|
14
|
+
} from '../../_models/control-models/socket-server-service-params.control-model';
|
|
15
|
+
import {
|
|
16
|
+
DynamoNTS_SocketPresence
|
|
17
|
+
} from '../../_models/control-models/socket-presence.control-model';
|
|
18
|
+
import {
|
|
19
|
+
DynamoNTS_SocketEvent, DynamoNTS_SocketEventPreprocessTask, DynamoNTS_SocketEventTask
|
|
20
|
+
} from '../../_models/control-models/socket-event.control-model';
|
|
13
21
|
|
|
14
22
|
/**
|
|
15
23
|
* You need to define the following functions:
|
|
@@ -43,12 +51,15 @@ export abstract class DynamoNTS_SocketServerService<
|
|
|
43
51
|
protected incomingEvents: DynamoNTS_SocketEvent<any>[];
|
|
44
52
|
|
|
45
53
|
private subscriptionEvent: DynamoNTS_SocketEvent<any>;
|
|
54
|
+
private unsubscribeEvent: DynamoNTS_SocketEvent<any>;
|
|
46
55
|
|
|
47
56
|
/**
|
|
48
57
|
* You need to setup a function that converts subscription event content to presence.
|
|
49
58
|
* You should do authentication and authorization here.
|
|
50
59
|
*/
|
|
51
|
-
protected abstract getPresenceFromSubscrioptionEventContent(
|
|
60
|
+
protected abstract getPresenceFromSubscrioptionEventContent(
|
|
61
|
+
socketSubscription: T_SubscriptionContent, socket: SocketIO.Socket
|
|
62
|
+
): Promise<T_Presence>;
|
|
52
63
|
protected presences: T_Presence[] = [];
|
|
53
64
|
|
|
54
65
|
protected logSetup: boolean;
|
|
@@ -62,7 +73,10 @@ export abstract class DynamoNTS_SocketServerService<
|
|
|
62
73
|
super();
|
|
63
74
|
|
|
64
75
|
this.asyncConstructor().catch((error: any) => {
|
|
65
|
-
Dynamo_Log.error(
|
|
76
|
+
Dynamo_Log.error(
|
|
77
|
+
`Socket Server Service setup failed: ${this.params?.name} (${this.params?.port})`,
|
|
78
|
+
error
|
|
79
|
+
);
|
|
66
80
|
});
|
|
67
81
|
}
|
|
68
82
|
|
|
@@ -74,9 +88,16 @@ export abstract class DynamoNTS_SocketServerService<
|
|
|
74
88
|
|
|
75
89
|
await this.prepareEvents();
|
|
76
90
|
|
|
77
|
-
if (this.logSetup) console.log(
|
|
91
|
+
if (this.logSetup) console.log(
|
|
92
|
+
`Socket Server Service setup done: ${this.params.name} (${this.params.port}) ` +
|
|
93
|
+
`serurity: ${this.params.security}\n`
|
|
94
|
+
);
|
|
78
95
|
} catch (error) {
|
|
79
|
-
Dynamo_Log.error(
|
|
96
|
+
Dynamo_Log.error(
|
|
97
|
+
`Socket Server Service setup failed: ${this.params?.name} (${this.params?.port})`,
|
|
98
|
+
error
|
|
99
|
+
);
|
|
100
|
+
|
|
80
101
|
throw new Dynamo_Error({
|
|
81
102
|
errorCode: 'NTS-SSS-001',
|
|
82
103
|
error: error,
|
|
@@ -90,7 +111,11 @@ export abstract class DynamoNTS_SocketServerService<
|
|
|
90
111
|
private async prepareEvents(): Promise<void> {
|
|
91
112
|
try {
|
|
92
113
|
/** Setup connect event */
|
|
93
|
-
const connectEventIndex = this.incomingEvents.findIndex(
|
|
114
|
+
const connectEventIndex = this.incomingEvents.findIndex(
|
|
115
|
+
(event: DynamoNTS_SocketEvent<any>) =>
|
|
116
|
+
event.eventKey === DynamoNTS_SocketEventKey.connection
|
|
117
|
+
);
|
|
118
|
+
|
|
94
119
|
if (0 <= connectEventIndex) {
|
|
95
120
|
this.connectEvent = this.incomingEvents.splice(connectEventIndex)[0];
|
|
96
121
|
} else {
|
|
@@ -100,31 +125,70 @@ export abstract class DynamoNTS_SocketServerService<
|
|
|
100
125
|
}
|
|
101
126
|
this.connectEvent.serviceName = this.params.name;
|
|
102
127
|
|
|
103
|
-
/** Setup
|
|
128
|
+
/** Setup subscriptions events */
|
|
104
129
|
if (this.getPresenceFromSubscrioptionEventContent) {
|
|
105
|
-
|
|
130
|
+
/** Setup subscription event */
|
|
131
|
+
const subscriptionEventIndex = this.incomingEvents.findIndex(
|
|
132
|
+
(event: DynamoNTS_SocketEvent<any>) =>
|
|
133
|
+
event.eventKey === DynamoNTS_SocketEventKey.subscribe
|
|
134
|
+
);
|
|
135
|
+
|
|
106
136
|
if (0 <= subscriptionEventIndex) {
|
|
107
137
|
this.subscriptionEvent = this.incomingEvents.splice(subscriptionEventIndex)[0];
|
|
108
138
|
Dynamo_Log.error(
|
|
109
139
|
`You should not set the subscription event, but the subscriptions tasks, ` +
|
|
110
140
|
`in case you need additional steps for your subscriptions.`,
|
|
111
|
-
`${this.params.name} (${this.params.port})
|
|
141
|
+
`${this.params.name} (${this.params.port})`
|
|
112
142
|
);
|
|
113
143
|
} else {
|
|
114
144
|
this.subscriptionEvent = new DynamoNTS_SocketEvent<T_SubscriptionContent>({
|
|
115
|
-
eventKey: DynamoNTS_SocketEventKey.subscribe
|
|
145
|
+
eventKey: DynamoNTS_SocketEventKey.subscribe,
|
|
116
146
|
});
|
|
117
147
|
}
|
|
148
|
+
|
|
118
149
|
if (this.getSubscriptionPreProcessess) {
|
|
119
150
|
this.subscriptionEvent.preProcessess.push(...this.getSubscriptionPreProcessess());
|
|
120
151
|
}
|
|
152
|
+
|
|
121
153
|
if (this.getSubscriptionTasks) {
|
|
122
154
|
this.subscriptionEvent.tasks.push(...this.getSubscriptionTasks());
|
|
123
155
|
}
|
|
124
156
|
this.subscriptionEvent.serviceName = this.params.name;
|
|
157
|
+
|
|
158
|
+
/** Setup subscription event */
|
|
159
|
+
const unsubscribeEventIndex = this.incomingEvents.findIndex(
|
|
160
|
+
(event: DynamoNTS_SocketEvent<any>) =>
|
|
161
|
+
event.eventKey === DynamoNTS_SocketEventKey.unsubscribe
|
|
162
|
+
);
|
|
163
|
+
|
|
164
|
+
if (0 <= unsubscribeEventIndex) {
|
|
165
|
+
this.unsubscribeEvent = this.incomingEvents.splice(unsubscribeEventIndex)[0];
|
|
166
|
+
Dynamo_Log.error(
|
|
167
|
+
`You should not set the unsubscribe event, but the unsubscribe tasks, ` +
|
|
168
|
+
`in case you need additional steps for your unsubscribe.`,
|
|
169
|
+
`${this.params.name} (${this.params.port})`
|
|
170
|
+
);
|
|
171
|
+
} else {
|
|
172
|
+
this.unsubscribeEvent = new DynamoNTS_SocketEvent<T_SubscriptionContent>({
|
|
173
|
+
eventKey: DynamoNTS_SocketEventKey.unsubscribe,
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
if (this.getUnsubscribePreProcessess) {
|
|
178
|
+
this.unsubscribeEvent.preProcessess.push(...this.getUnsubscribePreProcessess());
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
if (this.getUnsubscribeTasks) {
|
|
182
|
+
this.unsubscribeEvent.tasks.push(...this.getUnsubscribeTasks());
|
|
183
|
+
}
|
|
184
|
+
this.unsubscribeEvent.serviceName = this.params.name;
|
|
125
185
|
|
|
126
186
|
/** Setup disconnect event */
|
|
127
|
-
const disconnectEventIndex = this.incomingEvents.findIndex(
|
|
187
|
+
const disconnectEventIndex = this.incomingEvents.findIndex(
|
|
188
|
+
(event: DynamoNTS_SocketEvent<any>) =>
|
|
189
|
+
event.eventKey === DynamoNTS_SocketEventKey.disconnect
|
|
190
|
+
);
|
|
191
|
+
|
|
128
192
|
if (0 <= disconnectEventIndex) {
|
|
129
193
|
this.incomingEvents[disconnectEventIndex].serviceName = this.params.name;
|
|
130
194
|
this.incomingEvents[disconnectEventIndex].tasks.push(this.disconnectBaseTask);
|
|
@@ -134,27 +198,33 @@ export abstract class DynamoNTS_SocketServerService<
|
|
|
134
198
|
serviceName: this.params.name,
|
|
135
199
|
eventKey: DynamoNTS_SocketEventKey.disconnect,
|
|
136
200
|
tasks: [
|
|
137
|
-
this.disconnectBaseTask
|
|
138
|
-
]
|
|
201
|
+
this.disconnectBaseTask,
|
|
202
|
+
],
|
|
139
203
|
})
|
|
140
204
|
);
|
|
141
205
|
}
|
|
142
206
|
|
|
143
207
|
/** Setup error event */
|
|
144
|
-
const errorEventIndex = this.incomingEvents.findIndex(
|
|
208
|
+
const errorEventIndex = this.incomingEvents.findIndex(
|
|
209
|
+
(event: DynamoNTS_SocketEvent<any>) => event.eventKey === DynamoNTS_SocketEventKey.error
|
|
210
|
+
);
|
|
211
|
+
|
|
145
212
|
if (errorEventIndex == -1 && this.errorHandling) {
|
|
146
213
|
this.incomingEvents.push(new DynamoNTS_SocketEvent<T_ErrorContent>({
|
|
147
214
|
serviceName: this.params.name,
|
|
148
215
|
eventKey: DynamoNTS_SocketEventKey.error,
|
|
149
216
|
tasks: [
|
|
150
|
-
this.errorHandling
|
|
151
|
-
]
|
|
217
|
+
this.errorHandling,
|
|
218
|
+
],
|
|
152
219
|
}));
|
|
153
220
|
}
|
|
154
221
|
|
|
155
222
|
this.subscriptionEvent.socketName = this.params.name;
|
|
156
223
|
} else {
|
|
157
|
-
Dynamo_Log.error(
|
|
224
|
+
Dynamo_Log.error(
|
|
225
|
+
'getPresenceFromSubscrioptionEventContent is not set',
|
|
226
|
+
`${this.params.name} (${this.params.port})`
|
|
227
|
+
);
|
|
158
228
|
}
|
|
159
229
|
|
|
160
230
|
/** Set incoming events serviceName */
|
|
@@ -162,12 +232,22 @@ export abstract class DynamoNTS_SocketServerService<
|
|
|
162
232
|
event.serviceName = this.params.name;
|
|
163
233
|
});
|
|
164
234
|
} catch (error) {
|
|
165
|
-
Dynamo_Log.error(
|
|
235
|
+
Dynamo_Log.error(
|
|
236
|
+
`Socket Server Service - Event Preparation setup failed: ` +
|
|
237
|
+
`${this.params?.name} (${this.params?.port})`,
|
|
238
|
+
error
|
|
239
|
+
);
|
|
240
|
+
|
|
166
241
|
throw error;
|
|
167
242
|
}
|
|
168
243
|
}
|
|
169
244
|
|
|
170
|
-
|
|
245
|
+
// eslint-disable-next-line max-lines-per-function
|
|
246
|
+
async setupSocketServer(
|
|
247
|
+
newSocketServer: SocketIO.Server,
|
|
248
|
+
security: DynamoNTS_SocketSecurity.open | DynamoNTS_SocketSecurity.secure,
|
|
249
|
+
successCallback: () => void
|
|
250
|
+
): Promise<SocketIO.Server> {
|
|
171
251
|
try {
|
|
172
252
|
if (this.logFn) console.log('\nfn:. setupSocketServer');
|
|
173
253
|
|
|
@@ -178,61 +258,142 @@ export abstract class DynamoNTS_SocketServerService<
|
|
|
178
258
|
await this.connectEvent.executeEventTasks(socket);
|
|
179
259
|
|
|
180
260
|
if (this.getPresenceFromSubscrioptionEventContent) {
|
|
181
|
-
socket.on(
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
261
|
+
socket.on(
|
|
262
|
+
DynamoNTS_SocketEventKey.subscribe,
|
|
263
|
+
async (content: any) => {
|
|
264
|
+
try {
|
|
265
|
+
/**
|
|
266
|
+
* usually socket logs are in event.executeEventTasks(),
|
|
267
|
+
* but subscribe event is an exception from this,
|
|
268
|
+
* to be able to check content before getPresenceFromSubscrioptionEventContent
|
|
269
|
+
*/
|
|
270
|
+
if (dynamoNTS_globalSettings.logSocketEventContent) {
|
|
271
|
+
Dynamo_Log.log(
|
|
272
|
+
`--=> incoming socket(${this.params.name}) ` +
|
|
273
|
+
`event: ${DynamoNTS_SocketEventKey.subscribe};` +
|
|
274
|
+
`\ncontent:`, content
|
|
275
|
+
);
|
|
276
|
+
} else {
|
|
277
|
+
Dynamo_Log.log(
|
|
278
|
+
`--=> incoming socket(${this.params.name}) ` +
|
|
279
|
+
`event: ${DynamoNTS_SocketEventKey.subscribe}`
|
|
280
|
+
);
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
const presence: T_Presence = await this.getPresenceFromSubscrioptionEventContent(
|
|
284
|
+
content, socket
|
|
192
285
|
);
|
|
193
|
-
} else {
|
|
194
|
-
Dynamo_Log.log(`--=> incoming socket(${this.params.name}) event: ${DynamoNTS_SocketEventKey.subscribe}`);
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
const presence: T_Presence = await this.getPresenceFromSubscrioptionEventContent(content, socket);
|
|
198
|
-
presence.serviceName = this.params.name;
|
|
199
|
-
issuer = presence.issuerId;
|
|
200
|
-
|
|
201
|
-
this.addSocketToPresence(presence);
|
|
202
|
-
await this.subscriptionEvent.executeEventTasks(content, issuer);
|
|
203
286
|
|
|
204
|
-
|
|
287
|
+
presence.serviceName = this.params.name;
|
|
288
|
+
issuer = presence.issuerId;
|
|
289
|
+
|
|
290
|
+
this.addSocketToPresence(presence);
|
|
291
|
+
await this.subscriptionEvent.executeEventTasks(content, issuer);
|
|
292
|
+
|
|
293
|
+
socket.emit(
|
|
294
|
+
DynamoNTS_SocketEventKey.subscriptionSuccessful,
|
|
295
|
+
'subscribe was successful',
|
|
296
|
+
error => {
|
|
297
|
+
Dynamo_Log.error(
|
|
298
|
+
`Emitting subscriptionSuccessful event failed!` +
|
|
299
|
+
`\nerror:`, error
|
|
300
|
+
);
|
|
301
|
+
}
|
|
302
|
+
);
|
|
303
|
+
|
|
304
|
+
Dynamo_Log.success(
|
|
305
|
+
`<-==> socket(${this.params.name}) subscription successfull (${issuer})`
|
|
306
|
+
);
|
|
307
|
+
} catch (error) {
|
|
205
308
|
Dynamo_Log.error(
|
|
206
|
-
`
|
|
207
|
-
|
|
309
|
+
`Socket Subscription failed: ${this.params.name} ` ,
|
|
310
|
+
`(${this.params.port}) will disconnect now...`,
|
|
311
|
+
error
|
|
208
312
|
);
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
} catch (error) {
|
|
213
|
-
Dynamo_Log.error(`Socket Subscription failed: ${this.params.name} (${this.params.port}) will disconnect now...`, error);
|
|
214
|
-
socket.emit(DynamoNTS_SocketEventKey.error, error);
|
|
215
|
-
socket.disconnect();
|
|
313
|
+
socket.emit(DynamoNTS_SocketEventKey.error, error);
|
|
314
|
+
socket.disconnect();
|
|
315
|
+
}
|
|
216
316
|
}
|
|
217
|
-
|
|
317
|
+
);
|
|
318
|
+
|
|
319
|
+
socket.on(
|
|
320
|
+
DynamoNTS_SocketEventKey.unsubscribe,
|
|
321
|
+
async (issuer: string) => {
|
|
322
|
+
try {
|
|
323
|
+
/**
|
|
324
|
+
* usually socket logs are in event.executeEventTasks(),
|
|
325
|
+
* but unsubscribe event is an exception from this,
|
|
326
|
+
* to be able to check content before getPresenceFromSubscrioptionEventContent
|
|
327
|
+
*/
|
|
328
|
+
if (dynamoNTS_globalSettings.logSocketEventContent) {
|
|
329
|
+
Dynamo_Log.log(
|
|
330
|
+
`--=> incoming socket(${this.params.name}) ` +
|
|
331
|
+
`event: ${DynamoNTS_SocketEventKey.unsubscribe};` +
|
|
332
|
+
`\ncontent:`, issuer
|
|
333
|
+
);
|
|
334
|
+
} else {
|
|
335
|
+
Dynamo_Log.log(
|
|
336
|
+
`--=> incoming socket(${this.params.name}) ` +
|
|
337
|
+
`event: ${DynamoNTS_SocketEventKey.unsubscribe}`
|
|
338
|
+
);
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
this.removeSubscription(socket, issuer);
|
|
342
|
+
|
|
343
|
+
await this.subscriptionEvent.executeEventTasks(issuer, issuer);
|
|
344
|
+
|
|
345
|
+
socket.emit(
|
|
346
|
+
DynamoNTS_SocketEventKey.unsubscribeSuccessful,
|
|
347
|
+
'unsubscribe was successful',
|
|
348
|
+
error => {
|
|
349
|
+
Dynamo_Log.error(
|
|
350
|
+
`Emitting unsubscribeSuccessful event failed!` +
|
|
351
|
+
`\nerror:`, error
|
|
352
|
+
);
|
|
353
|
+
}
|
|
354
|
+
);
|
|
355
|
+
|
|
356
|
+
Dynamo_Log.success(
|
|
357
|
+
`< x > socket(${this.params.name}) unsubscribe successfull (${issuer})`
|
|
358
|
+
);
|
|
359
|
+
} catch (error) {
|
|
360
|
+
Dynamo_Log.error(
|
|
361
|
+
`Socket unsubscribe failed: ${this.params.name} ` ,
|
|
362
|
+
`(${this.params.port}) will disconnect now...`,
|
|
363
|
+
error
|
|
364
|
+
);
|
|
365
|
+
socket.emit(DynamoNTS_SocketEventKey.error, error);
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
);
|
|
218
369
|
}
|
|
219
370
|
|
|
220
371
|
this.incomingEvents.forEach((event: DynamoNTS_SocketEvent<any>) => {
|
|
221
|
-
socket.on(
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
372
|
+
socket.on(
|
|
373
|
+
event.eventKey,
|
|
374
|
+
async (content: any) => {
|
|
375
|
+
try {
|
|
376
|
+
await event.executeEventTasks(content, issuer);
|
|
377
|
+
} catch (error) {
|
|
378
|
+
Dynamo_Log.error(
|
|
379
|
+
`Socket Event failed: ${this.params.name} (${this.params.port})`,
|
|
380
|
+
error
|
|
381
|
+
);
|
|
382
|
+
socket.emit(DynamoNTS_SocketEventKey.error, error);
|
|
383
|
+
}
|
|
227
384
|
}
|
|
228
|
-
|
|
385
|
+
);
|
|
229
386
|
});
|
|
230
387
|
|
|
231
388
|
if (dynamoNTS_globalSettings.logMainSocketEvents) {
|
|
232
389
|
Dynamo_Log.success(`< > socket(${this.params.name}): new CONNECTION established`);
|
|
233
390
|
}
|
|
234
391
|
} catch (error) {
|
|
235
|
-
Dynamo_Log.error(
|
|
392
|
+
Dynamo_Log.error(
|
|
393
|
+
`Socket Connection failed: ${this.params.name} ` +
|
|
394
|
+
`(${this.params.port}) will disconnect now...`,
|
|
395
|
+
error
|
|
396
|
+
);
|
|
236
397
|
socket.emit(DynamoNTS_SocketEventKey.error, error);
|
|
237
398
|
socket.disconnect();
|
|
238
399
|
}
|
|
@@ -249,20 +410,29 @@ export abstract class DynamoNTS_SocketServerService<
|
|
|
249
410
|
|
|
250
411
|
if (security === DynamoNTS_SocketSecurity.open) {
|
|
251
412
|
this.openSocketServer = newSocketServer;
|
|
413
|
+
|
|
252
414
|
return this.openSocketServer;
|
|
253
415
|
} else {
|
|
254
416
|
this.secureSocketServer = newSocketServer;
|
|
417
|
+
|
|
255
418
|
return this.secureSocketServer;
|
|
256
419
|
}
|
|
257
420
|
} catch (error) {
|
|
258
|
-
Dynamo_Log.error(
|
|
421
|
+
Dynamo_Log.error(
|
|
422
|
+
`Socket Server Service - Deep Setup failed: ${this.params?.name} ` +
|
|
423
|
+
`(${this.params?.port})`,
|
|
424
|
+
error
|
|
425
|
+
);
|
|
426
|
+
|
|
259
427
|
throw error;
|
|
260
428
|
}
|
|
261
429
|
}
|
|
262
430
|
|
|
263
431
|
protected async addSocketToPresence(newPresence: T_Presence): Promise<void> {
|
|
264
432
|
try {
|
|
265
|
-
const activePresence: T_Presence = this.presences.find(
|
|
433
|
+
const activePresence: T_Presence = this.presences.find(
|
|
434
|
+
(pres: T_Presence) => pres.issuerId === newPresence.issuerId
|
|
435
|
+
);
|
|
266
436
|
|
|
267
437
|
if (activePresence) {
|
|
268
438
|
activePresence.sockets.push(...newPresence.sockets);
|
|
@@ -270,7 +440,11 @@ export abstract class DynamoNTS_SocketServerService<
|
|
|
270
440
|
this.presences.push(newPresence);
|
|
271
441
|
}
|
|
272
442
|
} catch (error) {
|
|
273
|
-
Dynamo_Log.error(
|
|
443
|
+
Dynamo_Log.error(
|
|
444
|
+
`Socket Subscription failed: ${this.params?.name} (${this.params?.port})`,
|
|
445
|
+
error
|
|
446
|
+
);
|
|
447
|
+
|
|
274
448
|
throw error;
|
|
275
449
|
}
|
|
276
450
|
}
|
|
@@ -285,23 +459,27 @@ export abstract class DynamoNTS_SocketServerService<
|
|
|
285
459
|
...this._getDefaultErrorSettings(
|
|
286
460
|
'disconnectBaseTask',
|
|
287
461
|
error,
|
|
288
|
-
issuer
|
|
462
|
+
issuer
|
|
289
463
|
),
|
|
290
464
|
|
|
291
|
-
errorCode: 'NTS-SSS-109'
|
|
465
|
+
errorCode: 'NTS-SSS-109',
|
|
292
466
|
});
|
|
293
467
|
}
|
|
294
468
|
}
|
|
295
469
|
|
|
296
470
|
private async removeSubscription(socket: SocketIO.Socket, issuer?: string): Promise<void> {
|
|
297
471
|
try {
|
|
298
|
-
const activePresenceIndex: number = this.presences.findIndex(
|
|
472
|
+
const activePresenceIndex: number = this.presences.findIndex(
|
|
473
|
+
(pres: DynamoNTS_SocketPresence) => pres.sockets.includes(socket)
|
|
474
|
+
);
|
|
299
475
|
|
|
300
476
|
if (activePresenceIndex === -1) {
|
|
301
477
|
throw new Dynamo_Error({
|
|
302
478
|
...this._getDefaultErrorSettings(
|
|
303
479
|
'sendEventForId',
|
|
304
|
-
new Error(
|
|
480
|
+
new Error(
|
|
481
|
+
`closing socket(${this.params.name}) does not match any in the activePresences`
|
|
482
|
+
),
|
|
305
483
|
issuer
|
|
306
484
|
),
|
|
307
485
|
|
|
@@ -310,11 +488,14 @@ export abstract class DynamoNTS_SocketServerService<
|
|
|
310
488
|
}
|
|
311
489
|
|
|
312
490
|
const activePresence: DynamoNTS_SocketPresence = this.presences[activePresenceIndex];
|
|
491
|
+
|
|
313
492
|
if (activePresence.issuerId !== issuer) {
|
|
314
493
|
throw new Dynamo_Error({
|
|
315
494
|
...this._getDefaultErrorSettings(
|
|
316
495
|
'sendEventForId',
|
|
317
|
-
new Error(
|
|
496
|
+
new Error(
|
|
497
|
+
`socket subscription for ${issuer} and ${activePresence.issuerId} does not match.`
|
|
498
|
+
),
|
|
318
499
|
issuer
|
|
319
500
|
),
|
|
320
501
|
|
|
@@ -323,11 +504,14 @@ export abstract class DynamoNTS_SocketServerService<
|
|
|
323
504
|
}
|
|
324
505
|
|
|
325
506
|
const socketIndex = activePresence.sockets.findIndex((s: SocketIO.Socket) => s === socket);
|
|
507
|
+
|
|
326
508
|
if (socketIndex === -1) {
|
|
327
509
|
throw new Dynamo_Error({
|
|
328
510
|
...this._getDefaultErrorSettings(
|
|
329
511
|
'sendEventForId',
|
|
330
|
-
new Error(
|
|
512
|
+
new Error(
|
|
513
|
+
`closing socket(${this.params.name}) does not match any in the activePresences`
|
|
514
|
+
),
|
|
331
515
|
issuer
|
|
332
516
|
),
|
|
333
517
|
|
|
@@ -336,10 +520,14 @@ export abstract class DynamoNTS_SocketServerService<
|
|
|
336
520
|
}
|
|
337
521
|
|
|
338
522
|
activePresence.sockets.splice(socketIndex);
|
|
523
|
+
|
|
339
524
|
if (this.getSubscriptionCloseTasks) {
|
|
340
|
-
await Dynamo_Array.asyncForEach(
|
|
341
|
-
|
|
342
|
-
|
|
525
|
+
await Dynamo_Array.asyncForEach(
|
|
526
|
+
this.getSubscriptionCloseTasks(),
|
|
527
|
+
async (task: DynamoNTS_SocketEventTask<null>) => {
|
|
528
|
+
await task(null, issuer);
|
|
529
|
+
}
|
|
530
|
+
);
|
|
343
531
|
}
|
|
344
532
|
|
|
345
533
|
if (activePresence.sockets.length === 0) {
|
|
@@ -384,7 +572,9 @@ export abstract class DynamoNTS_SocketServerService<
|
|
|
384
572
|
|
|
385
573
|
idIsSubscribed(id: string): boolean {
|
|
386
574
|
try {
|
|
387
|
-
return !!this.presences.find(
|
|
575
|
+
return !!this.presences.find(
|
|
576
|
+
(presence: DynamoNTS_SocketPresence) => presence.issuerId === id
|
|
577
|
+
);
|
|
388
578
|
} catch (error) {
|
|
389
579
|
throw new Dynamo_Error({
|
|
390
580
|
...this._getDefaultErrorSettings(
|
|
@@ -417,7 +607,9 @@ export abstract class DynamoNTS_SocketServerService<
|
|
|
417
607
|
|
|
418
608
|
async sendEventForId(id: string, event: string, content: any, issuer: string): Promise<void> {
|
|
419
609
|
try {
|
|
420
|
-
const presence: DynamoNTS_SocketPresence = this.presences.find(
|
|
610
|
+
const presence: DynamoNTS_SocketPresence = this.presences.find(
|
|
611
|
+
(pres: DynamoNTS_SocketPresence) => pres.issuerId === id
|
|
612
|
+
);
|
|
421
613
|
|
|
422
614
|
if (!presence) {
|
|
423
615
|
throw new Dynamo_Error({
|
|
@@ -435,9 +627,16 @@ export abstract class DynamoNTS_SocketServerService<
|
|
|
435
627
|
presence.emitEvent(event, content, issuer);
|
|
436
628
|
|
|
437
629
|
if (dynamoNTS_globalSettings.logSocketEventContent) {
|
|
438
|
-
Dynamo_Log.success(
|
|
630
|
+
Dynamo_Log.success(
|
|
631
|
+
` <--= emitted socket(${this.params.name}) event for presence: ${event}, ` +
|
|
632
|
+
`presenceId: ${id}, sockets: ${presence.sockets.length}` +
|
|
633
|
+
`\ncontent:`, content
|
|
634
|
+
);
|
|
439
635
|
} else {
|
|
440
|
-
Dynamo_Log.success(
|
|
636
|
+
Dynamo_Log.success(
|
|
637
|
+
` <--= emitted socket(${this.params.name}) event for presence: ${event}, ` +
|
|
638
|
+
`presenceId: ${id}, sockets: ${presence.sockets.length}`
|
|
639
|
+
);
|
|
441
640
|
}
|
|
442
641
|
} catch (error) {
|
|
443
642
|
try {
|
|
@@ -448,7 +647,10 @@ export abstract class DynamoNTS_SocketServerService<
|
|
|
448
647
|
await this.removeSubscription(socket);
|
|
449
648
|
});
|
|
450
649
|
|
|
451
|
-
const presence: DynamoNTS_SocketPresence = this.presences.find(
|
|
650
|
+
const presence: DynamoNTS_SocketPresence = this.presences.find(
|
|
651
|
+
(pres: DynamoNTS_SocketPresence) => pres.issuerId === id
|
|
652
|
+
);
|
|
653
|
+
|
|
452
654
|
if (!presence) {
|
|
453
655
|
throw new Dynamo_Error({
|
|
454
656
|
...this._getDefaultErrorSettings(
|
|
@@ -535,13 +737,23 @@ export abstract class DynamoNTS_SocketServerService<
|
|
|
535
737
|
/**
|
|
536
738
|
* You can setup tasks for the subscription event in this function
|
|
537
739
|
*/
|
|
538
|
-
|
|
740
|
+
protected getSubscriptionCloseTasks?(): DynamoNTS_SocketEventTask<null>[];
|
|
539
741
|
|
|
540
742
|
/**
|
|
541
743
|
* You can setup preprocessess for the subscription event in this function
|
|
542
744
|
*/
|
|
543
745
|
protected getSubscriptionPreProcessess?(): DynamoNTS_SocketEventPreprocessTask[];
|
|
544
746
|
|
|
747
|
+
/**
|
|
748
|
+
* You can setup tasks for the unsubscribe event in this function
|
|
749
|
+
*/
|
|
750
|
+
protected getUnsubscribeTasks?(): DynamoNTS_SocketEventTask<T_SubscriptionContent>[];
|
|
751
|
+
|
|
752
|
+
/**
|
|
753
|
+
* You can setup preprocessess for the unsubscribe event in this function
|
|
754
|
+
*/
|
|
755
|
+
protected getUnsubscribePreProcessess?(): DynamoNTS_SocketEventPreprocessTask[];
|
|
756
|
+
|
|
545
757
|
/**
|
|
546
758
|
* You can setup tasks for the error event in this function
|
|
547
759
|
*/
|