aboutsummaryrefslogtreecommitdiffstats
path: root/actioncable/app/assets/javascripts/action_cable.coffee.erb
blob: 32f9f517f405ebe66a8be8c113c04a17e6add446 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#= require_self
#= require ./action_cable/consumer

@ActionCable =
  INTERNAL: <%= ActionCable::INTERNAL.to_json %>

  createConsumer: (url) ->
    url ?= @getConfig("url") ? @INTERNAL.default_mount_path
    new ActionCable.Consumer @createWebSocketURL(url)

  getConfig: (name) ->
    element = document.head.querySelector("meta[name='action-cable-#{name}']")
    element?.getAttribute("content")

  createWebSocketURL: (url) ->
    if url and not /^wss?:/i.test(url)
      a = document.createElement("a")
      a.href = url
      # Fix populating Location properties in IE. Otherwise, protocol will be blank.
      a.href = a.href
      a.protocol = a.protocol.replace("http", "ws")
      a.href
    else
      url

  startDebugging: ->
    @debugging = true

  stopDebugging: ->
    @debugging = null

  log: (messages...) ->
    if @debugging
      messages.push(Date.now())
      console.log("[ActionCable]", messages...)

# NOTE: We expose ActionCable as a browser global so we can reference it
# internally without concern for how the module is loaded.
window?.ActionCable = @ActionCable

module?.exports = @ActionCable