But when moving to a VPS with Nginx proxy this fails. As this seems to “converts” from https:// to wss:// or ws:// I have no clue what I have done wrong. https://ws.go4webdev.org (live)
The console gives this error:
WebSocket connection to ‘wss://localhost:8080/echo’ failed:
It is defined at load and assigned when clicking on button “open”
document.getElementById("open").onclick = function(evt) {
// at load this should be undefined at this level AFAIK.
if (typeof ws !== 'undefined' && ws !== null) {
return
}
ws = new WebSocket("ws://" + document.location.host + "/echo");
No matter if I use ws or wss here.
My assumption is that this is not fired for some reason:
ws = new WebSocket("ws://" + document.location.host + "/echo");
But it is fired at localhost and work properly. So the Go code at the server must be OK as I see it. The console reports no error.
And I do know nothing about websockets :-). From my limited understanding, you start a http server and “upgrade” to a ws server. Hence you cannot call the original http server to communicate with the websocket server. AFAIK.
My guts tell me that this is not about Go. It is about Websocket protocol and Javascript as client. Or any type of CORS issue involved.
I think I found a solution. It seems that Websockets is an old protocol that is only supported by HTTP 1.1 (cannot confirm this). But when added this to the Nginx sites-available, it seems to work.