mirror of https://github.com/rails/rails
feat(js): Dynamic Actioncable WebSocket URL
Allow createWebSocketURL fn to accept a function to generate the websocket URL rather than a string.
This commit is contained in:
parent
a62683f3e4
commit
c7ca85ef31
|
@ -30,14 +30,20 @@ export function getConfig(name) {
|
|||
}
|
||||
|
||||
export function createWebSocketURL(url) {
|
||||
if (url && !/^wss?:/i.test(url)) {
|
||||
let webSocketURL
|
||||
if (typeof url === 'function') {
|
||||
webSocketURL = url()
|
||||
} else {
|
||||
webSocketURL = url
|
||||
}
|
||||
if (webSocketURL && !/^wss?:/i.test(webSocketURL)) {
|
||||
const a = document.createElement("a")
|
||||
a.href = url
|
||||
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 url
|
||||
return webSocketURL
|
||||
}
|
||||
}
|
||||
|
|
|
@ -41,5 +41,13 @@ module("ActionCable", () => {
|
|||
|
||||
assert.equal(consumer.url, testURL)
|
||||
})
|
||||
|
||||
test("uses function to generate URL", assert => {
|
||||
const generateURL = () => {
|
||||
return testURL
|
||||
}
|
||||
const consumer = ActionCable.createConsumer(generateURL)
|
||||
assert.equal(consumer.url, testURL)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
|
Loading…
Reference in New Issue