Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Websocket timeout #29

Closed
ghost opened this issue Mar 5, 2018 · 14 comments
Closed

Websocket timeout #29

ghost opened this issue Mar 5, 2018 · 14 comments

Comments

@ghost
Copy link

ghost commented Mar 5, 2018

Hi adshao, first of all thanks for your work. Really appreaciated. I just wanted to ask you if there's a connection timeout or ping/pong mechanism to ensure connection stability?

@adshao
Copy link
Owner

adshao commented Mar 5, 2018

hi @davidklodner , thanks for the issue.
for connection timeout, do you mean readdealine as in https://godoc.org/github.com/gorilla/websocket#Conn.SetReadDeadline , I think we can add it as a websocket config; while for ping/pong handler, I cannot find any info about it from binance api doc, and I tested it with https://godoc.org/github.com/gorilla/websocket#Conn.SetPingHandler and https://godoc.org/github.com/gorilla/websocket#Conn.SetPongHandler, there is nothing sent from binance server, I think it's fine to ignore them.
Do you have any better suggestion?

@ghost
Copy link
Author

ghost commented Mar 7, 2018

@adshao Try setting a pong handler and sending the server a ping. If the server correctly implements the websocket protocol, then the application should receive a pong. This may not be documented because it's at the Websocket protocol level, not part of the application.

@adshao
Copy link
Owner

adshao commented Mar 7, 2018

@claudia-jones thanks for the update, you are right, we can periodically send ping message to server, and check pong message for connection stability.

@kustov-an
Copy link
Contributor

Hi! Any updates on this?

@adshao
Copy link
Owner

adshao commented May 7, 2018

hi @kustov-an , will implement it soon.

@adshao
Copy link
Owner

adshao commented May 14, 2018

I don't think we need ping/pong handler here, instead, HandshakeTimeout https://godoc.org/github.com/gorilla/websocket#Dialer and CloseHandler https://godoc.org/github.com/gorilla/websocket#Conn.SetCloseHandler are enough to check connection stability.
Since these changes will break the compatibility, we need to refactor the web socket interfaces.

@kustov-an
Copy link
Contributor

Correct me if I wrong, but as I understand

  1. HandshakeTimeout is only used when establishing a new connection
  2. CloseHandler is executed when the connection is closed by the peer
    So those changes are not going to help when the connection is lost due to network problems?

@adshao
Copy link
Owner

adshao commented May 14, 2018

hi @kustov-an , if the connection is lost, ReadMessage will return error: https://github.com/adshao/go-binance/blob/master/websocket.go#L45, so we can use errHandler here.

@kustov-an
Copy link
Contributor

Hi @adshao, please take a look at my pull request #50 addressing this issue

@adshao
Copy link
Owner

adshao commented Jun 11, 2018

close as solved

@adshao adshao closed this as completed Jun 11, 2018
@aleibovici
Copy link

@adshao your project has been a tremendous help. Much appreciated. I recently started seeing many "websocket: close 1008 (policy violation): Pong timeout" messages. I see that this case is closed, but I wonder if this is something you or anyone else is seeing. My code simply restart the a Websocket, but I wonder if I should be doing something diferent.

@snawaz
Copy link

snawaz commented Jul 13, 2022

@adshao your project has been a tremendous help. Much appreciated. I recently started seeing many "websocket: close 1008 (policy violation): Pong timeout" messages. I see that this case is closed, but I wonder if this is something you or anyone else is seeing. My code simply restart the a Websocket, but I wonder if I should be doing something diferent.

this is because #50 does not fix the problem, as it sends ping message whereas the client needs to send pong message. As the per the binance docs, it's the server which sends ping to the client and then if the client does not respond with a pong message within 10 mins, then the server will disconnect the connection.

@goriunov
Copy link

For future readers, as per @snawaz looks like if you set custom ping handler it will not automatically send pong based on the Gorilla websocket implementation. You will need to send pong back by yourself from within your custom ping handler.

image

This primarily required for the implementation on the gorilla websocket with custom handler. Note that if no handler is passed or no SetPingHandler called, it will default to the default ping handler

@snawaz
Copy link

snawaz commented Apr 22, 2024

For future readers, as per @snawaz looks like if you set custom ping handler it will not automatically send pong based on the Gorilla websocket implementation. You will need to send pong back by yourself from within your custom ping handler.

That's correct. I have a custom ping handler which sends websocket.PongMessage, as soon as it receives the PingMessage.

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants