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

Ping msg timeout detection #37

Open
cert-lv opened this issue Feb 20, 2018 · 5 comments
Open

Ping msg timeout detection #37

cert-lv opened this issue Feb 20, 2018 · 5 comments

Comments

@cert-lv
Copy link

cert-lv commented Feb 20, 2018

Hi,

Melody has a ping-pong functionality to keep the connection open and there is a config option PongWait. But how can I detect timeouts? For example, if client's network connection is or becomes too slow - how can I detect it with Melody?

@shiwano
Copy link
Contributor

shiwano commented Feb 21, 2018

Hi, @cert-lv. You are a guy who sent email to me about the issue, aren't you? 😁

If you want to detect session's timeout that is occured by ping-pong, I think that it would be detected in using HandlePong method and HandleClose method in combination.

@cert-lv
Copy link
Author

cert-lv commented Feb 21, 2018

Hello, @shiwano.

I was thinking more like about low latency detection, when client connection is slow, but still exists, not closed.

Current solution could be calling m.Broadcast(msg) to send an additional message with timestamp as payload, in a loop. Then in HandleMessage() check the diff with current timestamp. But then one more ping-like message will exist.

Better solution could be to use already existing ping messages. According to the WebSocket protocol, ping message can include optional payload and client/browser must send back the same payload. Currently Melody doesn't use this payload.

Would you like to include this functionality?

Simplest solution is just to include current timestamp in every ping message. Universal solution is to allow any custom payload. Package users could do then something like:

m.SetPingData(func(s *melody.Session) []byte {
    // TODO: Set timestamp as []bytes here
    return []byte("hello")
})

... to set their custom payloads. After that process incoming data:

m.HandlePong(func(s *melody.Session, data []byte) {
    log.Println(string(data))
})

@shiwano
Copy link
Contributor

shiwano commented Feb 22, 2018

Ah I see! If measuring websocket connection latency is what you want to do, I think that it makes sense to use ping message. Now it can not do, but I also guess it would be good that melody do that.

https://github.com/vtortola/WebSocketListener/wiki/Measuring-WebSockets-connection-latency
Actually, it seems that there is also a websocket library which provides like the feature.

@SouL-H
Copy link

SouL-H commented Nov 21, 2023

There is still no concrete solution, is there?

@leonharetd
Copy link

s.writeRaw(&envelope{t: websocket.PingMessage, msg: []byte{}})

I can't get the message after the browser sends it, but I implemented a client using golang that finds that it can receive the message,but it was a null value,A null value causes the browser to filter out the message。

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

No branches or pull requests

4 participants