Update frontend/js/websocket-client.js
Browse files
frontend/js/websocket-client.js
CHANGED
|
@@ -34,13 +34,23 @@ export class WebSocketClient {
|
|
| 34 |
this.websocket.onopen = () => {
|
| 35 |
console.log("WebSocket connection established");
|
| 36 |
|
| 37 |
-
//
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
|
| 44 |
};
|
| 45 |
|
| 46 |
this.websocket.onmessage = (event) => {
|
|
@@ -132,4 +142,4 @@ export class WebSocketClient {
|
|
| 132 |
this.websocket = null;
|
| 133 |
}
|
| 134 |
}
|
| 135 |
-
|
|
|
|
| 34 |
this.websocket.onopen = () => {
|
| 35 |
console.log("WebSocket connection established");
|
| 36 |
|
| 37 |
+
// Guard to make sure the socket state has fully transitioned to OPEN
|
| 38 |
+
const sendInitialConfig = () => {
|
| 39 |
+
if (this.websocket.readyState === WebSocket.OPEN) {
|
| 40 |
+
this.websocket.send(JSON.stringify({
|
| 41 |
+
type: 'config',
|
| 42 |
+
config: config
|
| 43 |
+
}));
|
| 44 |
+
resolve();
|
| 45 |
+
} else if (this.websocket.readyState === WebSocket.CONNECTING) {
|
| 46 |
+
// If still buffering, defer execution to a micro-task loop
|
| 47 |
+
setTimeout(sendInitialConfig, 10);
|
| 48 |
+
} else {
|
| 49 |
+
reject(new Error("WebSocket closed prematurely during handshake configuration."));
|
| 50 |
+
}
|
| 51 |
+
};
|
| 52 |
|
| 53 |
+
sendInitialConfig();
|
| 54 |
};
|
| 55 |
|
| 56 |
this.websocket.onmessage = (event) => {
|
|
|
|
| 142 |
this.websocket = null;
|
| 143 |
}
|
| 144 |
}
|
| 145 |
+
}
|