Go Gorilla Websockets ~ On ping/pong fail (user disconnct) call function?

Acidic9 picture Acidic9 · Jun 8, 2016 · Viewed 10.7k times · Source

I'm writing a chat program with Golang and Gorilla's Websocket toolkit.

I'm wondering if there is a way to run a function whenever a user disconnects or a ping/pong message fails. I need this to remove them from variables and such. Is there any easy way to do this?

Answer

Cerise Limón picture Cerise Limón · Jun 8, 2016

The application should close the connection and cleanup variables and such when the read methods (NextReader, ReadMessage) return an error.

Use ping/pong to detect disconnects. The chat example shows how to do this.

If a pong goes missing, the read methods will return with the read past deadline error.