@@ -72,11 +72,31 @@ func (s *Server) broadcastNamespaces() {
72
72
func StartServer () {
73
73
server = NewServer ()
74
74
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
+ )
80
97
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
+ }
82
102
}
0 commit comments