go-socket.io icon indicating copy to clipboard operation
go-socket.io copied to clipboard

Connection constantly broken

Open Fransebas opened this issue 6 years ago • 5 comments

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.

Fransebas avatar Jun 28 '19 15:06 Fransebas

Sorry, I'm a clam 🤦🏼‍♂️. I need to use version 1.4 Currently this library supports 1.4 version

Fransebas avatar Jun 28 '19 15:06 Fransebas

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 =(.

Fransebas avatar Jun 28 '19 20:06 Fransebas

i get too

Eric-art-coder avatar Jul 01 '19 06:07 Eric-art-coder

i got the same problem, messages lost constantly

zannet avatar Jul 18 '19 11:07 zannet

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

jubeiam avatar Feb 28 '23 20:02 jubeiam