Skip to content

Commit

Permalink
Merge pull request #169 from sockjs/ws-ping
Browse files Browse the repository at this point in the history
#129, #162 add ws ping-pong and close if no response
  • Loading branch information
brycekahle committed May 30, 2015
2 parents 7c8bfd2 + 8c12b38 commit c7f9a5f
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 @@ -52,6 +52,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 @@ -90,6 +91,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 @@ -163,7 +163,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 @@ -266,6 +266,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 c7f9a5f

Please sign in to comment.