rootpd / go-binance

Go wrapper for Binance API
MIT License
33 stars 121 forks source link

Websocket connections getting closed #2

Open adrpino opened 6 years ago

adrpino commented 6 years ago

I am attempting to run the example and when reading data from the websocket I get the following error:

level=error time=2018-05-17T21:12:47.556428787Z caller=service_websocket.go:35 wsRead="websocket: close 1008 (policy violation): {\"code\":-1,\"msg\":\"Illegal format ws or stream\"}"

Has the API changed in some way and some adaptations are required? I remember using this package months ago and worked like a bliss.

If you need help with it I'm willing to give a hand

rootpd commented 6 years ago

Hi, to be honest I have no idea. It's a bit more alive at official fork here https://github.com/binance-exchange/go-binance/ - at least PR-wise.

I haven't seen or used the APIs for a while now but looking at the error it looks that something with Websocket APIs really changed. I'll keep this open just in case I got back to this some day.

theMattCode commented 6 years ago

Got that error, too. Issues are not activated in the fork project.

sinyakinilya commented 6 years ago

This error/issue happens when goroutine exitHandler sends message 1 second.

        case t := <-ticker.C:
            err := c.WriteMessage(websocket.TextMessage, []byte(t.String()))
            if err != nil {
                level.Error(as.Logger).Log("wsWrite", err)
                return
            }

It works fine without "send" message

Jolly23 commented 6 years ago

@sinyakinilya How to fix?

Jolly23 commented 6 years ago

@rootpd @adrpino @sinyakinilya @Betazoid If I delete service_websocket.go line 414~419, it works fine. But I do not know what error will occur since I deleted that part of codes.

crlrfzj commented 6 years ago

I use conn.NextWriter instead of conn.WriteMessage to write msg and it works well.

w, err := conn.NextWriter(websocket.TextMessage)
if err != nil {
    logger.Error(err.Error())
    return
}
w.Write([]byte(msg))
kiennt23 commented 5 years ago

I use conn.NextWriter instead of conn.WriteMessage to write msg and it works well.

w, err := conn.NextWriter(websocket.TextMessage)
if err != nil {
  logger.Error(err.Error())
  return
}
w.Write([]byte(msg))

Has anyone fixed this issue? I tried using the fix from @crlrfzj but it doesn't work.

jloup commented 5 years ago

exitHandler in service_websocket.go is sending unsollicited text message to server every second.

I do not see the need for sending this message every second.

Binance api docs says :

The websocket server will send a ping frame every 3 minutes. If the websocket server does not receive a pong frame back from the connection within a 10 minute period, the connection will be disconnected. Unsolicited pong frames are allowed

But gorilla websocket handle PING/PONG frames on its own.

Two solutions that worked for me :

kiennt23 commented 5 years ago

Remove ticker is a better solution IMO. I created a PR for the fix

filinvadim commented 4 years ago

Removed it. I support this repo here: https://github.com/filinvadim/go-binance

Update: i have special wss client for echange wss endpoints - https://github.com/filinvadim/stubborn. Welcome!