@iobroker/socket-classes 1.4.4 → 1.4.6

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/README.md CHANGED
@@ -978,6 +978,9 @@ Read all instances of the given adapter, or all instances of all adapters if ada
978
978
  -->
979
979
 
980
980
  ## Changelog
981
+ ### 1.4.6 (2023-10-19)
982
+ * (bluefox) Added `publishInstanceMessageAll` command
983
+
981
984
  ### 1.4.4 (2023-10-11)
982
985
  * (bluefox) Caught errors by subscribe/unsubscribe
983
986
 
@@ -140,27 +140,24 @@ class SocketAdmin extends SocketCommon {
140
140
  stateChange(id, state) {
141
141
  this.commands.stateChange(id, state);
142
142
 
143
- if (this.server && this.server.sockets) {
144
- const sockets = this.server.sockets.sockets || this.server.sockets.connected;
145
-
143
+ const sockets = this.getSocketsList();
144
+ if (sockets) {
146
145
  sockets.forEach(socket =>
147
146
  this.commands.publish(socket, 'stateChange', id, state));
148
147
  }
149
148
  }
150
149
 
151
150
  fileChange(id, fileName, size) {
152
- if (this.server && this.server.sockets) {
153
- const sockets = this.server.sockets.sockets || this.server.sockets.connected;
154
-
151
+ const sockets = this.getSocketsList();
152
+ if (sockets) {
155
153
  sockets.forEach(socket =>
156
154
  this.commands.publishFile(socket, id, fileName, size));
157
155
  }
158
156
  }
159
157
 
160
158
  repoUpdated() {
161
- if (this.server && this.server.sockets) {
162
- const sockets = this.server.sockets.sockets || this.server.sockets.connected;
163
-
159
+ const sockets = this.getSocketsList();
160
+ if (sockets) {
164
161
  sockets.forEach(socket =>
165
162
  this.__updateSession(socket));
166
163
 
@@ -169,9 +166,8 @@ class SocketAdmin extends SocketCommon {
169
166
  }
170
167
 
171
168
  objectChange(id, obj) {
172
- if (this.server && this.server.sockets) {
173
- const sockets = this.server.sockets.sockets || this.server.sockets.connected;
174
-
169
+ const sockets = this.getSocketsList();
170
+ if (sockets) {
175
171
  sockets.forEach(socket =>
176
172
  this.commands.publish(socket, 'objectChange', id, obj));
177
173
  }
@@ -186,13 +182,14 @@ class SocketAdmin extends SocketCommon {
186
182
  }
187
183
 
188
184
  sendCommand(obj) {
189
- if (this.commands.sendCommand(obj) && this.server && this.server.sockets) {
190
- const sockets = this.server.sockets.sockets || this.server.sockets.connected;
185
+ if (this.commands.sendCommand(obj)) {
186
+ const sockets = this.getSocketsList();
187
+ if (sockets) {
188
+ sockets.forEach(socket =>
189
+ this.__updateSession(socket));
191
190
 
192
- sockets.forEach(socket =>
193
- this.__updateSession(socket));
194
-
195
- this.server.sockets.emit(obj.command, obj.message.id, obj.message.data);
191
+ this.server.sockets.emit(obj.command, obj.message.id, obj.message.data);
192
+ }
196
193
  }
197
194
  }
198
195
 
@@ -198,9 +198,10 @@ class SocketCommon {
198
198
  _unsubscribeAll() {
199
199
  if (this.server && this.server.ioBroker) {
200
200
  // this could be an object or array
201
- const sockets = this.server.sockets.sockets || this.server.sockets.connected;
202
-
203
- Object.keys(sockets).forEach(i => this.commands.unsubscribeSocket(sockets[i]));
201
+ const sockets = this.getSocketsList();
202
+ if (sockets) {
203
+ Object.keys(sockets).forEach(i => this.commands.unsubscribeSocket(sockets[i]));
204
+ }
204
205
  } else if (this.server && this.server.sockets) {
205
206
  for (const socket in this.server.sockets) {
206
207
  if (Object.prototype.hasOwnProperty.call(this.server.sockets, socket)) {
@@ -383,10 +384,9 @@ class SocketCommon {
383
384
 
384
385
  if (this.server) {
385
386
  const clientsArray = [];
386
- if (this.server.sockets) {
387
+ const sockets = this.getSocketsList();
388
+ if (sockets) {
387
389
  // this could be an object or array
388
- const sockets = this.server.sockets.sockets || this.server.sockets.connected;
389
-
390
390
  Object.keys(sockets).forEach(i => clientsArray.push(sockets[i]._name || 'noname'));
391
391
  }
392
392
  const text = `[${clientsArray.length}]${clientsArray.join(', ')}`;
@@ -423,15 +423,33 @@ class SocketCommon {
423
423
  return this.commands.publishFile(socket, id, fileName, size);
424
424
  }
425
425
 
426
+ getSocketsList() {
427
+ if (this.server && this.server.sockets) {
428
+ // this could be an object or array
429
+ return this.server.sockets.sockets || this.server.sockets.connected;
430
+ }
431
+ return null;
432
+ }
433
+
434
+ publishInstanceMessageAll(sourceInstance, messageType, sid, data) {
435
+ const sockets = this.getSocketsList();
436
+ if (sockets) {
437
+ Object.keys(sockets).forEach(i => {
438
+ if (sockets[i].id === sid) {
439
+ this.publishInstanceMessage(sockets[i], sourceInstance, messageType, data);
440
+ }
441
+ });
442
+ }
443
+ }
444
+
426
445
  close() {
427
446
  this._unsubscribeAll();
428
447
 
429
448
  this.commands.destroy();
430
449
 
431
- if (this.server && this.server.sockets) {
450
+ const sockets = this.getSocketsList();
451
+ if (sockets) {
432
452
  // this could be an object or array
433
- const sockets = this.server.sockets.sockets || this.server.sockets.connected;
434
-
435
453
  Object.keys(sockets).forEach(i => {
436
454
  const socket = sockets[i];
437
455
  if (socket._sessionTimer) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@iobroker/socket-classes",
3
- "version": "1.4.4",
3
+ "version": "1.4.6",
4
4
  "description": "ioBroker server-side web sockets",
5
5
  "author": {
6
6
  "name": "bluefox",
@@ -21,7 +21,7 @@
21
21
  "url": "https://github.com/ioBroker/ioBroker.socket-classes"
22
22
  },
23
23
  "dependencies": {
24
- "@iobroker/adapter-core": "^3.0.3",
24
+ "@iobroker/adapter-core": "^3.0.4",
25
25
  "axios": "^1.5.1",
26
26
  "cookie-parser": "^1.4.6",
27
27
  "passport": "^0.6.0"