@futdevpro/nts-dynamo 1.6.13 → 1.6.15

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.
@@ -133,7 +133,7 @@ export abstract class DynamoNTS_AppExtended extends DynamoNTS_App {
133
133
  if (this.socketServices?.length) {
134
134
  this.setSocketSecurity();
135
135
 
136
- this.mountSocketServerServices();
136
+ this.setupSocketServerServices();
137
137
 
138
138
  if (this.debugLog) console.log(`\nAll sockets setted up.... sockets using security: ${this.socketSecurity}`);
139
139
  } else {
@@ -163,7 +163,7 @@ export abstract class DynamoNTS_AppExtended extends DynamoNTS_App {
163
163
  /**
164
164
  *
165
165
  */
166
- private mountSocketServerServices(): void {
166
+ private setupSocketServerServices(): void {
167
167
  try {
168
168
  let httpSocketServer: Http.Server;
169
169
  if (this.socketSecurity !== DynamoNTS_RouteSecurity.secure) {
@@ -206,38 +206,43 @@ export abstract class DynamoNTS_AppExtended extends DynamoNTS_App {
206
206
  }
207
207
 
208
208
  this.socketServices.forEach((service: DynamoNTS_SocketServerService<any>) => {
209
- const existingPorts: DynamoNTS_SocketServerService<any>[] = this.socketServices.filter(
210
- (mod: DynamoNTS_SocketServerService<any>) => mod.port === service.port
211
- );
209
+ try {
210
+ const existingPorts: DynamoNTS_SocketServerService<any>[] = this.socketServices.filter(
211
+ (mod: DynamoNTS_SocketServerService<any>) => mod.port === service.port
212
+ );
212
213
 
213
- if (1 < existingPorts.length || this.ports.httpPort === service.port || this.ports.httpsPort === service.port) {
214
- let error = new Error(`PORT DUPLICATION: ${service.port}`);
215
- let errorStack: string[] = error.stack.split('\n');
216
- errorStack.splice(1, 4);
217
- error.stack = errorStack.join('\n');
214
+ if (1 < existingPorts.length || this.ports.httpPort === service.port || this.ports.httpsPort === service.port) {
215
+ let error = new Error(`PORT DUPLICATION: ${service.port}`);
216
+ let errorStack: string[] = error.stack.split('\n');
217
+ errorStack.splice(1, 4);
218
+ error.stack = errorStack.join('\n');
218
219
 
219
- throw error;
220
- }
220
+ throw error;
221
+ }
221
222
 
222
- if (service.security === DynamoNTS_SocketSecurity.open) {
223
- if (this.debugLog) console.log(`\nsocket setup (open): ${service.name}:${service.port}`);
224
- service.setupSocketServer(new SocketIO.Server(httpSocketServer));
223
+ if (service.security === DynamoNTS_SocketSecurity.open) {
224
+ if (this.debugLog) console.log(`\nsocket setup (open): ${service.name}:${service.port}`);
225
+ service.setupSocketServer(new SocketIO.Server(httpSocketServer));
225
226
 
226
- } else if (service.security === DynamoNTS_SocketSecurity.secure) {
227
- if (this.debugLog) console.log(`\nsocket setup (secure): ${service.name}:${service.port}`);
228
- service.setupSocketServer(new SocketIO.Server(this.httpsServer));
229
-
230
- } else {
231
- let error = new Error(`INVALID Socket Service security: ${service.security} on ${service.name}`);
232
- let errorStack: string[] = error.stack.split('\n');
233
- errorStack.splice(1, 4);
234
- error.stack = errorStack.join('\n');
227
+ } else if (service.security === DynamoNTS_SocketSecurity.secure) {
228
+ if (this.debugLog) console.log(`\nsocket setup (secure): ${service.name}:${service.port}`);
229
+ service.setupSocketServer(new SocketIO.Server(this.httpsServer));
230
+
231
+ } else {
232
+ let error = new Error(`INVALID Socket Service security: ${service.security} on ${service.name}`);
233
+ let errorStack: string[] = error.stack.split('\n');
234
+ errorStack.splice(1, 4);
235
+ error.stack = errorStack.join('\n');
235
236
 
237
+ throw error;
238
+ }
239
+ } catch (error) {
240
+ Dynamo_Log.error(`\nSocket Server service setup failed. (${service.name})\n`, error, '\n');
236
241
  throw error;
237
242
  }
238
243
  });
239
244
  } catch (error) {
240
- Dynamo_Log.error('\nSocket Server setup failed.\n', error, '\n');
245
+ Dynamo_Log.error(`\nSocket Server services setup failed. (${this.socketServices?.length} services)\n`, error, '\n');
241
246
  throw error;
242
247
  }
243
248
  }
@@ -77,54 +77,70 @@ export abstract class DynamoNTS_SocketClientService extends DynamoNTS_SingletonS
77
77
 
78
78
  this.connectSocket();
79
79
  } catch (error) {
80
- Dynamo_Log.error(`${this.params.name} Socket Client Service setup failed!`, error);
80
+ Dynamo_Log.error(`${this.params?.name} Socket Client Service setup failed!`, error);
81
81
  }
82
82
  }
83
83
 
84
84
  private setupDefaultEvents(): void {
85
- this.defaultEvents.forEach((defaultEvent: DynamoNTS_SocketEvent<any>) => {
86
- const eventDeclared = this.incomingEvents.find((event: DynamoNTS_SocketEvent<any>) => event.eventType === defaultEvent.eventType);
87
- if (!eventDeclared) {
88
- this.incomingEvents.push(defaultEvent);
89
- } else {
90
- eventDeclared.tasks.unshift(...defaultEvent.tasks);
91
- }
92
- });
85
+ try {
86
+ this.defaultEvents.forEach((defaultEvent: DynamoNTS_SocketEvent<any>) => {
87
+ const eventDeclared = this.incomingEvents.find((event: DynamoNTS_SocketEvent<any>) => event.eventType === defaultEvent.eventType);
88
+ if (!eventDeclared) {
89
+ this.incomingEvents.push(defaultEvent);
90
+ } else {
91
+ eventDeclared.tasks.unshift(...defaultEvent.tasks);
92
+ }
93
+ });
94
+ } catch (error) {
95
+ Dynamo_Log.error(`Socket Client Service Setup Default Events failed: ${this.params?.name} (${this.params?.port})`, error);
96
+ }
93
97
  }
94
98
 
95
99
  private setupSocketEvents(): void {
96
- if (this.log) console.log(`Setup Socket Events... (${this.params.name})`);
100
+ try {
101
+ if (this.log) console.log(`Setup Socket Events... (${this.params.name})`);
97
102
 
98
- this.incomingEvents.forEach((event: DynamoNTS_SocketEvent<any>) => {
99
- if (!event.socketName) {
100
- event.socketName = this.name;
101
- }
102
- this.socket.on(event.eventType, async (content: any) => {
103
- try {
104
- await event.executeEventTasks(content, this.params.service);
105
- } catch (error) {
106
- Dynamo_Log.error(`Socket Event failed: ${this.params.name} (${this.params.port})`, error);
107
- this.emitEvent(DynamoNTS_SocketEventType.error, error);
103
+ this.incomingEvents.forEach((event: DynamoNTS_SocketEvent<any>) => {
104
+ if (!event.socketName) {
105
+ event.socketName = this.name;
108
106
  }
107
+ this.socket.on(event.eventType, async (content: any) => {
108
+ try {
109
+ await event.executeEventTasks(content, this.params.service);
110
+ } catch (error) {
111
+ Dynamo_Log.error(`Socket Event failed: ${this.params.name} (${this.params.port})`, error);
112
+ this.emitEvent(DynamoNTS_SocketEventType.error, error);
113
+ }
114
+ });
109
115
  });
110
- });
116
+ } catch (error) {
117
+ Dynamo_Log.error(`Socket Client Service Setup Socket Events failed: ${this.params?.name} (${this.params?.port})`, error);
118
+ }
111
119
  }
112
120
 
113
121
  private connectSocket(): void {
114
- console.log(`${this.params.name} Socket Client connecting to ${this.params.address}:${this.params.port} ...`);
122
+ try {
123
+ console.log(`${this.params.name} Socket Client connecting to ${this.params.address}:${this.params.port} ...`);
115
124
 
116
- this.socket.connect();
125
+ this.socket.connect();
117
126
 
118
- if (this.params.reconnect) {
119
- this.tryReconnectIfNeeded();
127
+ if (this.params.reconnect) {
128
+ this.tryReconnectIfNeeded();
129
+ }
130
+ } catch (error) {
131
+ Dynamo_Log.error(`Socket Client Service Connect Socket failed: ${this.params?.name} (${this.params?.port})`, error);
120
132
  }
121
133
  }
122
134
 
123
135
  private async tryReconnectIfNeeded(): Promise<void> {
124
- await delay(this._params.reconnectDelay);
125
-
126
- if (!this._connected) {
127
- this.connectSocket();
136
+ try {
137
+ await delay(this._params.reconnectDelay);
138
+
139
+ if (!this._connected) {
140
+ this.connectSocket();
141
+ }
142
+ } catch (error) {
143
+ Dynamo_Log.error(`Socket Client Service Try Reconnect failed: ${this.params?.name} (${this.params?.port})`, error);
128
144
  }
129
145
  }
130
146
 
@@ -134,8 +150,13 @@ export abstract class DynamoNTS_SocketClientService extends DynamoNTS_SingletonS
134
150
  * @param content event content
135
151
  */
136
152
  protected emitEvent(eventType: string, content: any): void {
137
- console.log(`\n<== outgoing ${this.name} socket event: ${eventType}`);
138
- this.socket.emit(eventType, content);
153
+ try {
154
+ console.log(`\n<== outgoing ${this.name} socket event: ${eventType}`);
155
+ this.socket.emit(eventType, content);
156
+ } catch (error) {
157
+ Dynamo_Log.error(`Socket Client Service Emit Event failed: ${this.params?.name} (${this.params?.port})`, error);
158
+ throw error;
159
+ }
139
160
  }
140
161
 
141
162
  /**
@@ -64,9 +64,9 @@ export abstract class DynamoNTS_SocketServerService<
64
64
 
65
65
  this.prepareEvents();
66
66
 
67
- if (this.log) console.log(`Socket Controller setup done: ${this.params.name} (${this.params.port}) serurity: ${this.params.security}\n`);
67
+ if (this.log) console.log(`Socket Server Service setup done: ${this.params.name} (${this.params.port}) serurity: ${this.params.security}\n`);
68
68
  } catch (error) {
69
- Dynamo_Log.error(`Socket Controller setup failed: ${this.params?.name} (${this.params?.port})`, error);
69
+ Dynamo_Log.error(`Socket Server Service setup failed: ${this.params?.name} (${this.params?.port})`, error);
70
70
  }
71
71
  }
72
72
 
@@ -74,191 +74,235 @@ export abstract class DynamoNTS_SocketServerService<
74
74
  * You must setup events and required services for this function
75
75
  */
76
76
  private prepareEvents(): void {
77
- const connectEventIndex = this.incomingEvents.findIndex((event: DynamoNTS_SocketEvent<any>) => event.eventType === DynamoNTS_SocketEventType.connection);
78
- if (0 <= connectEventIndex) {
79
- this.connectEvent = this.incomingEvents.splice(connectEventIndex)[0];
80
- } else {
81
- this.connectEvent = new DynamoNTS_SocketEvent({
82
- eventType: DynamoNTS_SocketEventType.connection,
83
- tasks: []
84
- });
85
- }
86
- this.connectEvent.socketName = this.params.name;
87
-
88
- if (this.getPresenceFromSubscrioptionEventContent) {
89
- const subscriptionEventIndex = this.incomingEvents.findIndex((event: DynamoNTS_SocketEvent<any>) => event.eventType === DynamoNTS_SocketEventType.subscribe);
90
- if (0 <= subscriptionEventIndex) {
91
- this.subscriptionEvent = this.incomingEvents.splice(subscriptionEventIndex)[0];
92
- Dynamo_Log.error(
93
- `You should not set the subscription event, but the subscriptions tasks, ` +
94
- `in case you need additional steps for your subscripotions.`,
95
- `${this.params.name} (${this.params.port})`,
96
- );
77
+ try {
78
+ const connectEventIndex = this.incomingEvents.findIndex((event: DynamoNTS_SocketEvent<any>) => event.eventType === DynamoNTS_SocketEventType.connection);
79
+ if (0 <= connectEventIndex) {
80
+ this.connectEvent = this.incomingEvents.splice(connectEventIndex)[0];
97
81
  } else {
98
- this.subscriptionEvent = new DynamoNTS_SocketEvent<T_SubscriptionContent>({
99
- eventType: DynamoNTS_SocketEventType.subscribe
82
+ this.connectEvent = new DynamoNTS_SocketEvent({
83
+ eventType: DynamoNTS_SocketEventType.connection,
84
+ tasks: []
100
85
  });
101
86
  }
102
- if (this.getSubscriptionPreProcessess) {
103
- this.subscriptionEvent.preProcessess.push(...this.getSubscriptionPreProcessess());
104
- }
105
- if (this.getSubscriptionTasks) {
106
- this.subscriptionEvent.tasks.push(...this.getSubscriptionTasks());
107
- }
87
+ this.connectEvent.socketName = this.params.name;
108
88
 
109
- const disconnectEventIndex = this.incomingEvents.findIndex((event: DynamoNTS_SocketEvent<any>) => event.eventType === DynamoNTS_SocketEventType.disconnect);
110
- if (0 <= disconnectEventIndex) {
111
- this.incomingEvents[disconnectEventIndex].tasks.push(this.removeSubscriptionOnDisconnect);
112
- } else {
113
- this.incomingEvents.push(
114
- new DynamoNTS_SocketEvent({
115
- eventType: DynamoNTS_SocketEventType.disconnect,
89
+ if (this.getPresenceFromSubscrioptionEventContent) {
90
+ const subscriptionEventIndex = this.incomingEvents.findIndex((event: DynamoNTS_SocketEvent<any>) => event.eventType === DynamoNTS_SocketEventType.subscribe);
91
+ if (0 <= subscriptionEventIndex) {
92
+ this.subscriptionEvent = this.incomingEvents.splice(subscriptionEventIndex)[0];
93
+ Dynamo_Log.error(
94
+ `You should not set the subscription event, but the subscriptions tasks, ` +
95
+ `in case you need additional steps for your subscripotions.`,
96
+ `${this.params.name} (${this.params.port})`,
97
+ );
98
+ } else {
99
+ this.subscriptionEvent = new DynamoNTS_SocketEvent<T_SubscriptionContent>({
100
+ eventType: DynamoNTS_SocketEventType.subscribe
101
+ });
102
+ }
103
+ if (this.getSubscriptionPreProcessess) {
104
+ this.subscriptionEvent.preProcessess.push(...this.getSubscriptionPreProcessess());
105
+ }
106
+ if (this.getSubscriptionTasks) {
107
+ this.subscriptionEvent.tasks.push(...this.getSubscriptionTasks());
108
+ }
109
+
110
+ const disconnectEventIndex = this.incomingEvents.findIndex((event: DynamoNTS_SocketEvent<any>) => event.eventType === DynamoNTS_SocketEventType.disconnect);
111
+ if (0 <= disconnectEventIndex) {
112
+ this.incomingEvents[disconnectEventIndex].tasks.push(this.removeSubscriptionOnDisconnect);
113
+ } else {
114
+ this.incomingEvents.push(
115
+ new DynamoNTS_SocketEvent({
116
+ eventType: DynamoNTS_SocketEventType.disconnect,
117
+ tasks: [
118
+ this.removeSubscriptionOnDisconnect
119
+ ]
120
+ })
121
+ );
122
+ }
123
+
124
+ const errorEventIndex = this.incomingEvents.findIndex((event: DynamoNTS_SocketEvent<any>) => event.eventType === DynamoNTS_SocketEventType.error);
125
+ if (errorEventIndex == -1 && this.errorHandling) {
126
+ this.incomingEvents.push(new DynamoNTS_SocketEvent<T_ErrorContent>({
127
+ eventType: DynamoNTS_SocketEventType.error,
116
128
  tasks: [
117
- this.removeSubscriptionOnDisconnect
129
+ this.errorHandling
118
130
  ]
119
- })
120
- );
121
- }
131
+ }));
132
+ }
122
133
 
123
- const errorEventIndex = this.incomingEvents.findIndex((event: DynamoNTS_SocketEvent<any>) => event.eventType === DynamoNTS_SocketEventType.error);
124
- if (errorEventIndex == -1 && this.errorHandling) {
125
- this.incomingEvents.push(new DynamoNTS_SocketEvent<T_ErrorContent>({
126
- eventType: DynamoNTS_SocketEventType.error,
127
- tasks: [
128
- this.errorHandling
129
- ]
130
- }));
134
+ this.subscriptionEvent.socketName = this.params.name;
131
135
  }
132
136
 
133
- this.subscriptionEvent.socketName = this.params.name;
137
+ this.incomingEvents.forEach((event: DynamoNTS_SocketEvent<any>) => {
138
+ event.socketName = this.params.name;
139
+ });
140
+ } catch (error) {
141
+ Dynamo_Log.error(`Socket Server Service - Event Preparation setup failed: ${this.params?.name} (${this.params?.port})`, error);
134
142
  }
135
-
136
- this.incomingEvents.forEach((event: DynamoNTS_SocketEvent<any>) => {
137
- event.socketName = this.params.name;
138
- });
139
143
  }
140
144
 
141
145
  async setupSocketServer(newSocketServer: SocketIO.Server): Promise<void> {
142
- this.socketServer = newSocketServer;
143
- this.socketServer.on(DynamoNTS_SocketEventType.connection, async (socket: SocketIO.Socket) => {
144
- let issuer: string;
145
-
146
- for (let i = 0; i < this.connectEvent.preProcessess.length; i++) {
147
- await this.connectEvent.preProcessess[i](socket);
148
- }
146
+ try {
147
+ this.socketServer = newSocketServer;
148
+ this.socketServer.on(DynamoNTS_SocketEventType.connection, async (socket: SocketIO.Socket) => {
149
+ let issuer: string;
150
+
151
+ for (let i = 0; i < this.connectEvent.preProcessess.length; i++) {
152
+ await this.connectEvent.preProcessess[i](socket);
153
+ }
149
154
 
150
- for (let i = 0; i < this.connectEvent.tasks.length; i++) {
151
- await this.connectEvent.tasks[i](socket);
152
- }
155
+ for (let i = 0; i < this.connectEvent.tasks.length; i++) {
156
+ await this.connectEvent.tasks[i](socket);
157
+ }
153
158
 
154
- if (this.getPresenceFromSubscrioptionEventContent) {
155
- socket.on(DynamoNTS_SocketEventType.subscribe, async (content: any) => {
156
- try {
157
- const presence: T_Presence = await this.getPresenceFromSubscrioptionEventContent(content, socket);
158
- issuer = presence.issuerId;
159
-
160
- this.socketSubscription(presence);
161
- await this.subscriptionEvent.executeEventTasks(content, issuer);
162
-
163
- console.log(`socket subscription successfull (${issuer})`);
164
- } catch (error) {
165
- Dynamo_Log.error(`Socket Subscription failed: ${this.params.name} (${this.params.port}) will disconnect now...`, error);
166
- socket.emit(DynamoNTS_SocketEventType.error, error);
167
- socket.disconnect();
168
- }
169
- })
170
- }
159
+ if (this.getPresenceFromSubscrioptionEventContent) {
160
+ socket.on(DynamoNTS_SocketEventType.subscribe, async (content: any) => {
161
+ try {
162
+ const presence: T_Presence = await this.getPresenceFromSubscrioptionEventContent(content, socket);
163
+ issuer = presence.issuerId;
164
+
165
+ this.socketSubscription(presence);
166
+ await this.subscriptionEvent.executeEventTasks(content, issuer);
167
+
168
+ console.log(`socket subscription successfull (${issuer})`);
169
+ } catch (error) {
170
+ Dynamo_Log.error(`Socket Subscription failed: ${this.params.name} (${this.params.port}) will disconnect now...`, error);
171
+ socket.emit(DynamoNTS_SocketEventType.error, error);
172
+ socket.disconnect();
173
+ }
174
+ })
175
+ }
171
176
 
172
- this.incomingEvents.forEach((event: DynamoNTS_SocketEvent<any>) => {
173
- socket.on(event.eventType, async (content: any) => {
174
- try {
175
- await event.executeEventTasks(content, issuer);
176
- } catch (error) {
177
- Dynamo_Log.error(`Socket Event failed: ${this.params.name} (${this.params.port})`, error);
178
- socket.emit(DynamoNTS_SocketEventType.error, error);
179
- }
177
+ this.incomingEvents.forEach((event: DynamoNTS_SocketEvent<any>) => {
178
+ socket.on(event.eventType, async (content: any) => {
179
+ try {
180
+ await event.executeEventTasks(content, issuer);
181
+ } catch (error) {
182
+ Dynamo_Log.error(`Socket Event failed: ${this.params.name} (${this.params.port})`, error);
183
+ socket.emit(DynamoNTS_SocketEventType.error, error);
184
+ }
185
+ });
180
186
  });
181
- });
182
187
 
183
- });
188
+ });
184
189
 
185
- this.socketServer.listen(this.params.port);
190
+ this.socketServer.listen(this.params.port);
186
191
 
187
- console.log(
188
- `\nsocket service setup finished: ${this.params.name}` +
189
- `\nsocket server listening on port: ${this.params.port}`
190
- );
192
+ console.log(
193
+ `\nsocket service setup finished: ${this.params.name}` +
194
+ `\nsocket server listening on port: ${this.params.port}`
195
+ );
196
+ } catch (error) {
197
+ Dynamo_Log.error(`Socket Server Service - Deep Setup failed: ${this.params?.name} (${this.params?.port})`, error);
198
+ throw error;
199
+ }
191
200
  }
192
201
 
193
202
  protected async socketSubscription(newPresence: T_Presence): Promise<void> {
194
- const activePresence: T_Presence = this.presences.find((pres: T_Presence) => pres.issuerId === newPresence.issuerId);
195
- if (activePresence) {
196
- activePresence.sockets.push(newPresence.sockets[0]);
197
- } else {
198
- this.presences.push(newPresence);
203
+ try {
204
+ const activePresence: T_Presence = this.presences.find((pres: T_Presence) => pres.issuerId === newPresence.issuerId);
205
+
206
+ if (activePresence) {
207
+ activePresence.sockets.push(newPresence.sockets[0]);
208
+ } else {
209
+ this.presences.push(newPresence);
210
+ }
211
+ } catch (error) {
212
+ Dynamo_Log.error(`Socket Subscription failed: ${this.params?.name} (${this.params?.port})`, error);
213
+ throw error;
199
214
  }
200
215
  }
201
216
 
202
217
  private async removeSubscriptionOnDisconnect(socket: SocketIO.Socket, issuer?: string): Promise<void> {
203
- const activePresenceIndex: number = this.presences.findIndex((pres: DynamoNTS_SocketPresence) => pres.sockets.includes(socket));
204
- if (0 <= activePresenceIndex) {
205
- const activePresence: DynamoNTS_SocketPresence = this.presences[activePresenceIndex];
206
- if (activePresence.issuerId !== issuer) {
207
- Dynamo_Log.error(`Socket subscription for ${issuer} and ${activePresence.issuerId} does not match.`);
208
- }
218
+ try {
219
+ const activePresenceIndex: number = this.presences.findIndex((pres: DynamoNTS_SocketPresence) => pres.sockets.includes(socket));
220
+ if (0 <= activePresenceIndex) {
221
+ const activePresence: DynamoNTS_SocketPresence = this.presences[activePresenceIndex];
222
+ if (activePresence.issuerId !== issuer) {
223
+ Dynamo_Log.error(`Socket subscription for ${issuer} and ${activePresence.issuerId} does not match.`);
224
+ }
209
225
 
210
- const socketIndex = activePresence.sockets.findIndex((s: SocketIO.Socket) => s === socket);
211
- if (0 <= socketIndex) {
212
- activePresence.sockets.splice(socketIndex);
213
- if (this.getSubscriptionCloseTasks) {
214
- await Dynamo_Array.asyncForEach(this.getSubscriptionCloseTasks(), async (task: DynamoNTS_SocketEventTask<null>) => {
215
- await task(null, issuer);
216
- });
226
+ const socketIndex = activePresence.sockets.findIndex((s: SocketIO.Socket) => s === socket);
227
+ if (0 <= socketIndex) {
228
+ activePresence.sockets.splice(socketIndex);
229
+ if (this.getSubscriptionCloseTasks) {
230
+ await Dynamo_Array.asyncForEach(this.getSubscriptionCloseTasks(), async (task: DynamoNTS_SocketEventTask<null>) => {
231
+ await task(null, issuer);
232
+ });
233
+ }
234
+ } else {
235
+ Dynamo_Log.error(`Socket does not match present in presence.`);
217
236
  }
218
- } else {
219
- Dynamo_Log.error(`Socket does not match present in presence.`);
220
- }
221
237
 
222
- if (activePresence.sockets.length === 0) {
223
- this.presences.splice(activePresenceIndex);
238
+ if (activePresence.sockets.length === 0) {
239
+ this.presences.splice(activePresenceIndex);
240
+ }
224
241
  }
242
+ } catch (error) {
243
+ Dynamo_Log.error(`Socket Subscription Removal failed: ${this.params?.name} (${this.params?.port})`, error);
225
244
  }
226
245
  }
227
246
 
228
247
  emitEvent(event: string, content: any): void {
229
- this.socketServer.emit(event, content);
248
+ try {
249
+ this.socketServer.emit(event, content);
250
+ } catch (error) {
251
+ Dynamo_Log.error(`Socket Event Emit (${event}) failed: ${this.params?.name} (${this.params?.port})`, error);
252
+ throw error;
253
+ }
230
254
  }
231
255
 
232
256
  idIsSubscribed(id: string): boolean {
233
- return !!this.presences.find((presence: DynamoNTS_SocketPresence) => presence.issuerId === id);
257
+ try {
258
+ return !!this.presences.find((presence: DynamoNTS_SocketPresence) => presence.issuerId === id);
259
+ } catch (error) {
260
+ Dynamo_Log.error(`Socket ID Subscription Check (${id}) failed: ${this.params?.name} (${this.params?.port})`, error);
261
+ throw error;
262
+ }
234
263
  }
235
264
 
236
265
  emitError(presenceIssuerId: string, error: any): void {
237
- this.sendEventForId(presenceIssuerId, DynamoNTS_SocketEventType.error, error);
266
+ try {
267
+ this.sendEventForId(presenceIssuerId, DynamoNTS_SocketEventType.error, error);
268
+ } catch (error) {
269
+ Dynamo_Log.error(`Socket Error Emit (id: ${presenceIssuerId}) failed: ${this.params?.name} (${this.params?.port})`, error);
270
+ throw error;
271
+ }
238
272
  }
239
273
 
240
274
  async sendEventForId(id: string, event: string, content: any, error?: (err: any) => void): Promise<void> {
241
- const presence: DynamoNTS_SocketPresence = this.presences.find((pres: DynamoNTS_SocketPresence) => pres.issuerId === id);
242
- if (presence) {
243
- presence.emitEvent(event, content);
244
- } else {
245
- if (error) {
246
-
247
- throw new Dynamo_Error({
248
- status: 404,
249
- errorCode: 'NTS-S99-001',
250
- addECToUserMsg: true,
251
- message: `No active socket whit this specific ID: ${id}`,
252
- userMessage: this.defaultErrorUserMsg
253
- });
275
+ try {
276
+ const presence: DynamoNTS_SocketPresence = this.presences.find((pres: DynamoNTS_SocketPresence) => pres.issuerId === id);
277
+ if (presence) {
278
+ presence.emitEvent(event, content);
279
+ } else {
280
+ if (error) {
281
+
282
+ throw new Dynamo_Error({
283
+ status: 404,
284
+ errorCode: 'NTS-S99-001',
285
+ addECToUserMsg: true,
286
+ message: `No active socket whit this specific ID: ${id}`,
287
+ userMessage: this.defaultErrorUserMsg
288
+ });
289
+ }
254
290
  }
291
+ } catch (error) {
292
+ Dynamo_Log.error(`Socket Event Emit for id (${id}, ${event}) failed: ${this.params?.name} (${this.params?.port})`, error);
293
+ throw error;
255
294
  }
256
295
  }
257
296
 
258
297
  broadcastEvent(event: string, content: any): void {
259
- this.presences.forEach((presence: DynamoNTS_SocketPresence) => {
260
- presence.emitEvent(event, content);
261
- })
298
+ try {
299
+ this.presences.forEach((presence: DynamoNTS_SocketPresence) => {
300
+ presence.emitEvent(event, content);
301
+ });
302
+ } catch (error) {
303
+ Dynamo_Log.error(`Socket Event Broadcast (${event}) failed: ${this.params?.name} (${this.params?.port})`, error);
304
+ throw error;
305
+ }
262
306
  }
263
307
 
264
308
  /**