Skip to content

Commit 8d4788b

Browse files
update socket config
1 parent 2052dc7 commit 8d4788b

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

api/server.go

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,31 @@ func (s *Server) broadcastNamespaces() {
7272
func StartServer() {
7373
server = NewServer()
7474

75-
handler := cors.Default().Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
76-
if r.URL.Path == "/ws" {
77-
websocket.Handler(server.handleConn).ServeHTTP(w, r)
78-
}
79-
}))
75+
corsOptions := cors.Options{
76+
AllowedOrigins: []string{"http://localhost:5173", "ws://localhost:8080"},
77+
AllowedMethods: []string{"GET", "POST", "OPTIONS"},
78+
AllowedHeaders: []string{"*"},
79+
Debug: true,
80+
}
81+
handler := cors.New(corsOptions).Handler(
82+
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
83+
if r.URL.Path == "/ws" {
84+
wsHandler := websocket.Server{
85+
Handler: func(ws *websocket.Conn) {
86+
server.handleConn(ws)
87+
},
88+
Handshake: func(config *websocket.Config, r *http.Request) error {
89+
config.Origin, _ = websocket.Origin(config, r)
90+
return nil
91+
},
92+
}
93+
wsHandler.ServeHTTP(w, r)
94+
}
95+
}),
96+
)
8097

81-
http.ListenAndServe(":8080", handler)
98+
log.Println("Starting WebSocket server on :8080")
99+
if err := http.ListenAndServe(":8080", handler); err != nil {
100+
log.Fatal("ListenAndServe:", err)
101+
}
82102
}

0 commit comments

Comments
 (0)