connection.coffee 2.8 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 12 13
    unless @isOpen()
      @open()

14
    if @isOpen()
J
Javan Makhmali 已提交
15
      @webSocket.send(JSON.stringify(data))
16 17 18 19
      true
    else
      false

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

31
  close: ->
J
Javan Makhmali 已提交
32
    @webSocket?.close()
33 34

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

  isOpen: ->
48 49
    @isState("open")

J
Javan Makhmali 已提交
50 51
  # Private

J
Javan Makhmali 已提交
52
  isAlive: ->
53
    @webSocket? and not @isState("closing", "closed")
P
Pratik Naik 已提交
54

55 56
  isState: (states...) ->
    @getState() in states
57

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

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

68 69 70 71 72
  uninstallEventHandlers: ->
    for eventName of @events
      @webSocket["on#{eventName}"] = ->
    return

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

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

J
Javan Makhmali 已提交
93
    close: ->
J
Javan Makhmali 已提交
94
      ActionCable.log("WebSocket onclose event")
95
      @disconnect()
96

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

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