But when moving to a VPS with Nginx proxy this fails. As this “converts” from https:// to wss:// 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:
And the page shows up like this:
I suspect that it is this line that is the problem, but I cannot figure out the syntax.
//var addr = flag.String("addr", "127.0.0.1:8080", "http service address") <-- works on my local computer
var addr = flag.String("addr", "https://ws.go4webdev.org", "http service address") <-- fails on VPS
My gut feeling tells me that this is close to a CORS error, but I cannot confirm.
Are you running this behind a reverse proxy or something? A simple fix would be to get rid of that part of the template and instead push the logic to the browser. Something like this:
ws = new WebSocket("ws://" + document.location.host + "/echo");
Thanks, but when move this to the javascript there is no value assigned to the ws. I have updated the javascript and added some alerts and here it is live (still not working).
It seems to me that the page is loaded twice and maybe clear the value of ws?
window.addEventListener("load", function(evt) {
alert("value of ws at load " + ws)
var output = document.getElementById("output");
var input = document.getElementById("input");
var ws;
var print = function(message) {
var d = document.createElement("div");
d.textContent = message;
output.appendChild(d);
output.scroll(0, output.scrollHeight);
};
document.getElementById("open").onclick = function(evt) {
alert("value at ws at open: " + ws)
if (typeof ws !== 'undefined' && ws !== null) {
return
}
alert("assign value to ws")
ws = new WebSocket("ws://" + document.location.host + "/echo");
alert("ws://" + document.location.host + "/echo") // this is not executed for some reason...
alert(ws);
ws.onopen = function(evt) {
print("OPEN");
}
ws.onclose = function(evt) {
print("CLOSE");
ws = null;
}
ws.onmessage = function(evt) {
print("RESPONSE: " + evt.data);
}
ws.onerror = function(evt) {
print("ERROR: " + evt.data);
}
return false;
};
document.getElementById("send").onclick = function(evt) {
if (!ws) {
return false;
}
print("SEND: " + input.value);
ws.send(input.value);
return false;
};
document.getElementById("close").onclick = function(evt) {
if (!ws) {
return false;
}
ws.close();
return false;
};
});
Maybe you need wss. I had a WebSocket project once and when deployed behind a reverse proxy (nginx), it was accessed through wss. Here are a couple of code snippets:
// The websocket URL scheme.
var wsScheme="{{.WSScheme}}";
...
wsConn = new WebSocket(wsScheme+"//" + wsUrl +"/socket");
I’m wondering if, since you have those buttons inside a form, they are attempting to submit the form when you press them. Try changing those buttons to button type="button" so they don’t submit the form, causing a reload.
I think I found one 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.