I know for TCP connections writing to a broken socket (lost connection) is one of the fastest ways to realize a client has disconnected. This message will also have the side benefit of helping the client keep their connection open longer because some network systems may terminate a connection if no data has been sent/received in a given amount of time.
Well, that's the initial approach I wanted to take. However, this doesn't seem to work. Here's what I'm doing:
for id, subscr := range subscribers {
msg.To = id
subscr.ws.SetWriteDeadline(time.Now().Add(300*time.Millisecond))
if err != nil {
log.Println("Cannot deliver connection checker msg:",msg,err)
} else {
log.Println("Connection checked")
}
}
I thought that I would an io/timeout error here after pulling the cable, but the Send method never returns an error.