I am trying to deploy WebSocket on my site. After the main lessons, I added this class:
@ServerEndpoint(value="/websocketendpoint") public class WebSocket { private static Set<Session> peers = Collections.synchronizedSet(new HashSet<Session>()); @OnMessage public String onMessage(String message){ return null; } @OnOpen public void onOpen(Session peer){ peers.add(peer); } @OnClose public void onClose(Session peer){ peers.remove(peer); } }
And this is JS:
var wsUri = "ws://" + document.location.host + document.location.pathname + "websocketendpoint"; var ws = new WebSocket(wsUri); ws.onopen = function(){ ws.send("Message to send"); alert("Message is sent..."); }; ws.onmessage = function (evt){ var received_msg = evt.data; alert("Message is received..."); }; ws.onclose = function(){ alert("Connection is closed..."); };
I added js to one of my pages and only ws.onclose , in the console I get this error:
Firefox : Firefox cannot establish a server connection on WS: // local: 8080 / MySITE / websocketendpoint
Chrome : WebSocket handshake error: Unexpected response code: 404
I tried using 'ws: //echo.websocket.org' as wsUri and it works, so I think the problem is on the server side.
I use the following libraries: javaee-api-7.0, javax.websocket-api-1.0 My browsers are compatible with websockets (I checked)
These topics did not help me, so I ask you to explain how to fix this problem.
source share