connection.coffee 2.7 KB
Newer Older
1
# Encapsulate the cable connection held by the consumer. This is an internal class not intended for direct user manipulation.
2

3
{message_types} = ActionCable.INTERNAL
4

5
class ActionCable.Connection
J
Javan Makhmali 已提交
6 7
  @reopenDelay: 500

J
Javan Makhmali 已提交
8
  constructor: (@consumer) ->
9 10

  send: (data) ->
11
    if @isOpen()
J
Javan Makhmali 已提交
12
      @webSocket.send(JSON.stringify(data))
13 14 15 16
      true
    else
      false

17
  open: =>
18
    if @isActive()
J
Javan Makhmali 已提交
19
      ActionCable.log("Attemped to open WebSocket, but existing socket is #{@getState()}")
20
      throw new Error("Existing connection must be closed before opening")
21
    else
J
Javan Makhmali 已提交
22
      ActionCable.log("Opening WebSocket, current state is #{@getState()}")
23
      @uninstallEventHandlers() if @webSocket?
24 25
      @webSocket = new WebSocket(@consumer.url)
      @installEventHandlers()
26
      true
27

28
  close: ->
J
Javan Makhmali 已提交
29
    @webSocket?.close()
30 31

  reopen: ->
J
Javan Makhmali 已提交
32
    ActionCable.log("Reopening WebSocket, current state is #{@getState()}")
33
    if @isActive()
J
Javan Makhmali 已提交
34 35
      try
        @close()
36 37
      catch error
        ActionCable.log("Failed to reopen WebSocket", error)
J
Javan Makhmali 已提交
38
      finally
39
        ActionCable.log("Reopening WebSocket in #{@constructor.reopenDelay}ms")
J
Javan Makhmali 已提交
40
        setTimeout(@open, @constructor.reopenDelay)
41 42
    else
      @open()
43 44

  isOpen: ->
45 46
    @isState("open")

47 48
  isActive: ->
    @isState("open", "connecting")
J
Javan Makhmali 已提交
49

50
  # Private
P
Pratik Naik 已提交
51

52 53
  isState: (states...) ->
    @getState() in states
54

55
  getState: ->
J
Javan Makhmali 已提交
56
    return state.toLowerCase() for state, value of WebSocket when value is @webSocket?.readyState
57
    null
58

J
Javan Makhmali 已提交
59 60
  installEventHandlers: ->
    for eventName of @events
61 62
      handler = @events[eventName].bind(this)
      @webSocket["on#{eventName}"] = handler
J
Javan Makhmali 已提交
63
    return
64

65 66 67 68 69
  uninstallEventHandlers: ->
    for eventName of @events
      @webSocket["on#{eventName}"] = ->
    return

J
Javan Makhmali 已提交
70 71
  events:
    message: (event) ->
72
      {identifier, message, type} = JSON.parse(event.data)
73
      switch type
74
        when message_types.welcome
D
Daniel Rhodes 已提交
75 76 77
          @consumer.connectionMonitor.connected()
        when message_types.ping
          @consumer.connectionMonitor.ping()
78
        when message_types.confirmation
79
          @consumer.subscriptions.notify(identifier, "connected")
80
        when message_types.rejection
81
          @consumer.subscriptions.reject(identifier)
82 83
        else
          @consumer.subscriptions.notify(identifier, "received", message)
84

J
Javan Makhmali 已提交
85
    open: ->
J
Javan Makhmali 已提交
86
      ActionCable.log("WebSocket onopen event")
87
      @disconnected = false
88
      @consumer.subscriptions.reload()
89

J
Javan Makhmali 已提交
90
    close: ->
J
Javan Makhmali 已提交
91
      ActionCable.log("WebSocket onclose event")
92
      @disconnect()
93

J
Javan Makhmali 已提交
94
    error: ->
J
Javan Makhmali 已提交
95
      ActionCable.log("WebSocket onerror event")
96 97 98 99 100
      @disconnect()

  disconnect: ->
    return if @disconnected
    @disconnected = true
101
    @consumer.connectionMonitor.disconnected()
102
    @consumer.subscriptions.notifyAll("disconnected")