buildWebSocketUrl.js
524 Bytes
export function buildWebSocketUrl(rawUrl, pageProtocol) {
if (!rawUrl || typeof rawUrl !== "string") return ""
let url = rawUrl.trim()
const secure =
pageProtocol != null
? pageProtocol === "https:"
: typeof window !== "undefined" && window.location.protocol === "https:"
if (/^wss?:\/\//i.test(url)) {
if (secure && /^ws:\/\//i.test(url)) {
return "wss://" + url.slice(5)
}
return url
}
const scheme = secure ? "wss://" : "ws://"
return scheme + url.replace(/^\/\//, "")
}