diff options
author | Javan Makhmali <javan@javan.us> | 2015-09-01 08:14:08 -0400 |
---|---|---|
committer | Javan Makhmali <javan@javan.us> | 2015-09-01 08:14:08 -0400 |
commit | 0cf1db6be29fb2269d722bedd690641e0f949b36 (patch) | |
tree | 349ceb6d0c96e25a58d50e4b4d1eb92e4491c1b3 /lib/assets | |
parent | b91d0e8f7d309500df79bffa9017fa2d9fbf2c05 (diff) | |
parent | 60adbaf4af97dc4aa9cd3a96058e0c94f642e911 (diff) | |
download | rails-0cf1db6be29fb2269d722bedd690641e0f949b36.tar.gz rails-0cf1db6be29fb2269d722bedd690641e0f949b36.tar.bz2 rails-0cf1db6be29fb2269d722bedd690641e0f949b36.zip |
Merge branch 'better-reconnect'
Diffstat (limited to 'lib/assets')
-rw-r--r-- | lib/assets/javascripts/cable/connection.js.coffee | 46 | ||||
-rw-r--r-- | lib/assets/javascripts/cable/connection_monitor.js.coffee | 8 |
2 files changed, 24 insertions, 30 deletions
diff --git a/lib/assets/javascripts/cable/connection.js.coffee b/lib/assets/javascripts/cable/connection.js.coffee index 464f0c1ff7..2259ddcedd 100644 --- a/lib/assets/javascripts/cable/connection.js.coffee +++ b/lib/assets/javascripts/cable/connection.js.coffee @@ -11,19 +11,18 @@ class Cable.Connection false open: -> - return if @isState("open", "connecting") - @webSocket = new WebSocket(@consumer.url) - @installEventHandlers() + if @isOpen() + throw new Error("Must close existing connection before opening") + else + @webSocket = new WebSocket(@consumer.url) + @installEventHandlers() close: -> - return if @isState("closed", "closing") @webSocket?.close() reopen: -> - if @isOpen() - @closeSilently => @open() - else - @open() + @close() + @open() isOpen: -> @isState("open") @@ -37,26 +36,10 @@ class Cable.Connection return state.toLowerCase() for state, value of WebSocket when value is @webSocket?.readyState null - closeSilently: (callback = ->) -> - @uninstallEventHandlers() - @installEventHandler("close", callback) - @installEventHandler("error", callback) - try - @webSocket.close() - finally - @uninstallEventHandlers() - installEventHandlers: -> for eventName of @events - @installEventHandler(eventName) - - installEventHandler: (eventName, handler) -> - handler ?= @events[eventName].bind(this) - @webSocket.addEventListener(eventName, handler) - - uninstallEventHandlers: -> - for eventName of @events - @webSocket.removeEventListener(eventName) + handler = @events[eventName].bind(this) + @webSocket["on#{eventName}"] = handler events: message: (event) -> @@ -64,14 +47,19 @@ class Cable.Connection @consumer.subscriptions.notify(identifier, "received", message) open: -> + @disconnected = false @consumer.subscriptions.reload() close: -> - @consumer.subscriptions.notifyAll("disconnected") + @disconnect() error: -> - @consumer.subscriptions.notifyAll("disconnected") - @closeSilently() + @disconnect() + + disconnect: -> + return if @disconnected + @disconnected = true + @consumer.subscriptions.notifyAll("disconnected") toJSON: -> state: @getState() diff --git a/lib/assets/javascripts/cable/connection_monitor.js.coffee b/lib/assets/javascripts/cable/connection_monitor.js.coffee index 5573ea5a77..30ce11957c 100644 --- a/lib/assets/javascripts/cable/connection_monitor.js.coffee +++ b/lib/assets/javascripts/cable/connection_monitor.js.coffee @@ -19,6 +19,12 @@ class Cable.ConnectionMonitor @reset() @pingedAt = now() + disconnected: -> + if @reconnectAttempts++ is 0 + setTimeout => + @consumer.connection.open() unless @consumer.connection.isOpen() + , 200 + received: -> @pingedAt = now() @@ -50,7 +56,7 @@ class Cable.ConnectionMonitor reconnectIfStale: -> if @connectionIsStale() - @reconnectAttempts += 1 + @reconnectAttempts++ @consumer.connection.reopen() connectionIsStale: -> |