diff --git a/src/trans-websocket.coffee b/src/trans-websocket.coffee index 1514c623..f4583cbc 100644 --- a/src/trans-websocket.coffee +++ b/src/trans-websocket.coffee @@ -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: -> @@ -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 diff --git a/src/transport.coffee b/src/transport.coffee index d04fe91b..b99db865 100644 --- a/src/transport.coffee +++ b/src/transport.coffee @@ -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 @@ -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