olahol/melody

Ping msg timeout detection

Opened this issue · 5 comments

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?

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.

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

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.

There is still no concrete solution, is there?

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。