From 7f07dbca2f7a306f991140743578d5806cfabc92 Mon Sep 17 00:00:00 2001 From: mircearoata Date: Wed, 24 Jul 2024 19:24:22 +0200 Subject: [PATCH] Handle inaccessible websocket port --- backend/websocket/websocket.go | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/backend/websocket/websocket.go b/backend/websocket/websocket.go index d863b883..14f2cbe0 100644 --- a/backend/websocket/websocket.go +++ b/backend/websocket/websocket.go @@ -2,6 +2,7 @@ package websocket import ( "log/slog" + "net/http" "github.com/spf13/viper" engineio_types "github.com/zishang520/engine.io/types" @@ -11,13 +12,19 @@ import ( ) func ListenAndServeWebsocket() { - httpServer := engineio_types.CreateServer(nil) + httpMux := http.NewServeMux() + + httpServer := &http.Server{ + Addr: "localhost:" + viper.GetString("websocket-port"), + Handler: httpMux, + } + options := &socket.ServerOptions{} options.SetCors(&engineio_types.Cors{ Origin: true, // Allow any origin }) io := socket.NewServer(nil, options) - httpServer.Handle("/socket.io", io.ServeHandler(nil)) + httpMux.Handle("/socket.io/", io.ServeHandler(nil)) _ = io.On("connection", func(data ...any) { client := data[0].(*socket.Socket) @@ -39,5 +46,8 @@ func ListenAndServeWebsocket() { }) }) - httpServer.Listen("localhost:"+viper.GetString("websocket-port"), nil) + err := httpServer.ListenAndServe() + if err != nil { + slog.Error("failed to start websocket server", slog.Any("err", err)) + } }