Skip to content

Commit

Permalink
sphinx-agent: Pick websocket scheme based on window location
Browse files Browse the repository at this point in the history
  • Loading branch information
alcarney committed Oct 4, 2024
1 parent a79f9ae commit 85a125f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
1 change: 1 addition & 0 deletions lib/esbonio/changes/896.fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The websocket connection uri used by the preview should now correctly pick between `ws://` and `wss://` based on the window's origin
11 changes: 6 additions & 5 deletions lib/esbonio/esbonio/sphinx_agent/static/webview.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,13 +216,14 @@ function renderLineMarkers() {
document.body.append(markerStyle)
}

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

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

const socket = new WebSocket(wsServer);
Expand All @@ -242,7 +243,7 @@ const handlers = {
console.debug("Reloading page...")
window.location.reload()
},
"view/scroll": (params) => {scrollViewTo(params.uri, params.line)}
"view/scroll": (params) => { scrollViewTo(params.uri, params.line) }
}

function handle(message) {
Expand Down Expand Up @@ -291,7 +292,7 @@ function main() {
renderLineMarkers()
}

rewriteInternalLinks(ws)
rewriteInternalLinks(wsPort)

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

0 comments on commit 85a125f

Please sign in to comment.