diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/assets/javascripts/cable/connection.js.coffee | 39 | ||||
-rw-r--r-- | lib/assets/javascripts/cable/connection_monitor.js.coffee | 2 |
2 files changed, 21 insertions, 20 deletions
diff --git a/lib/assets/javascripts/cable/connection.js.coffee b/lib/assets/javascripts/cable/connection.js.coffee index 6ee28fcb75..cd9539a6aa 100644 --- a/lib/assets/javascripts/cable/connection.js.coffee +++ b/lib/assets/javascripts/cable/connection.js.coffee @@ -3,39 +3,41 @@ class Cable.Connection constructor: (@consumer) -> new Cable.ConnectionMonitor @consumer - @connect() + @open() send: (data) -> - if @isConnected() + if @isOpen() @websocket.send(JSON.stringify(data)) true else false - connect: -> - @removeWebsocket() - @createWebSocket() - - createWebSocket: -> + open: -> @websocket = new WebSocket(@consumer.url) @websocket.onmessage = @onMessage - @websocket.onopen = @onConnect + @websocket.onopen = @onOpen @websocket.onclose = @onClose @websocket.onerror = @onError @websocket - removeWebsocket: -> - if @websocket? - @websocket.onclose = -> # no-op - @websocket.onerror = -> # no-op - @websocket.close() - @websocket = null + close: -> + @websocket.close() unless @isClosed() + + reopen: -> + @close() + @open() + + isOpen: -> + @websocket.readyState is WebSocket.OPEN + + isClosed: -> + @websocket.readyState in [ WebSocket.CLOSED, WebSocket.CLOSING ] onMessage: (message) => data = JSON.parse message.data @consumer.subscribers.notify(data.identifier, "received", data.message) - onConnect: => + onOpen: => @consumer.subscribers.reload() onClose: => @@ -43,10 +45,9 @@ class Cable.Connection onError: => @disconnect() - - isConnected: -> - @websocket?.readyState is 1 + @websocket.onclose = -> # no-op + @websocket.onerror = -> # no-op + try @close() disconnect: -> @consumer.subscribers.notifyAll("disconnected") - @removeWebsocket() diff --git a/lib/assets/javascripts/cable/connection_monitor.js.coffee b/lib/assets/javascripts/cable/connection_monitor.js.coffee index cf36b2a457..bb4ee8f7f6 100644 --- a/lib/assets/javascripts/cable/connection_monitor.js.coffee +++ b/lib/assets/javascripts/cable/connection_monitor.js.coffee @@ -35,7 +35,7 @@ class Cable.ConnectionMonitor reconnect: -> console.log "Ping took too long to arrive. Reconnecting.." @connectionAttempts += 1 - @consumer.connection.connect() + @consumer.connection.reopen() now = -> new Date().getTime() |