Websocket not working: 404 unexpected response code

I am trying to create a javaee application with websockets but cannot get the program to work correctly. Using the Tomcat 7, Java EE 7 application with websites.

Here is my Java code:

import javax.websocket.CloseReason; import javax.websocket.EndpointConfig; import javax.websocket.OnClose; import javax.websocket.OnError; import javax.websocket.OnMessage; import javax.websocket.OnOpen; import javax.websocket.RemoteEndpoint; import javax.websocket.Session; import javax.websocket.server.ServerEndpoint; @ServerEndpoint(value="/hello") public class EchoEndpoint { RemoteEndpoint.Async endpoint; @OnOpen public void open(Session session, EndpointConfig conf) { //Connection opened. System.out.println("EchoEndpoint on open"); endpoint = session.getAsyncRemote(); } @OnMessage public void onMessage(Session session, String msg) { //Message received. System.out.println("EchoEndpoint on message"); } @OnError public void error(Session session, Throwable error) { //Connection error. System.out.println("EchoEndpoint on error"); } @OnClose public void close(Session session, CloseReason reason) { //Connection closed. System.out.println("EchoEndpoint on close"); } public void send(String string) { System.out.println("Send called to EchoEndpoint"); endpoint.sendText(string); } } 

web.xml:

 <?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> </web-app> 

index.html

 <!DOCTYPE html> <meta charset="utf-8" /> <title>WebSocket Test</title> <script language="javascript" type="text/javascript"> var wsUri = "ws://localhost:8080/myapp/hello"; var output; function init() { output = document.getElementById("output"); testWebSocket(); } function testWebSocket() { websocket = new WebSocket(wsUri); websocket.onopen = function(evt) { onOpen(evt) }; websocket.onclose = function(evt) { onClose(evt) }; websocket.onmessage = function(evt) { onMessage(evt) }; websocket.onerror = function(evt) { onError(evt) }; } function onOpen(evt) { writeToScreen("CONNECTED"); doSend("WebSocket rocks"); } function onClose(evt) { writeToScreen("DISCONNECTED"); } function onMessage(evt) { writeToScreen('<span style="color: blue;">RESPONSE: ' + evt.data+'</span>'); //websocket.close(); } function onError(evt) { writeToScreen('<span style="color: red;">ERROR:</span> ' + evt.data); } function doSend(message) { writeToScreen("SENT: " + message); websocket.send(message); } function writeToScreen(message) { var pre = document.createElement("p"); pre.style.wordWrap = "break-word"; pre.innerHTML = message; output.appendChild(pre); } window.addEventListener("load", init, false); </script> <h2>WebSocket Test</h2> <div id="output"></div> </html> 

On the client side, when trying to execute:

websocket = new WebSocket (wsUri);

I get:

Websocket connection with ws: // localhost: 8080 / myapp / hello failed: Unexpected response code: 404

Am I missing something in web.xml or somewhere else?

+3
source share
1 answer

WebSocket is implemented in Tomcat 7 by extending WebSocketServlet. But the WebSocket 1.0 API is introduced in tomcat 8. Your code is not compatible with Tomcat 7. Please use tomcat 8 and try the same.

Updating ...

Tomcat7 now supports websocket, as @samael said

+7
source

Source: https://habr.com/ru/post/1236941/


All Articles