olahol / melody

:notes: Minimalist websocket framework for Go
BSD 2-Clause "Simplified" License
3.76k stars 365 forks source link

Ping msg timeout detection #37

Open cert-lv opened 6 years ago

cert-lv commented 6 years ago

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 commented 6 years ago

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

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 commented 6 years ago

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 commented 6 years ago

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 commented 1 year ago

There is still no concrete solution, is there?

leonharetd commented 8 months ago

https://github.com/olahol/melody/blob/87e736d0f6d527ba7df20de8bb5977b70f2897a3/session.go#L71 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。