diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/assets/javascripts/cable/connection.js.coffee | 20 |
1 files changed, 13 insertions, 7 deletions
diff --git a/lib/assets/javascripts/cable/connection.js.coffee b/lib/assets/javascripts/cable/connection.js.coffee index 4f7d2abada..530a589e87 100644 --- a/lib/assets/javascripts/cable/connection.js.coffee +++ b/lib/assets/javascripts/cable/connection.js.coffee @@ -10,6 +10,7 @@ class Cable.Connection false open: => + return if @isState("open", "connecting") @websocket = new WebSocket(@consumer.url) @websocket.onmessage = @onMessage @websocket.onopen = @onOpen @@ -17,21 +18,26 @@ class Cable.Connection @websocket.onerror = @onError close: -> - @websocket.close() unless @isClosed() + return if @isState("closed", "closing") + @websocket?.close() reopen: -> - if @isClosed() - @open() - else + if @isOpen() @websocket.onclose = @open @websocket.onerror = @open @websocket.close() + else + @open() isOpen: -> - @websocket.readyState is WebSocket.OPEN + @isState("open") + + isState: (states...) -> + @getState() in states - isClosed: -> - @websocket.readyState in [ WebSocket.CLOSED, WebSocket.CLOSING ] + getState: -> + return state.toLowerCase() for state, value of WebSocket when value is @websocket?.readyState + null onMessage: (message) => data = JSON.parse message.data |