hesh915 / go-socket.io-client

socket.io client for golang
BSD 3-Clause "New" or "Revised" License
97 stars 68 forks source link

reconnect after disconnect #14

Open jeriveromartinez opened 5 years ago

jeriveromartinez commented 5 years ago

i need keep a stable connection between go client and node server, but after go client read the message this break teh connection.

2019/03/30 17:10:56 on message:hello1553987456281 2019/03/30 17:10:58 on disconnect

how i keep go client connected?

Carminati7 commented 4 years ago

You need to add logic to the connect function you are using, i do try to connect again on disconnect and if the connection fail start to poll every other second. Sample code to help

func connect() {
    opts := &socketio_client.Options{
        Transport: "websocket",
        Query:     make(map[string]string),
    }
    uri := "http://localhost:3001"
    client, err := socketio_client.NewClient(uri, opts)
    if err != nil {
        log.Printf("NewClient error:%v\n", err)
        time.Sleep(2 * time.Second)
        connect()
        return
    }
    client.On("connection", func() {
        log.Printf("on connect\n")
    })
    client.On("hello", func(msg string) {
        log.Printf("on message:%v\n", msg)
    })
    client.On("disconnection", func() {
        connect()
        log.Printf("on disconnect\n")
    })
}