Skip to content

Commit

Permalink
#129, #162 add ws ping-pong and close if no response
Browse files Browse the repository at this point in the history
This utilizes the web socket protocol ping-pong op codes. If the
connection supports them, it will send them instead of heartbeat
frames. If the client doesn’t respond within 10 seconds, it considers
the connection closed.
  • Loading branch information
brycekahle committed Oct 25, 2014
1 parent f80bf1a commit 8c12b38
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
13 changes: 13 additions & 0 deletions src/trans-websocket.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class WebSocketReceiver extends transport.GenericReceiver
@connection.setNoDelay(true)
catch x
@ws.addEventListener('message', (m) => @didMessage(m.data))
@heartbeat_cb = => @heartbeat_timeout()
super @connection

setUp: ->
Expand Down Expand Up @@ -88,6 +89,18 @@ class WebSocketReceiver extends transport.GenericReceiver
@ws = null
@connection = null

heartbeat: ->
supportsHeartbeats = @ws.ping null, ->
clearTimeout(hto_ref)

if supportsHeartbeats
hto_ref = setTimeout(@heartbeat_cb, 10000)
else
super

heartbeat_timeout: ->
@session.close(3000, 'No response from heartbeat')



Transport = transport.Transport
Expand Down
5 changes: 4 additions & 1 deletion src/transport.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ class Session
x = =>
if @recv
@to_tref = setTimeout(x, @heartbeat_delay)
@recv.doSendFrame("h")
@recv.heartbeat()
@to_tref = setTimeout(x, @heartbeat_delay)
return

Expand Down Expand Up @@ -264,6 +264,9 @@ class GenericReceiver
utils.quote(m)
@doSendFrame('a' + '[' + q_msgs.join(',') + ']')

heartbeat: ->
@doSendFrame('h')


# Write stuff to response, using chunked encoding if possible.
class ResponseReceiver extends GenericReceiver
Expand Down

0 comments on commit 8c12b38

Please sign in to comment.