@iobroker/socket-classes 0.3.2 → 0.4.2

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
@@ -69,6 +69,15 @@ io.close();
69
69
  -->
70
70
 
71
71
  ## Changelog
72
+ ### 0.4.2 (2022-05-23)
73
+ * (bluefox) Corrected renameFile command for admin
74
+
75
+ ### 0.4.1 (2022-05-23)
76
+ * (bluefox) Corrected changePassword command for admin
77
+
78
+ ### 0.4.0 (2022-05-19)
79
+ * (bluefox) Added support of socket.io 4.x
80
+
72
81
  ### 0.3.2 (2022-05-19)
73
82
  * (bluefox) Hide warn messages
74
83
 
@@ -141,21 +141,27 @@ class SocketAdmin extends SocketCommon {
141
141
  this.commands.stateChange(id, state);
142
142
 
143
143
  if (this.server && this.server.sockets) {
144
- this.server.sockets.connected.forEach(socket =>
144
+ const sockets = this.server.sockets.sockets || this.server.sockets.connected;
145
+
146
+ sockets.forEach(socket =>
145
147
  this.commands.publish(socket, 'stateChange', id, state));
146
148
  }
147
149
  }
148
150
 
149
151
  fileChange(id, fileName, size) {
150
152
  if (this.server && this.server.sockets) {
151
- this.server.sockets.connected.forEach(socket =>
153
+ const sockets = this.server.sockets.sockets || this.server.sockets.connected;
154
+
155
+ sockets.forEach(socket =>
152
156
  this.commands.publishFile(socket, id, fileName, size));
153
157
  }
154
158
  }
155
159
 
156
160
  repoUpdated() {
157
161
  if (this.server && this.server.sockets) {
158
- this.server.sockets.connected.forEach(socket =>
162
+ const sockets = this.server.sockets.sockets || this.server.sockets.connected;
163
+
164
+ sockets.forEach(socket =>
159
165
  this.__updateSession(socket));
160
166
 
161
167
  this.server.sockets.emit('repoUpdated');
@@ -164,7 +170,9 @@ class SocketAdmin extends SocketCommon {
164
170
 
165
171
  objectChange(id, obj) {
166
172
  if (this.server && this.server.sockets) {
167
- this.server.sockets.connected.forEach(socket =>
173
+ const sockets = this.server.sockets.sockets || this.server.sockets.connected;
174
+
175
+ sockets.forEach(socket =>
168
176
  this.commands.publish(socket, 'objectChange', id, obj));
169
177
  }
170
178
  }
@@ -179,7 +187,9 @@ class SocketAdmin extends SocketCommon {
179
187
 
180
188
  sendCommand(obj) {
181
189
  if (this.commands.sendCommand(obj) && this.server && this.server.sockets) {
182
- this.server.sockets.connected.forEach(socket =>
190
+ const sockets = this.server.sockets.sockets || this.server.sockets.connected;
191
+
192
+ sockets.forEach(socket =>
183
193
  this.__updateSession(socket));
184
194
 
185
195
  this.server.sockets.emit(obj.command, obj.message.id, obj.message.data);
@@ -630,8 +630,8 @@ class SocketCommands {
630
630
  zipFiles
631
631
  .writeDirAsZip(
632
632
  this.adapter, // normally we have to pass here the internal "objects" object, but as
633
- // only writeFile is used, and it has the same name we can pass here the
634
- // adapter, which has the function with the same name and arguments
633
+ // only writeFile is used, and it has the same name we can pass here the
634
+ // adapter, which has the function with the same name and arguments
635
635
  message.id,
636
636
  message.name,
637
637
  Buffer.from(message.data, 'base64'),
@@ -800,7 +800,7 @@ class SocketCommands {
800
800
 
801
801
  this.commands['renameFile'] = (socket, _adapter, oldName, newName, callback) => {
802
802
  if (this._checkPermissions(socket, 'rename', callback, oldName)) {
803
- this.adapter._rename(_adapter, oldName, newName, {user: socket._acl.user}, (err, ...args) =>
803
+ this.adapter.rename(_adapter, oldName, newName, {user: socket._acl.user}, (err, ...args) =>
804
804
  SocketCommands._fixCallback(callback, err, ...args));
805
805
  }
806
806
  };
@@ -568,11 +568,9 @@ class SocketCommandsAdmin extends SocketCommands {
568
568
  }
569
569
  };
570
570
  this.commands['changePassword'] = (socket, user, pass, callback) => {
571
- if (this.__updateSession(socket)) {
572
- if (user === socket._acl.user || this._checkPermissions(socket, 'changePassword', callback, user)) {
573
- this.adapter.setPassword(user, pass, {user: socket._acl.user}, (err, ...args) =>
574
- SocketCommands._fixCallback(callback, err, ...args));
575
- }
571
+ if (user === socket._acl.user || this._checkPermissions(socket, 'changePassword', callback, user)) {
572
+ this.adapter.setPassword(user, pass, {user: socket._acl.user}, (err, ...args) =>
573
+ SocketCommands._fixCallback(callback, err, ...args));
576
574
  }
577
575
  };
578
576
  }
@@ -67,7 +67,14 @@ class SocketCommon {
67
67
  // it can be used as client too for cloud
68
68
  if (socketClass) {
69
69
  if (!server.__inited) {
70
- this.server = socketClass.listen(server, socketOptions);
70
+ if (typeof socketClass.listen === 'function') {
71
+ // old socket.io@2.x and ws
72
+ this.server = socketClass.listen(server, socketOptions);
73
+ } else {
74
+ // socket.io 4.x
75
+ this.server = socketClass(server, socketOptions);
76
+ }
77
+
71
78
  server.__inited = true;
72
79
  this.adapter.log.info(`${this.settings.secure ? 'Secure ' : ''}socket.io server listening on port ${this.settings.port}`);
73
80
  }
@@ -77,6 +84,7 @@ class SocketCommon {
77
84
  }
78
85
 
79
86
  // Enable cross domain access
87
+ // deprecated, because no more used in socket.io@4 only(in @2)
80
88
  if (this.settings.crossDomain && this.server.set) {
81
89
  this.server.set('origins', '*:*');
82
90
  }
@@ -141,8 +149,10 @@ class SocketCommon {
141
149
  _unsubscribeAll() {
142
150
  if (this.server && this.server.ioBroker) {
143
151
  // this could be an object or array
144
- Object.keys(this.server.sockets.connected).forEach(i =>
145
- this.commands.unsubscribeSocket(this.server.sockets.connected[i]));
152
+ const sockets = this.server.sockets.sockets || this.server.sockets.connected;
153
+
154
+ Object.keys(sockets).forEach(i =>
155
+ this.commands.unsubscribeSocket(sockets[i]));
146
156
  } else
147
157
  if (this.server && this.server.sockets) {
148
158
  for (const socket in this.server.sockets) {
@@ -320,8 +330,10 @@ class SocketCommon {
320
330
  let clientsArray = [];
321
331
  if (this.server.sockets) {
322
332
  // this could be an object or array
323
- Object.keys(this.server.sockets.connected).forEach(i =>
324
- clientsArray.push(this.server.sockets.connected[i]._name || 'noname'));
333
+ const sockets = this.server.sockets.sockets || this.server.sockets.connected;
334
+
335
+ Object.keys(sockets).forEach(i =>
336
+ clientsArray.push(sockets[i]._name || 'noname'));
325
337
  }
326
338
  const text = `[${clientsArray.length}]${clientsArray.join(', ')}`;
327
339
  this.adapter.setState('info.connected', text, true);
@@ -360,8 +372,10 @@ class SocketCommon {
360
372
 
361
373
  if (this.server && this.server.sockets) {
362
374
  // this could be an object or array
363
- Object.keys(this.server.sockets.connected).forEach(i => {
364
- const socket = this.server.sockets.connected[i];
375
+ const sockets = this.server.sockets.sockets || this.server.sockets.connected;
376
+
377
+ Object.keys(sockets).forEach(i => {
378
+ const socket = sockets[i];
365
379
  if (socket._sessionTimer) {
366
380
  clearTimeout(socket._sessionTimer);
367
381
  socket._sessionTimer = null;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@iobroker/socket-classes",
3
- "version": "0.3.2",
3
+ "version": "0.4.2",
4
4
  "description": "ioBroker server-side web sockets",
5
5
  "author": {
6
6
  "name": "bluefox",