Spaces:
Running

michdubai commited on
Commit
4d80984
·
verified ·
1 Parent(s): 12538f6

Update frontend/js/websocket-client.js

Browse files
Files changed (1) hide show
  1. frontend/js/websocket-client.js +17 -7
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
- // Send initial configuration
38
- this.websocket.send(JSON.stringify({
39
- type: 'config',
40
- config: config
41
- }));
 
 
 
 
 
 
 
 
 
 
42
 
43
- resolve();
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
+ }