@@ -30,6 +30,9 @@ const wsInitialPingTime = 1 * time.Second
30
30
31
31
const DefaultCommandTimeout = 2 * time .Second
32
32
33
+ var GlobalLock = & sync.Mutex {}
34
+ var RouteToConnMap = map [string ]string {} // routeid => connid
35
+
33
36
func RunWebSocketServer (listener net.Listener ) {
34
37
gr := mux .NewRouter ()
35
38
gr .HandleFunc ("/ws" , HandleWs )
@@ -240,6 +243,31 @@ func WriteLoop(conn *websocket.Conn, outputCh chan any, closeCh chan any, routeI
240
243
}
241
244
}
242
245
246
+ func registerConn (wsConnId string , routeId string , wproxy * wshutil.WshRpcProxy ) {
247
+ GlobalLock .Lock ()
248
+ defer GlobalLock .Unlock ()
249
+ curConnId := RouteToConnMap [routeId ]
250
+ if curConnId != "" {
251
+ log .Printf ("[websocket] warning: replacing existing connection for route %q\n " , routeId )
252
+ wshutil .DefaultRouter .UnregisterRoute (routeId )
253
+ }
254
+ RouteToConnMap [routeId ] = wsConnId
255
+ wshutil .DefaultRouter .RegisterRoute (routeId , wproxy )
256
+ }
257
+
258
+ func unregisterConn (wsConnId string , routeId string ) {
259
+ GlobalLock .Lock ()
260
+ defer GlobalLock .Unlock ()
261
+ curConnId := RouteToConnMap [routeId ]
262
+ if curConnId != wsConnId {
263
+ // only unregister if we are the current connection (otherwise we were already removed)
264
+ log .Printf ("[websocket] warning: trying to unregister connection %q for route %q but it is not the current connection (ignoring)\n " , wsConnId , routeId )
265
+ return
266
+ }
267
+ delete (RouteToConnMap , routeId )
268
+ wshutil .DefaultRouter .UnregisterRoute (routeId )
269
+ }
270
+
243
271
func HandleWsInternal (w http.ResponseWriter , r * http.Request ) error {
244
272
windowId := r .URL .Query ().Get ("windowid" )
245
273
if windowId == "" {
@@ -261,23 +289,19 @@ func HandleWsInternal(w http.ResponseWriter, r *http.Request) error {
261
289
wsConnId := uuid .New ().String ()
262
290
outputCh := make (chan any , 100 )
263
291
closeCh := make (chan any )
264
- eventbus .RegisterWSChannel (wsConnId , windowId , outputCh )
265
292
var routeId string
266
293
if windowId == wshutil .ElectronRoute {
267
294
routeId = wshutil .ElectronRoute
268
295
} else {
269
296
routeId = wshutil .MakeWindowRouteId (windowId )
270
297
}
271
- defer eventbus .UnregisterWSChannel (wsConnId )
272
298
log .Printf ("[websocket] new connection: windowid:%s connid:%s routeid:%s\n " , windowId , wsConnId , routeId )
273
- // we create a wshproxy to handle rpc messages to/from the window
274
- wproxy := wshutil .MakeRpcProxy ()
275
- wshutil .DefaultRouter .RegisterRoute (routeId , wproxy )
276
- defer func () {
277
- wshutil .DefaultRouter .UnregisterRoute (routeId )
278
- close (wproxy .ToRemoteCh )
279
- }()
280
- // WshServerFactoryFn(rpcInputCh, rpcOutputCh, wshrpc.RpcContext{})
299
+ eventbus .RegisterWSChannel (wsConnId , windowId , outputCh )
300
+ defer eventbus .UnregisterWSChannel (wsConnId )
301
+ wproxy := wshutil .MakeRpcProxy () // we create a wshproxy to handle rpc messages to/from the window
302
+ defer close (wproxy .ToRemoteCh )
303
+ registerConn (wsConnId , routeId , wproxy )
304
+ defer unregisterConn (wsConnId , routeId )
281
305
wg := & sync.WaitGroup {}
282
306
wg .Add (2 )
283
307
go func () {
0 commit comments