Skip to content

Commit 8c12b38

Browse files
committed
#129, #162 add ws ping-pong and close if no response
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.
1 parent f80bf1a commit 8c12b38

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/trans-websocket.coffee

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ class WebSocketReceiver extends transport.GenericReceiver
5050
@connection.setNoDelay(true)
5151
catch x
5252
@ws.addEventListener('message', (m) => @didMessage(m.data))
53+
@heartbeat_cb = => @heartbeat_timeout()
5354
super @connection
5455

5556
setUp: ->
@@ -88,6 +89,18 @@ class WebSocketReceiver extends transport.GenericReceiver
8889
@ws = null
8990
@connection = null
9091

92+
heartbeat: ->
93+
supportsHeartbeats = @ws.ping null, ->
94+
clearTimeout(hto_ref)
95+
96+
if supportsHeartbeats
97+
hto_ref = setTimeout(@heartbeat_cb, 10000)
98+
else
99+
super
100+
101+
heartbeat_timeout: ->
102+
@session.close(3000, 'No response from heartbeat')
103+
91104

92105

93106
Transport = transport.Transport

src/transport.coffee

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ class Session
158158
x = =>
159159
if @recv
160160
@to_tref = setTimeout(x, @heartbeat_delay)
161-
@recv.doSendFrame("h")
161+
@recv.heartbeat()
162162
@to_tref = setTimeout(x, @heartbeat_delay)
163163
return
164164

@@ -264,6 +264,9 @@ class GenericReceiver
264264
utils.quote(m)
265265
@doSendFrame('a' + '[' + q_msgs.join(',') + ']')
266266

267+
heartbeat: ->
268+
@doSendFrame('h')
269+
267270

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

0 commit comments

Comments
 (0)