2015-07-08 08:42:04 -04:00
|
|
|
# Encapsulate the cable connection held by the consumer. This is an internal class not intended for direct user manipulation.
|
2015-11-06 14:09:38 -05:00
|
|
|
|
2015-12-16 09:29:21 -05:00
|
|
|
{message_types} = ActionCable.INTERNAL
|
2015-11-06 14:09:38 -05:00
|
|
|
|
2015-12-16 09:29:21 -05:00
|
|
|
class ActionCable.Connection
|
2015-10-14 18:09:10 -04:00
|
|
|
@reopenDelay: 500
|
|
|
|
|
2015-06-25 16:24:58 -04:00
|
|
|
constructor: (@consumer) ->
|
2015-06-26 10:24:29 -04:00
|
|
|
@open()
|
2015-06-25 10:21:53 -04:00
|
|
|
|
|
|
|
send: (data) ->
|
2015-06-26 10:24:29 -04:00
|
|
|
if @isOpen()
|
2015-07-06 21:43:50 -04:00
|
|
|
@webSocket.send(JSON.stringify(data))
|
2015-06-25 10:21:53 -04:00
|
|
|
true
|
|
|
|
else
|
|
|
|
false
|
|
|
|
|
2015-10-14 18:06:21 -04:00
|
|
|
open: =>
|
|
|
|
if @webSocket and not @isState("closed")
|
|
|
|
throw new Error("Existing connection must be closed before opening")
|
2015-08-28 17:45:07 -04:00
|
|
|
else
|
|
|
|
@webSocket = new WebSocket(@consumer.url)
|
|
|
|
@installEventHandlers()
|
2015-10-14 18:06:21 -04:00
|
|
|
true
|
2015-06-25 13:52:47 -04:00
|
|
|
|
2015-06-26 10:24:29 -04:00
|
|
|
close: ->
|
2015-07-06 21:43:50 -04:00
|
|
|
@webSocket?.close()
|
2015-06-26 10:24:29 -04:00
|
|
|
|
|
|
|
reopen: ->
|
2015-10-14 18:09:10 -04:00
|
|
|
if @isState("closed")
|
|
|
|
@open()
|
|
|
|
else
|
|
|
|
try
|
|
|
|
@close()
|
|
|
|
finally
|
|
|
|
setTimeout(@open, @constructor.reopenDelay)
|
2015-06-26 10:24:29 -04:00
|
|
|
|
|
|
|
isOpen: ->
|
2015-07-06 21:42:49 -04:00
|
|
|
@isState("open")
|
|
|
|
|
2015-07-07 08:57:57 -04:00
|
|
|
# Private
|
|
|
|
|
2015-07-06 21:42:49 -04:00
|
|
|
isState: (states...) ->
|
|
|
|
@getState() in states
|
2015-06-26 10:24:29 -04:00
|
|
|
|
2015-07-06 21:42:49 -04:00
|
|
|
getState: ->
|
2015-07-06 21:43:50 -04:00
|
|
|
return state.toLowerCase() for state, value of WebSocket when value is @webSocket?.readyState
|
2015-07-06 21:42:49 -04:00
|
|
|
null
|
2015-06-25 10:21:53 -04:00
|
|
|
|
2015-07-07 08:57:57 -04:00
|
|
|
installEventHandlers: ->
|
|
|
|
for eventName of @events
|
2015-08-28 17:45:07 -04:00
|
|
|
handler = @events[eventName].bind(this)
|
|
|
|
@webSocket["on#{eventName}"] = handler
|
2015-10-14 18:09:32 -04:00
|
|
|
return
|
2015-06-25 10:21:53 -04:00
|
|
|
|
2015-07-07 08:57:57 -04:00
|
|
|
events:
|
|
|
|
message: (event) ->
|
2015-10-16 22:05:33 -04:00
|
|
|
{identifier, message, type} = JSON.parse(event.data)
|
|
|
|
|
2015-10-22 11:53:19 -04:00
|
|
|
switch type
|
2015-11-06 14:09:38 -05:00
|
|
|
when message_types.confirmation
|
2015-10-22 11:53:19 -04:00
|
|
|
@consumer.subscriptions.notify(identifier, "connected")
|
2015-11-06 14:09:38 -05:00
|
|
|
when message_types.rejection
|
2015-10-22 11:53:19 -04:00
|
|
|
@consumer.subscriptions.reject(identifier)
|
2015-11-06 14:09:38 -05:00
|
|
|
else
|
|
|
|
@consumer.subscriptions.notify(identifier, "received", message)
|
2015-10-22 11:53:19 -04:00
|
|
|
|
2015-07-07 08:57:57 -04:00
|
|
|
open: ->
|
2015-08-28 17:52:46 -04:00
|
|
|
@disconnected = false
|
2015-07-08 05:00:24 -04:00
|
|
|
@consumer.subscriptions.reload()
|
2015-06-25 10:21:53 -04:00
|
|
|
|
2015-07-07 08:57:57 -04:00
|
|
|
close: ->
|
2015-08-28 17:52:46 -04:00
|
|
|
@disconnect()
|
2015-06-25 10:21:53 -04:00
|
|
|
|
2015-07-07 08:57:57 -04:00
|
|
|
error: ->
|
2015-08-28 17:52:46 -04:00
|
|
|
@disconnect()
|
|
|
|
|
|
|
|
disconnect: ->
|
|
|
|
return if @disconnected
|
|
|
|
@disconnected = true
|
|
|
|
@consumer.subscriptions.notifyAll("disconnected")
|
2015-07-07 09:43:22 -04:00
|
|
|
|
|
|
|
toJSON: ->
|
|
|
|
state: @getState()
|