@soundworks/core 4.0.0-alpha.24 → 4.0.0-alpha.26

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/CHANGELOG.md CHANGED
@@ -1,3 +1,11 @@
1
+ ## v4.0.0-alpha.26 - 05/09/2025
2
+
3
+ - Fix: improve reconnect strategy in case of socket connection failure
4
+
5
+ ## v4.0.0-alpha.25 - 24/05/2025
6
+
7
+ - Fix: better default behavior for websocket connection URL
8
+
1
9
  ## v4.0.0-alpha.24 - 24/05/2025
2
10
 
3
11
  - Fix: unexpected behavior with new `serverAddress` behavior on browser clients and self-signed certificates
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@soundworks/core",
3
- "version": "4.0.0-alpha.24",
3
+ "version": "4.0.0-alpha.26",
4
4
  "description": "Open-source creative coding framework for distributed applications based on Web technologies",
5
5
  "authors": [
6
6
  "Benjamin Matuszewski",
@@ -62,11 +62,11 @@ class Socket {
62
62
 
63
63
  if (isBrowser()) {
64
64
  const hostname = window.location.hostname;
65
- // if a server address is given in config, use it, else fallback to URL hostname
66
- if (config.env.serverAddress !== '') {
67
- serverAddress = config.env.serverAddress;
68
- } else {
65
+
66
+ if (config.env.serverAddress === '') {
69
67
  serverAddress = hostname;
68
+ } else {
69
+ serverAddress = config.env.serverAddress;
70
70
  }
71
71
 
72
72
  // when in https with self-signed certificates, we don't want to use
@@ -79,14 +79,20 @@ class Socket {
79
79
  }
80
80
 
81
81
  if (config.env.useHttps && window.location.hostname !== serverAddress) {
82
- console.warn(`The WebSocket will try to connect at ${serverAddress} as defined in environment configuration while the page is accessed from ${hostname}. If you run the application with self-signed certificates, this can lead to socket errors as the certificate may not have been accepted for ${serverAddress}. In such case you should rather access the page from ${serverAddress}.`);
82
+ console.warn(`The WebSocket will try to connect at ${serverAddress} while the page is accessed from ${hostname}. This can lead to socket errors, e.g. If you run the application with self-signed certificates as the certificate may not have been accepted for ${serverAddress}. In such case you should rather access the page from ${serverAddress}.`);
83
83
  }
84
84
 
85
85
  webSocketOptions = [];
86
86
  } else {
87
- serverAddress = config.env.serverAddress;
87
+ if (config.env.serverAddress === '') {
88
+ serverAddress = '127.0.0.1';
89
+ } else {
90
+ serverAddress = config.env.serverAddress;
91
+ }
88
92
 
89
93
  webSocketOptions = {
94
+ // handshakeTimeout: 2000,
95
+ // do not reject self-signed certificates
90
96
  rejectUnauthorized: false,
91
97
  };
92
98
  }
@@ -103,8 +109,6 @@ class Socket {
103
109
  // Init socket
104
110
  // ----------------------------------------------------------
105
111
  return new Promise(resolve => {
106
- let connectionRefusedLogged = false;
107
-
108
112
  const trySocket = async () => {
109
113
  const ws = new WebSocket(url, webSocketOptions);
110
114
 
@@ -114,7 +118,6 @@ class Socket {
114
118
 
115
119
  this.#socket.addEventListener('message', e => {
116
120
  if (e.data === PING_MESSAGE) {
117
- // heartbeat();
118
121
  this.#socket.send(PONG_MESSAGE);
119
122
  // do not propagate ping / pong messages
120
123
  return;
@@ -157,26 +160,20 @@ class Socket {
157
160
 
158
161
  // cf. https://github.com/collective-soundworks/soundworks/issues/17
159
162
  ws.addEventListener('error', e => {
160
- if (e.type === 'error') {
161
- if (ws.terminate) {
162
- ws.terminate();
163
- } else {
164
- ws.close();
165
- }
163
+ if (ws.terminate) {
164
+ ws.terminate();
165
+ } else {
166
+ ws.close();
167
+ }
166
168
 
167
- // for node clients, retry connection
168
- if (e.error && e.error.code === 'ECONNREFUSED') {
169
- // we want to log the warning just once
170
- if (!connectionRefusedLogged) {
171
- logger.log('[soundworks.Socket] Connection refused, waiting for the server to start');
172
- console.log(e.error);
173
- connectionRefusedLogged = true;
174
- }
175
-
176
- // retry in 1 second
177
- setTimeout(trySocket, 1000);
178
- }
169
+ if (e.error) {
170
+ const msg = `[Socket Error] code: ${e.error.code}, message: ${e.error.message}`;
171
+ logger.log(msg);
179
172
  }
173
+
174
+ // Try reconnect in all cases, note that if the socket has been connected the
175
+ // close event will be propagated and the launcher will restart the process.
176
+ setTimeout(trySocket, 1000);
180
177
  });
181
178
  };
182
179
 
@@ -1,2 +1,2 @@
1
1
  // [WARNING] This file is generated, do not edit
2
- export default '4.0.0-alpha.24';
2
+ export default '4.0.0-alpha.26';
@@ -75,7 +75,7 @@ const DEFAULT_CONFIG = {
75
75
  env: {
76
76
  type: 'development',
77
77
  port: 8000,
78
- serverAddress: null,
78
+ serverAddress: '',
79
79
  subpath: '',
80
80
  websockets: {
81
81
  path: 'socket',
@@ -163,7 +163,7 @@ class Server {
163
163
  * env: {
164
164
  * type: 'development',
165
165
  * port: 8000,
166
- * serverAddress: null,
166
+ * serverAddress: '',
167
167
  * subpath: '',
168
168
  * websockets: {
169
169
  * path: 'socket',
@@ -187,16 +187,12 @@ class Server {
187
187
  throw new Error(`[soundworks:Server] Invalid "app.clients" config, at least one client should be declared`);
188
188
  }
189
189
 
190
- // if a node client is defined, serverAddress should be defined
191
- let hasNodeClient = false;
192
- for (let name in this.config.app.clients) {
193
- if (this.config.app.clients[name].target === 'node') {
194
- hasNodeClient = true;
195
- }
196
- }
197
-
198
- if (hasNodeClient && this.config.env.serverAddress === null) {
199
- throw new Error(`[soundworks:Server] Invalid "env.serverAddress" config, is mandatory when a node client target is defined`);
190
+ // @peeka - remove
191
+ // [2024-05-29] Override default `config.env.serverAddress`` provided from
192
+ // template `loadConfig` to '' so that browser clients can default to
193
+ // window.location.hostname and node clients to `127.0.0.1`
194
+ if (process.env.ENV === undefined && this.config.env.serverAddress === '127.0.0.1') {
195
+ this.config.env.serverAddress = '';
200
196
  }
201
197
 
202
198
  if (this.config.env.useHttps && this.config.env.httpsInfos !== null) {