2016-05-23 09:37:16 -04:00
|
|
|
#= export ActionCable
|
2015-11-06 14:09:38 -05:00
|
|
|
#= require_self
|
2016-01-31 13:01:03 -05:00
|
|
|
#= require ./action_cable/consumer
|
2015-11-06 14:09:38 -05:00
|
|
|
|
2015-12-16 09:29:21 -05:00
|
|
|
@ActionCable =
|
2015-11-06 14:09:38 -05:00
|
|
|
INTERNAL: <%= ActionCable::INTERNAL.to_json %>
|
2016-05-27 13:07:13 -04:00
|
|
|
WebSocket: window.WebSocket
|
|
|
|
logger: window.console
|
2015-11-06 14:09:38 -05:00
|
|
|
|
2016-03-03 21:10:10 -05:00
|
|
|
createConsumer: (url) ->
|
2016-03-03 21:27:52 -05:00
|
|
|
url ?= @getConfig("url") ? @INTERNAL.default_mount_path
|
2015-12-16 09:29:21 -05:00
|
|
|
new ActionCable.Consumer @createWebSocketURL(url)
|
2015-09-03 20:17:48 -04:00
|
|
|
|
|
|
|
getConfig: (name) ->
|
|
|
|
element = document.head.querySelector("meta[name='action-cable-#{name}']")
|
2016-03-03 21:10:10 -05:00
|
|
|
element?.getAttribute("content")
|
2015-12-15 10:35:02 -05:00
|
|
|
|
|
|
|
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
|
2016-02-19 10:48:50 -05:00
|
|
|
|
|
|
|
startDebugging: ->
|
|
|
|
@debugging = true
|
|
|
|
|
|
|
|
stopDebugging: ->
|
|
|
|
@debugging = null
|
|
|
|
|
|
|
|
log: (messages...) ->
|
|
|
|
if @debugging
|
|
|
|
messages.push(Date.now())
|
2016-05-27 13:07:13 -04:00
|
|
|
@logger.log("[ActionCable]", messages...)
|