How to detect whether websocket client is reachable

2,027 views
Skip to first unread message

zeroc8

unread,
Jun 19, 2012, 10:59:24 AM6/19/12
to golan...@googlegroups.com
Hi, how can I detect whether a websocket client is reachable, for example if somebody pulls the client's network cable.

Do I need to call SetDeadline on the websocket and then have the client send periodical keep alive messages?
Or is there a better way to accomplish this?

zeroc8

unread,
Jun 20, 2012, 10:34:32 AM6/20/12
to golan...@googlegroups.com

Well, I'm setting SetReadDeadline on the websocket and make sure the client sends a packet in a predefined interval that is shorter than the SetReadDeadline timeout.
It works, but causes a lot of network traffic. Still wondering if there's a better way.
 

jason

unread,
Jun 20, 2012, 3:47:20 PM6/20/12
to golan...@googlegroups.com
With read timeout you'll always have to wait the Max time to detect a lost client, were writing a periodic message to the client should tell you the connection was lost must sooner.

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.

zeroc8

unread,
Jun 21, 2012, 8:15:42 AM6/21/12
to golan...@googlegroups.com
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))
                    err := websocket.JSON.Send(subscr.ws,msg)                    
                    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.

John Beisley

unread,
Jun 21, 2012, 10:08:47 AM6/21/12
to zeroc8, golan...@googlegroups.com
Perhaps the data you are sending is small enough to fit into the
kernel network buffers and/or TCP transmit window. That is - it's not
a blocking operation for as long as 300ms.

You're sending the data, but you don't know that it reaches the
destination by setting a deadline.

Aiden

unread,
Mar 5, 2014, 2:50:50 PM3/5/14
to golan...@googlegroups.com
Did you ever get a solution to this? I have a similar situation with Send/Write on a websocket not giving an
error when the client is disconnected.
Reply all
Reply to author
Forward
0 new messages