From 8c12b386d36f6683e80cb64187af0f9498135852 Mon Sep 17 00:00:00 2001 From: Bryce Kahle Date: Sat, 25 Oct 2014 17:18:29 -0400 Subject: [PATCH] #129, #162 add ws ping-pong and close if no response MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- src/trans-websocket.coffee | 13 +++++++++++++ src/transport.coffee | 5 ++++- 2 files changed, 17 insertions(+), 1 deletion(-) 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