Skip to content

Commit 85a125f

Browse files
committed
sphinx-agent: Pick websocket scheme based on window location
1 parent a79f9ae commit 85a125f

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

lib/esbonio/changes/896.fix.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The websocket connection uri used by the preview should now correctly pick between `ws://` and `wss://` based on the window's origin

lib/esbonio/esbonio/sphinx_agent/static/webview.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -216,13 +216,14 @@ function renderLineMarkers() {
216216
document.body.append(markerStyle)
217217
}
218218

219-
const host = window.location.hostname;
220219
const queryString = window.location.search;
221220
const queryParams = new URLSearchParams(queryString);
222-
const ws = parseInt(queryParams.get("ws"));
223221
const showMarkers = queryParams.has("show-markers")
224222

225-
const wsServer = `ws://${host}:${ws}`
223+
const wsScheme = (window.location.protocol === 'https:') ? 'wss' : 'ws'
224+
const wsHost = window.location.hostname;
225+
const wsPort = parseInt(queryParams.get("ws"));
226+
const wsServer = `${wsScheme}://${wsHost}:${wsPort}`
226227
console.debug(`Connecting to '${wsServer}'...`)
227228

228229
const socket = new WebSocket(wsServer);
@@ -242,7 +243,7 @@ const handlers = {
242243
console.debug("Reloading page...")
243244
window.location.reload()
244245
},
245-
"view/scroll": (params) => {scrollViewTo(params.uri, params.line)}
246+
"view/scroll": (params) => { scrollViewTo(params.uri, params.line) }
246247
}
247248

248249
function handle(message) {
@@ -291,7 +292,7 @@ function main() {
291292
renderLineMarkers()
292293
}
293294

294-
rewriteInternalLinks(ws)
295+
rewriteInternalLinks(wsPort)
295296

296297
// Are we in an <iframe>?
297298
if (window.parent !== window.top) {

0 commit comments

Comments
 (0)