2016-03-04 01:21:50 +00:00
|
|
|
#= require ./connection_monitor
|
|
|
|
|
2015-07-08 12:42:04 +00:00
|
|
|
# Encapsulate the cable connection held by the consumer. This is an internal class not intended for direct user manipulation.
|
2015-11-06 19:09:38 +00:00
|
|
|
|
2016-03-17 14:05:06 +00:00
|
|
|
{message_types, protocols} = ActionCable.INTERNAL
|
|
|
|
[supportedProtocols..., unsupportedProtocol] = protocols
|
2015-11-06 19:09:38 +00:00
|
|
|
|
2015-12-16 14:29:21 +00:00
|
|
|
class ActionCable.Connection
|
2015-10-14 22:09:10 +00:00
|
|
|
@reopenDelay: 500
|
|
|
|
|
2015-06-25 20:24:58 +00:00
|
|
|
constructor: (@consumer) ->
|
2016-03-04 02:44:11 +00:00
|
|
|
{@subscriptions} = @consumer
|
2016-03-04 01:21:50 +00:00
|
|
|
@monitor = new ActionCable.ConnectionMonitor this
|
2016-03-17 14:05:06 +00:00
|
|
|
@disconnected = true
|
2015-06-25 14:21:53 +00:00
|
|
|
|
|
|
|
send: (data) ->
|
2015-06-26 14:24:29 +00:00
|
|
|
if @isOpen()
|
2015-07-07 01:43:50 +00:00
|
|
|
@webSocket.send(JSON.stringify(data))
|
2015-06-25 14:21:53 +00:00
|
|
|
true
|
|
|
|
else
|
|
|
|
false
|
|
|
|
|
2015-10-14 22:06:21 +00:00
|
|
|
open: =>
|
2016-03-03 22:33:56 +00:00
|
|
|
if @isActive()
|
2016-03-22 18:55:37 +00:00
|
|
|
ActionCable.log("Attempted to open WebSocket, but existing socket is #{@getState()}")
|
2016-12-20 14:32:45 +00:00
|
|
|
false
|
2015-08-28 21:45:07 +00:00
|
|
|
else
|
2016-03-17 14:05:06 +00:00
|
|
|
ActionCable.log("Opening WebSocket, current state is #{@getState()}, subprotocols: #{protocols}")
|
2016-02-19 15:26:41 +00:00
|
|
|
@uninstallEventHandlers() if @webSocket?
|
2016-05-27 17:07:13 +00:00
|
|
|
@webSocket = new ActionCable.WebSocket(@consumer.url, protocols)
|
2015-08-28 21:45:07 +00:00
|
|
|
@installEventHandlers()
|
2016-03-04 01:21:50 +00:00
|
|
|
@monitor.start()
|
2015-10-14 22:06:21 +00:00
|
|
|
true
|
2015-06-25 17:52:47 +00:00
|
|
|
|
2016-03-17 14:05:06 +00:00
|
|
|
close: ({allowReconnect} = {allowReconnect: true}) ->
|
|
|
|
@monitor.stop() unless allowReconnect
|
|
|
|
@webSocket?.close() if @isActive()
|
2015-06-26 14:24:29 +00:00
|
|
|
|
|
|
|
reopen: ->
|
2016-02-19 15:48:50 +00:00
|
|
|
ActionCable.log("Reopening WebSocket, current state is #{@getState()}")
|
2016-03-03 22:33:56 +00:00
|
|
|
if @isActive()
|
2015-10-14 22:09:10 +00:00
|
|
|
try
|
|
|
|
@close()
|
2016-02-19 15:57:43 +00:00
|
|
|
catch error
|
|
|
|
ActionCable.log("Failed to reopen WebSocket", error)
|
2015-10-14 22:09:10 +00:00
|
|
|
finally
|
2016-02-19 15:57:43 +00:00
|
|
|
ActionCable.log("Reopening WebSocket in #{@constructor.reopenDelay}ms")
|
2015-10-14 22:09:10 +00:00
|
|
|
setTimeout(@open, @constructor.reopenDelay)
|
2016-02-19 15:57:43 +00:00
|
|
|
else
|
|
|
|
@open()
|
2015-06-26 14:24:29 +00:00
|
|
|
|
2016-03-17 14:05:06 +00:00
|
|
|
getProtocol: ->
|
|
|
|
@webSocket?.protocol
|
|
|
|
|
2015-06-26 14:24:29 +00:00
|
|
|
isOpen: ->
|
2015-07-07 01:42:49 +00:00
|
|
|
@isState("open")
|
|
|
|
|
2016-03-03 22:33:56 +00:00
|
|
|
isActive: ->
|
|
|
|
@isState("open", "connecting")
|
2015-07-07 12:57:57 +00:00
|
|
|
|
2016-03-03 22:33:56 +00:00
|
|
|
# Private
|
2016-02-18 20:38:22 +00:00
|
|
|
|
2016-03-17 14:05:06 +00:00
|
|
|
isProtocolSupported: ->
|
|
|
|
@getProtocol() in supportedProtocols
|
|
|
|
|
2015-07-07 01:42:49 +00:00
|
|
|
isState: (states...) ->
|
|
|
|
@getState() in states
|
2015-06-26 14:24:29 +00:00
|
|
|
|
2015-07-07 01:42:49 +00:00
|
|
|
getState: ->
|
2015-07-07 01:43:50 +00:00
|
|
|
return state.toLowerCase() for state, value of WebSocket when value is @webSocket?.readyState
|
2015-07-07 01:42:49 +00:00
|
|
|
null
|
2015-06-25 14:21:53 +00:00
|
|
|
|
2015-07-07 12:57:57 +00:00
|
|
|
installEventHandlers: ->
|
|
|
|
for eventName of @events
|
2015-08-28 21:45:07 +00:00
|
|
|
handler = @events[eventName].bind(this)
|
|
|
|
@webSocket["on#{eventName}"] = handler
|
2015-10-14 22:09:32 +00:00
|
|
|
return
|
2015-06-25 14:21:53 +00:00
|
|
|
|
2016-02-19 15:26:41 +00:00
|
|
|
uninstallEventHandlers: ->
|
|
|
|
for eventName of @events
|
|
|
|
@webSocket["on#{eventName}"] = ->
|
|
|
|
return
|
|
|
|
|
2015-07-07 12:57:57 +00:00
|
|
|
events:
|
|
|
|
message: (event) ->
|
2016-04-06 04:56:45 +00:00
|
|
|
return unless @isProtocolSupported()
|
2015-10-17 02:05:33 +00:00
|
|
|
{identifier, message, type} = JSON.parse(event.data)
|
2015-10-22 15:53:19 +00:00
|
|
|
switch type
|
2016-03-01 00:21:19 +00:00
|
|
|
when message_types.welcome
|
2016-03-04 01:21:50 +00:00
|
|
|
@monitor.recordConnect()
|
2016-03-17 14:05:06 +00:00
|
|
|
@subscriptions.reload()
|
2016-03-01 01:48:15 +00:00
|
|
|
when message_types.ping
|
2016-03-04 01:21:50 +00:00
|
|
|
@monitor.recordPing()
|
2015-11-06 19:09:38 +00:00
|
|
|
when message_types.confirmation
|
2016-03-04 02:44:11 +00:00
|
|
|
@subscriptions.notify(identifier, "connected")
|
2015-11-06 19:09:38 +00:00
|
|
|
when message_types.rejection
|
2016-03-04 02:44:11 +00:00
|
|
|
@subscriptions.reject(identifier)
|
2015-11-06 19:09:38 +00:00
|
|
|
else
|
2016-03-04 02:44:11 +00:00
|
|
|
@subscriptions.notify(identifier, "received", message)
|
2015-10-22 15:53:19 +00:00
|
|
|
|
2015-07-07 12:57:57 +00:00
|
|
|
open: ->
|
2016-03-17 14:05:06 +00:00
|
|
|
ActionCable.log("WebSocket onopen event, using '#{@getProtocol()}' subprotocol")
|
2015-08-28 21:52:46 +00:00
|
|
|
@disconnected = false
|
2016-03-17 14:05:06 +00:00
|
|
|
if not @isProtocolSupported()
|
|
|
|
ActionCable.log("Protocol is unsupported. Stopping monitor and disconnecting.")
|
|
|
|
@close(allowReconnect: false)
|
2015-06-25 14:21:53 +00:00
|
|
|
|
2016-03-17 14:05:06 +00:00
|
|
|
close: (event) ->
|
2016-02-19 15:48:50 +00:00
|
|
|
ActionCable.log("WebSocket onclose event")
|
2016-03-17 14:05:06 +00:00
|
|
|
return if @disconnected
|
|
|
|
@disconnected = true
|
|
|
|
@monitor.recordDisconnect()
|
|
|
|
@subscriptions.notifyAll("disconnected", {willAttemptReconnect: @monitor.isRunning()})
|
2015-06-25 14:21:53 +00:00
|
|
|
|
2015-07-07 12:57:57 +00:00
|
|
|
error: ->
|
2016-02-19 15:48:50 +00:00
|
|
|
ActionCable.log("WebSocket onerror event")
|