2016-03-03 20:21:50 -05:00
|
|
|
#= require ./connection_monitor
|
|
|
|
|
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
|
|
|
|
2016-03-17 10:05:06 -04:00
|
|
|
{message_types, protocols} = ActionCable.INTERNAL
|
|
|
|
[supportedProtocols..., unsupportedProtocol] = protocols
|
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) ->
|
2016-03-03 21:44:11 -05:00
|
|
|
{@subscriptions} = @consumer
|
2016-03-03 20:21:50 -05:00
|
|
|
@monitor = new ActionCable.ConnectionMonitor this
|
2016-03-17 10:05:06 -04:00
|
|
|
@disconnected = true
|
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: =>
|
2016-03-03 17:33:56 -05:00
|
|
|
if @isActive()
|
2016-03-22 14:55:37 -04:00
|
|
|
ActionCable.log("Attempted to open WebSocket, but existing socket is #{@getState()}")
|
2015-10-14 18:06:21 -04:00
|
|
|
throw new Error("Existing connection must be closed before opening")
|
2015-08-28 17:45:07 -04:00
|
|
|
else
|
2016-03-17 10:05:06 -04:00
|
|
|
ActionCable.log("Opening WebSocket, current state is #{@getState()}, subprotocols: #{protocols}")
|
2016-02-19 10:26:41 -05:00
|
|
|
@uninstallEventHandlers() if @webSocket?
|
2016-03-17 10:05:06 -04:00
|
|
|
@webSocket = new WebSocket(@consumer.url, protocols)
|
2015-08-28 17:45:07 -04:00
|
|
|
@installEventHandlers()
|
2016-03-03 20:21:50 -05:00
|
|
|
@monitor.start()
|
2015-10-14 18:06:21 -04:00
|
|
|
true
|
2015-06-25 13:52:47 -04:00
|
|
|
|
2016-03-17 10:05:06 -04:00
|
|
|
close: ({allowReconnect} = {allowReconnect: true}) ->
|
|
|
|
@monitor.stop() unless allowReconnect
|
|
|
|
@webSocket?.close() if @isActive()
|
2015-06-26 10:24:29 -04:00
|
|
|
|
|
|
|
reopen: ->
|
2016-02-19 10:48:50 -05:00
|
|
|
ActionCable.log("Reopening WebSocket, current state is #{@getState()}")
|
2016-03-03 17:33:56 -05:00
|
|
|
if @isActive()
|
2015-10-14 18:09:10 -04:00
|
|
|
try
|
|
|
|
@close()
|
2016-02-19 10:57:43 -05:00
|
|
|
catch error
|
|
|
|
ActionCable.log("Failed to reopen WebSocket", error)
|
2015-10-14 18:09:10 -04:00
|
|
|
finally
|
2016-02-19 10:57:43 -05:00
|
|
|
ActionCable.log("Reopening WebSocket in #{@constructor.reopenDelay}ms")
|
2015-10-14 18:09:10 -04:00
|
|
|
setTimeout(@open, @constructor.reopenDelay)
|
2016-02-19 10:57:43 -05:00
|
|
|
else
|
|
|
|
@open()
|
2015-06-26 10:24:29 -04:00
|
|
|
|
2016-03-17 10:05:06 -04:00
|
|
|
getProtocol: ->
|
|
|
|
@webSocket?.protocol
|
|
|
|
|
2015-06-26 10:24:29 -04:00
|
|
|
isOpen: ->
|
2015-07-06 21:42:49 -04:00
|
|
|
@isState("open")
|
|
|
|
|
2016-03-03 17:33:56 -05:00
|
|
|
isActive: ->
|
|
|
|
@isState("open", "connecting")
|
2015-07-07 08:57:57 -04:00
|
|
|
|
2016-03-03 17:33:56 -05:00
|
|
|
# Private
|
2016-02-18 15:38:22 -05:00
|
|
|
|
2016-03-17 10:05:06 -04:00
|
|
|
isProtocolSupported: ->
|
|
|
|
@getProtocol() in supportedProtocols
|
|
|
|
|
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
|
|
|
|
2016-02-19 10:26:41 -05:00
|
|
|
uninstallEventHandlers: ->
|
|
|
|
for eventName of @events
|
|
|
|
@webSocket["on#{eventName}"] = ->
|
|
|
|
return
|
|
|
|
|
2015-07-07 08:57:57 -04:00
|
|
|
events:
|
|
|
|
message: (event) ->
|
2016-04-06 00:56:45 -04:00
|
|
|
return unless @isProtocolSupported()
|
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
|
2016-02-29 19:21:19 -05:00
|
|
|
when message_types.welcome
|
2016-03-03 20:21:50 -05:00
|
|
|
@monitor.recordConnect()
|
2016-03-17 10:05:06 -04:00
|
|
|
@subscriptions.reload()
|
2016-02-29 20:48:15 -05:00
|
|
|
when message_types.ping
|
2016-03-03 20:21:50 -05:00
|
|
|
@monitor.recordPing()
|
2015-11-06 14:09:38 -05:00
|
|
|
when message_types.confirmation
|
2016-03-03 21:44:11 -05:00
|
|
|
@subscriptions.notify(identifier, "connected")
|
2015-11-06 14:09:38 -05:00
|
|
|
when message_types.rejection
|
2016-03-03 21:44:11 -05:00
|
|
|
@subscriptions.reject(identifier)
|
2015-11-06 14:09:38 -05:00
|
|
|
else
|
2016-03-03 21:44:11 -05:00
|
|
|
@subscriptions.notify(identifier, "received", message)
|
2015-10-22 11:53:19 -04:00
|
|
|
|
2015-07-07 08:57:57 -04:00
|
|
|
open: ->
|
2016-03-17 10:05:06 -04:00
|
|
|
ActionCable.log("WebSocket onopen event, using '#{@getProtocol()}' subprotocol")
|
2015-08-28 17:52:46 -04:00
|
|
|
@disconnected = false
|
2016-03-17 10:05:06 -04:00
|
|
|
if not @isProtocolSupported()
|
|
|
|
ActionCable.log("Protocol is unsupported. Stopping monitor and disconnecting.")
|
|
|
|
@close(allowReconnect: false)
|
2015-06-25 10:21:53 -04:00
|
|
|
|
2016-03-17 10:05:06 -04:00
|
|
|
close: (event) ->
|
2016-02-19 10:48:50 -05:00
|
|
|
ActionCable.log("WebSocket onclose event")
|
2016-03-17 10:05:06 -04:00
|
|
|
return if @disconnected
|
|
|
|
@disconnected = true
|
|
|
|
@monitor.recordDisconnect()
|
|
|
|
@subscriptions.notifyAll("disconnected", {willAttemptReconnect: @monitor.isRunning()})
|
2015-06-25 10:21:53 -04:00
|
|
|
|
2015-07-07 08:57:57 -04:00
|
|
|
error: ->
|
2016-02-19 10:48:50 -05:00
|
|
|
ActionCable.log("WebSocket onerror event")
|