0

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!

Community
  • 1
  • 1
  • I don't have a specific solution for you but these are some very similar questions I found that might help: https://stackoverflow.com/questions/12498429/multiple-websockets & https://stackoverflow.com/questions/3329641/how-do-multiple-clients-connect-simultaneously-to-one-port-say-80-on-a-server – Steve Sep 22 '17 at 16:15
  • Is `MultipleEndpoints` the context root of your application? And simple question: I do understand that if you get rid of `ConfigData`, `Monitor` is working. But if you get rid of `Monitor`, does `ConfigData` work? I tried a simple multiple endpoint on my GlassFish and did not meet any particular problem so far.. – Al-un Oct 11 '17 at 20:29

0 Answers0