I'm trying to make a two page website and need separate ServerEndpoint for each. My java code structure for two classes looks like,
@ServerEndpoint ("/action")
public class MonitorWebSocket {
@OnOpen
public void open (Session session) throws IOException {
System.out.println("Connection Opened");
session.getBasicRemote().sendText("MonitorWebSocket");
}
@OnClose
public void close () {
}
@OnError
public void err (Throwable error) {
System.out.println("Error: " + error);
}
}
and
@ServerEndpoint ("/configData")
public class ConfigDataWebSocket {
@OnOpen
public void open (Session session) throws IOException {
System.out.println("Config Connection Open");
session.getBasicRemote().sendText("ConfigDataWebSocket");
}
@OnError
public void err (Throwable er) {
System.out.println("Error: " + er);
}
@OnClose
public void close () {
}
}
And my javascripts looks as follows. For page "index.html"
var ws = new WebSocket ("ws://localhost:8080/MultipleEndpoints/action");
ws.open = function (event) {
document.getElementById("read").innerText = event.data;
};
ws.onerror = function (event) {
console.log(event.data);
};
and for page "config.html"
var ws1 = new WebSocket ("ws://localhost:8080/MultipleEndpoints/configData");
ws1.onopen = function (event) {
document.getElementById("a").innerText = event.data;
};
ws1.onerror = function (event) {
console.log(event.data);
};
Now when I run this code, I receive following error in Browser log,
WebSocket connection to 'ws://localhost:8080/MultipleEndpoints/action' failed: Unexpected response code: 404
WebSocket connection to 'ws://localhost:8080/MultipleEndpoints/configData' failed: Unexpected response code: 404
But when I get rid of ConfigDataWebSocket class completely, my MonitorWebSocket works just fine and my div gets updated by the value sent from java class.
I have been over through my code a few times and cannot spot the mistake I'm making.
Thanks!