rails--rails/actioncable/app/javascript/action_cable/connection.js

117 lines
3.3 KiB
JavaScript
Raw Normal View History

#= 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.
{message_types, protocols} = ActionCable.INTERNAL
[supportedProtocols..., unsupportedProtocol] = protocols
class ActionCable.Connection
2015-10-14 22:09:10 +00:00
@reopenDelay: 500
2015-06-25 20:24:58 +00:00
constructor: (@consumer) ->
{@subscriptions} = @consumer
@monitor = new ActionCable.ConnectionMonitor this
@disconnected = true
send: (data) ->
if @isOpen()
2015-07-07 01:43:50 +00:00
@webSocket.send(JSON.stringify(data))
true
else
false
open: =>
if @isActive()
ActionCable.log("Attempted to open WebSocket, but existing socket is #{@getState()}")
false
else
ActionCable.log("Opening WebSocket, current state is #{@getState()}, subprotocols: #{protocols}")
@uninstallEventHandlers() if @webSocket?
@webSocket = new ActionCable.WebSocket(@consumer.url, protocols)
@installEventHandlers()
@monitor.start()
true
close: ({allowReconnect} = {allowReconnect: true}) ->
@monitor.stop() unless allowReconnect
@webSocket?.close() if @isActive()
reopen: ->
2016-02-19 15:48:50 +00:00
ActionCable.log("Reopening WebSocket, current state is #{@getState()}")
if @isActive()
2015-10-14 22:09:10 +00:00
try
@close()
catch error
ActionCable.log("Failed to reopen WebSocket", error)
2015-10-14 22:09:10 +00:00
finally
ActionCable.log("Reopening WebSocket in #{@constructor.reopenDelay}ms")
2015-10-14 22:09:10 +00:00
setTimeout(@open, @constructor.reopenDelay)
else
@open()
getProtocol: ->
@webSocket?.protocol
isOpen: ->
@isState("open")
isActive: ->
@isState("open", "connecting")
2015-07-07 12:57:57 +00:00
# Private
isProtocolSupported: ->
@getProtocol() in supportedProtocols
isState: (states...) ->
@getState() in states
getState: ->
2015-07-07 01:43:50 +00:00
return state.toLowerCase() for state, value of WebSocket when value is @webSocket?.readyState
null
2015-07-07 12:57:57 +00:00
installEventHandlers: ->
for eventName of @events
handler = @events[eventName].bind(this)
@webSocket["on#{eventName}"] = handler
2015-10-14 22:09:32 +00:00
return
uninstallEventHandlers: ->
for eventName of @events
@webSocket["on#{eventName}"] = ->
return
2015-07-07 12:57:57 +00:00
events:
message: (event) ->
return unless @isProtocolSupported()
{identifier, message, type} = JSON.parse(event.data)
switch type
when message_types.welcome
@monitor.recordConnect()
@subscriptions.reload()
when message_types.ping
@monitor.recordPing()
when message_types.confirmation
@subscriptions.notify(identifier, "connected")
when message_types.rejection
@subscriptions.reject(identifier)
else
@subscriptions.notify(identifier, "received", message)
2015-07-07 12:57:57 +00:00
open: ->
ActionCable.log("WebSocket onopen event, using '#{@getProtocol()}' subprotocol")
@disconnected = false
if not @isProtocolSupported()
ActionCable.log("Protocol is unsupported. Stopping monitor and disconnecting.")
@close(allowReconnect: false)
close: (event) ->
2016-02-19 15:48:50 +00:00
ActionCable.log("WebSocket onclose event")
return if @disconnected
@disconnected = true
@monitor.recordDisconnect()
@subscriptions.notifyAll("disconnected", {willAttemptReconnect: @monitor.isRunning()})
2015-07-07 12:57:57 +00:00
error: ->
2016-02-19 15:48:50 +00:00
ActionCable.log("WebSocket onerror event")