File size: 955 Bytes
56215c3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
const BACKEND_URL = (process.env.NEXT_PUBLIC_BACKEND_URL || "http://localhost:8000").replace(/\/$/, "")

export function createSimulationSocket(
  simulationId: string,
  onMessage: (msg: any) => void,
  onError: () => void
) {
  const wsBase = BACKEND_URL.replace(/^http:/, "ws:").replace(/^https:/, "wss:")
  const wsUrl = `${wsBase}/ws/${simulationId}`
  
  const ws = new WebSocket(wsUrl)
  
  ws.onmessage = (event) => {
    try {
      const msg = JSON.parse(event.data)
      // DEBUG: surface incoming websocket payload for troubleshooting
      try { console.debug("WS_RECV", msg) } catch (e) {}
      onMessage(msg)
    } catch (e) {
      console.error("Failed to parse WebSocket message:", e)
    }
  }
  
  ws.onerror = (error) => {
    console.error("WebSocket error:", error)
    onError()
  }
  
  ws.onclose = (event) => {
    console.log("WebSocket closed", { code: event.code, reason: event.reason })
    onError()
  }
  
  return ws
}