go-socket.io
go-socket.io copied to clipboard
Connection constantly broken
Setup
I'm connecting a Reactjs page with a Golang server, for the front-end, I'm using. socket.io-client": "1.7.4" to prevent any possible error with version 2.0. Both the page and the server are running on the same machine.
Client:
const socket = io.connect('ws://localhost:2000', {
//forceNew : false,
transports: ['websocket'],
reconnection : true,
});
Server:
func Constructor() (s SocketIOService) {
server, e := socketio.NewServer(nil)
s.Server = server
s.Sessions = make(map[string]*Session)
CustomUtils.CheckPanic(e, "Could not initiate socket io server")
server.OnConnect("/", func(socket socketio.Conn) error {
socket.SetContext("")
fmt.Println("connected:", socket.ID())
if _, ok := s.Sessions[socket.ID()]; !ok {
session := newSession(&socket)
s.Sessions[socket.ID()] = session
} else {
fmt.Println("Connection already exist")
}
return nil
})
server.OnError("/", func(e error) {
fmt.Println("meet error:", e)
})
server.OnDisconnect("/", func(s socketio.Conn, msg string) {
fmt.Println("closed", msg)
})
s.InitEvents()
go s.Server.Serve()
return s
}
// This where the entry connections are served, I do this to remove CORS issues
func (s *SocketIOService) SocketIOFix(w http.ResponseWriter, r *http.Request) {
allowHeaders := "Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization"
if origin := r.Header.Get("Origin"); origin != "" {
w.Header().Set("Access-Control-Allow-Origin", origin)
w.Header().Set("Vary", "Origin")
w.Header().Set("Access-Control-Allow-Methods", "POST, PUT, PATCH, GET, DELETE")
w.Header().Set("Access-Control-Allow-Credentials", "true")
w.Header().Set("Access-Control-Allow-Headers", allowHeaders)
}
if r.Method == "OPTIONS" {
return
}
r.Header.Del("Origin")
s.Server.ServeHTTP(w,r)
}
What happens
Each time the web page try's to connect to the server, around 9 connections are created and destroyed until a connection is successful. This causes me problems because I do some compute-intensive processing each time a new connection is made. I think I'm doing something wrong in the configuration because I think that the socket should try to reconnect using the same id but it doesn't
Also, the connection is broken multiple times after.
Sorry, I'm a clam 🤦🏼♂️. I need to use version 1.4
Currently this library supports 1.4 version
I change my client version to 1.4.0 and also tried with 1.4.8 and I still have the error, can someone help me =(.
i get too
i got the same problem, messages lost constantly
I did run Your code in example with gin-gonic and https://cdn.socket.io/socket.io-1.7.4.js and there was no error during connection exept failed connect to namespace without handler as the client was trying to connect to "/chat" namespace