diff options
Diffstat (limited to 'actioncable/app/javascript/action_cable/consumer.js')
-rw-r--r-- | actioncable/app/javascript/action_cable/consumer.js | 21 |
1 files changed, 20 insertions, 1 deletions
diff --git a/actioncable/app/javascript/action_cable/consumer.js b/actioncable/app/javascript/action_cable/consumer.js index e8440f39f5..51f3b60980 100644 --- a/actioncable/app/javascript/action_cable/consumer.js +++ b/actioncable/app/javascript/action_cable/consumer.js @@ -29,11 +29,15 @@ import Subscriptions from "./subscriptions" export default class Consumer { constructor(url) { - this.url = url + this._url = url this.subscriptions = new Subscriptions(this) this.connection = new Connection(this) } + get url() { + return createWebSocketURL(this._url) + } + send(data) { return this.connection.send(data) } @@ -52,3 +56,18 @@ export default class Consumer { } } } + +export function createWebSocketURL(url) { + const webSocketURL = typeof url === "function" ? url() : url + + if (webSocketURL && !/^wss?:/i.test(webSocketURL)) { + const a = document.createElement("a") + a.href = webSocketURL + // Fix populating Location properties in IE. Otherwise, protocol will be blank. + a.href = a.href + a.protocol = a.protocol.replace("http", "ws") + return a.href + } else { + return webSocketURL + } +} |