I have this Java WebSocket application here. My code for constructing the URL for the server endpoint is:
function constructWebSocket(endpoint, onMessageCallback) {
const url = constructWebSocketUrl(endpoint);
const socket = new WebSocket(url);
socket.onopen = (event) => {
console.log("onopen. Event: ", event);
};
socket.onmessage = onMessageCallback;
socket.onclose = (event) => {
console.log("onclose. Event: ", event);
};
socket.onerror = (event) => {
console.log("onerror. Event: ", event);
};
return socket;
}
function constructWebSocketUrl(endpoint) {
const host = document.location.host;
const path = document.location.pathname;
const protocol = document.location.protocol;
if (protocol.startsWith("https")) {
return `wss://${host}${path}${endpoint}`;
} else if (protocol.startsWith("http")) {
return `ws://${host}${path}${endpoint}`;
} else {
throw `Unknown protocol: ${protocol}.`;
}
}
(The app is deployed on Heroku.)
While the above snippet returns a valid WebSocket when I run the web app on local Tomcat, on Heroku it returns 404 Not Found
and close code 1006. What am I missing here?
When I try to call to a \randomize
WebSocket, in logs there is the following error message apended:
2024-04-12T10:31:55.096386+00:00 heroku[router]: at=info method=GET path="/randomize" host=wiki-game-killer-ae0862952895.herokuapp.com request_id=bd425c43-f582-4152-ad24-3f7ee429ca88 fwd="93.106.179.198" dyno=web.1 connect=0ms service=9ms status=404 bytes=854 protocol=https
The problem was simply Java (JDK) version mismatch. After I forced all the involved (3) projects to rely on the same JDK 20, WebSocket started to work.
Also, I needed to follow these instructions.