diff options
Diffstat (limited to 'actioncable/app/assets/javascripts')
4 files changed, 12 insertions, 9 deletions
diff --git a/actioncable/app/assets/javascripts/action_cable/connection.coffee b/actioncable/app/assets/javascripts/action_cable/connection.coffee index e8c9ae6bd0..9be5cdf5fb 100644 --- a/actioncable/app/assets/javascripts/action_cable/connection.coffee +++ b/actioncable/app/assets/javascripts/action_cable/connection.coffee @@ -8,9 +8,6 @@ class ActionCable.Connection constructor: (@consumer) -> send: (data) -> - unless @isOpen() - @open() - if @isOpen() @webSocket.send(JSON.stringify(data)) true @@ -18,7 +15,7 @@ class ActionCable.Connection false open: => - if @isAlive() + if @isActive() ActionCable.log("Attemped to open WebSocket, but existing socket is #{@getState()}") throw new Error("Existing connection must be closed before opening") else @@ -33,7 +30,7 @@ class ActionCable.Connection reopen: -> ActionCable.log("Reopening WebSocket, current state is #{@getState()}") - if @isAlive() + if @isActive() try @close() catch error @@ -47,10 +44,10 @@ class ActionCable.Connection isOpen: -> @isState("open") - # Private + isActive: -> + @isState("open", "connecting") - isAlive: -> - @webSocket? and not @isState("closing", "closed") + # Private isState: (states...) -> @getState() in states @@ -101,4 +98,5 @@ class ActionCable.Connection disconnect: -> return if @disconnected @disconnected = true + @consumer.connectionMonitor.disconnected() @consumer.subscriptions.notifyAll("disconnected") diff --git a/actioncable/app/assets/javascripts/action_cable/connection_monitor.coffee b/actioncable/app/assets/javascripts/action_cable/connection_monitor.coffee index 740e86643e..904a426644 100644 --- a/actioncable/app/assets/javascripts/action_cable/connection_monitor.coffee +++ b/actioncable/app/assets/javascripts/action_cable/connection_monitor.coffee @@ -18,6 +18,7 @@ class ActionCable.ConnectionMonitor disconnected: -> @disconnectedAt = now() + ActionCable.log("ConnectionMonitor disconnected") ping: -> @pingedAt = now() diff --git a/actioncable/app/assets/javascripts/action_cable/consumer.coffee b/actioncable/app/assets/javascripts/action_cable/consumer.coffee index 717c0641a9..3d93d40b99 100644 --- a/actioncable/app/assets/javascripts/action_cable/consumer.coffee +++ b/actioncable/app/assets/javascripts/action_cable/consumer.coffee @@ -23,3 +23,7 @@ class ActionCable.Consumer send: (data) -> @connection.send(data) + + ensureActiveConnection: -> + unless @connection.isActive() + @connection.open() diff --git a/actioncable/app/assets/javascripts/action_cable/subscriptions.coffee b/actioncable/app/assets/javascripts/action_cable/subscriptions.coffee index 2443bca14a..9d93b462a7 100644 --- a/actioncable/app/assets/javascripts/action_cable/subscriptions.coffee +++ b/actioncable/app/assets/javascripts/action_cable/subscriptions.coffee @@ -19,6 +19,7 @@ class ActionCable.Subscriptions add: (subscription) -> @subscriptions.push(subscription) + @consumer.ensureActiveConnection() @notify(subscription, "initialized") @sendCommand(subscription, "subscribe") @@ -59,4 +60,3 @@ class ActionCable.Subscriptions sendCommand: (subscription, command) -> {identifier} = subscription @consumer.send({command, identifier}) - |