go-binance icon indicating copy to clipboard operation
go-binance copied to clipboard

Websocket connections getting closed

Open adrpino opened this issue 6 years ago • 10 comments

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

adrpino avatar May 17 '18 21:05 adrpino

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.

rootpd avatar May 18 '18 13:05 rootpd

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

theMattCode avatar Jun 11 '18 06:06 theMattCode

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

sinyakinilya avatar Jun 18 '18 14:06 sinyakinilya

@sinyakinilya How to fix?

Jolly23 avatar Jun 29 '18 07:06 Jolly23

@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.

Jolly23 avatar Jun 29 '18 08:06 Jolly23

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))

crlrfzj avatar Jul 26 '18 01:07 crlrfzj

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.

kiennt23 avatar Jan 19 '19 00:01 kiennt23

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 :

  • Remove in ticker : err := c.WriteMessage(websocket.TextMessage, []byte("--heartbeat--")) if err != nil { level.Error(as.Logger).Log("wsWrite", err) return }
  • OR replace websocket.TextMessage by websocket.PongMessage as unsolicited pong frames are allowed

jloup avatar Feb 25 '19 17:02 jloup

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

kiennt23 avatar Feb 27 '19 02:02 kiennt23

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!

filinvadim avatar Jul 13 '20 19:07 filinvadim