From 6d488a22d361d19e3de98c603d98e64ccea8a2f3 Mon Sep 17 00:00:00 2001 From: Ryan Castner Date: Sun, 31 Mar 2019 13:41:12 -0400 Subject: feat(js): Dynamic ActionCable URL (#35579) * Failing test case * feat: Dynamic Url Generation Change createWebSocketURL to be a closure that allows url to be evaluated at the time the webSocket is established * refactor: createWebSocketURL to Consumer, remove need for closure Move initial call to createWebSocketURL in createConsumer * docs: Add documentation for dynamic url and string args to createConsumer Co-Authored-By: rmacklin [Ryan Castner, rmacklin] --- actioncable/app/javascript/action_cable/index.js | 20 +++----------------- 1 file changed, 3 insertions(+), 17 deletions(-) (limited to 'actioncable/app/javascript/action_cable/index.js') diff --git a/actioncable/app/javascript/action_cable/index.js b/actioncable/app/javascript/action_cable/index.js index e679745fd7..d484d99179 100644 --- a/actioncable/app/javascript/action_cable/index.js +++ b/actioncable/app/javascript/action_cable/index.js @@ -1,6 +1,6 @@ import Connection from "./connection" import ConnectionMonitor from "./connection_monitor" -import Consumer from "./consumer" +import Consumer, { createWebSocketURL } from "./consumer" import INTERNAL from "./internal" import Subscription from "./subscription" import Subscriptions from "./subscriptions" @@ -16,10 +16,11 @@ export { Subscriptions, adapters, logger, + createWebSocketURL, } export function createConsumer(url = getConfig("url") || INTERNAL.default_mount_path) { - return new Consumer(createWebSocketURL(url)) + return new Consumer(url) } export function getConfig(name) { @@ -28,18 +29,3 @@ export function getConfig(name) { return element.getAttribute("content") } } - -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 - } -} -- cgit v1.2.3