aboutsummaryrefslogtreecommitdiffstats
path: root/lib/assets/javascripts/cable/connection.coffee
diff options
context:
space:
mode:
Diffstat (limited to 'lib/assets/javascripts/cable/connection.coffee')
-rw-r--r--lib/assets/javascripts/cable/connection.coffee29
1 files changed, 22 insertions, 7 deletions
diff --git a/lib/assets/javascripts/cable/connection.coffee b/lib/assets/javascripts/cable/connection.coffee
index 2259ddcedd..33159130c7 100644
--- a/lib/assets/javascripts/cable/connection.coffee
+++ b/lib/assets/javascripts/cable/connection.coffee
@@ -1,5 +1,7 @@
# Encapsulate the cable connection held by the consumer. This is an internal class not intended for direct user manipulation.
class Cable.Connection
+ @reopenDelay: 500
+
constructor: (@consumer) ->
@open()
@@ -10,19 +12,25 @@ class Cable.Connection
else
false
- open: ->
- if @isOpen()
- throw new Error("Must close existing connection before opening")
+ open: =>
+ if @webSocket and not @isState("closed")
+ throw new Error("Existing connection must be closed before opening")
else
@webSocket = new WebSocket(@consumer.url)
@installEventHandlers()
+ true
close: ->
@webSocket?.close()
reopen: ->
- @close()
- @open()
+ if @isState("closed")
+ @open()
+ else
+ try
+ @close()
+ finally
+ setTimeout(@open, @constructor.reopenDelay)
isOpen: ->
@isState("open")
@@ -40,11 +48,18 @@ class Cable.Connection
for eventName of @events
handler = @events[eventName].bind(this)
@webSocket["on#{eventName}"] = handler
+ return
events:
message: (event) ->
- {identifier, message} = JSON.parse(event.data)
- @consumer.subscriptions.notify(identifier, "received", message)
+ {identifier, message, type} = JSON.parse(event.data)
+
+ if type?
+ switch type
+ when Cable.INTERNAL_MESSAGES.SUBSCRIPTION_CONFIRMATION
+ @consumer.subscriptions.notify(identifier, "connected")
+ else
+ @consumer.subscriptions.notify(identifier, "received", message)
open: ->
@disconnected = false