0

I am learning to create the web socket server in java with tomcat,

but when I run the tomcat9.0 with the sample code,

I always get the below error in the chrome developer tool response.

 **web socket failed: Error during WebSocket handshake: Unexpected response code: 404** 

I query the much the reference, I still not resolve the problem,

I am use the tomcat9.0 & jre 1.8.0, and I refer the blog.

I create the server code(project -> new -> Servlet file) is

 package idv.yu; import java.io.IOException; import javax.websocket.OnClose; import javax.websocket.OnError; import javax.websocket.OnMessage; import javax.websocket.OnOpen; import javax.websocket.Session; import javax.websocket.server.ServerEndpoint; @ServerEndpoint(value = "/endpoint") public class MyWebsocket { @OnOpen public void onOpen(Session session) { System.out.println("onOpen::" + session.getId()); } @OnClose public void onClose(Session session) { System.out.println("onClose::" + session.getId()); } @OnMessage public void onMessage(String message, Session session) { System.out.println("onMessage::From=" + session.getId() + " Message=" + message); try { session.getBasicRemote().sendText("Hello Client " + session.getId() + "!"); } catch (IOException e) { e.printStackTrace(); } } @OnError public void onError(Throwable t) { System.out.println("onError::" + t.getMessage()); } } 

My project tree structure is below:

enter image description here

In my chrome developer tool I set the code

 class WebSocketClient { constructor(protocol, hostname, port, endpoint) { this.webSocket = null; this.protocol = protocol; this.hostname = hostname; this.port = port; this.endpoint = endpoint; } getServerUrl() { return this.protocol + "://" + this.hostname + ":" + this.port + this.endpoint; } connect() { try { this.webSocket = new WebSocket(this.getServerUrl()); // // Implement WebSocket event handlers! // this.webSocket.onopen = function(event) { console.log('onopen::' + JSON.stringify(event, null, 4)); } this.webSocket.onmessage = function(event) { var msg = event.data; console.log('onmessage::' + JSON.stringify(msg, null, 4)); } this.webSocket.onclose = function(event) { console.log('onclose::' + JSON.stringify(event, null, 4)); } this.webSocket.onerror = function(event) { console.log('onerror::' + JSON.stringify(event, null, 4)); } } catch (exception) { console.error(exception); } } getStatus() { return this.webSocket.readyState; } send(message) { if (this.webSocket.readyState == WebSocket.OPEN) { this.webSocket.send(message); } else { console.error('webSocket is not open. readyState=' + this.webSocket.readyState); } } disconnect() { if (this.webSocket.readyState == WebSocket.OPEN) { this.webSocket.close(); } else { console.error('webSocket is not open. readyState=' + this.webSocket.readyState); } } } 

the last I set the command below:

 var client = new WebSocketClient('ws', '127.0.0.1', 8080, '/WebsocketDemo/endpoint'); client.connect(); 

But I get the error:

enter image description here

Have anyone known how to resolve the problem?

should the "web.xml" had to set any something?

my web.xml is default value, and I had check the project have websocket-api.jar file in the library. enter image description here

Thank you very much.

-- edit--

my WebContent/Web-INF/web.xml content below:

 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>WebsocketDemo</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> </web-app> 
6
  • Show the web.xml contents Commented Jun 24, 2018 at 4:20
  • @JimGarrison I had supplement the web.xml content in the edit, please. thank you. Commented Jun 24, 2018 at 11:55
  • As Sepehr GH mentioned, it may be linked to the deployment path: what is the contextroot of your application? Commented Jun 25, 2018 at 7:17
  • @Al1 I run the code method is right click on the 'MyWebsocket.java' file -> select 'run as' ->'run on server'. Commented Jun 25, 2018 at 9:59
  • @dickfala and what url appears on your browser? http://localhost:XXXX/WebsocketDemo/ ? Commented Jun 25, 2018 at 10:17

2 Answers 2

1

404 in websocket is not any different from 404 in HTTP, it means the address you are trying to reach can not be found on the server. In your case I think its about /WebsocketDemo part. The /endpoint is available in your endpoint configuration but its missing /WebsocketDemo. Perhaps you should deploy your WAR under /WebsocketDemo in webapps, or remove this part of the address.

Sign up to request clarification or add additional context in comments.

3 Comments

I use three PC try to build the same procedure, Or try to add the java-websocket-1.3.8.jar file in to the lib folder, but sometime the connect is correct, sometimes is fault with error 404. I think is it the websocket jar file dependency problem? But I don't known how to resolve...Orz..
@dickfala to be more clear, I meant you have to use the war file you deploy under /websocketDemo in tomcat, otherwise you have to remove /websocketDemo from your address var client = new WebSocketClient('ws', '127.0.0.1', 8080, '/endpoint');. Would you do second one and tell me the result?
, I remove /websocketDemo from my address, the result is same error (WebSocket connection to 'ws://127.0.0.1:8080/endpoint' failed: Error during WebSocket handshake: Unexpected response code: 404). Sorry , I don't known the where the war file to use~ (thank your of response my question.).
0

I was resolved the problem,

Going to the eclipse → Project → build automatically checked the item,

My project is running correct evertime.

Using the websocket is not to put other jar file, the tomcat8,9 have websocket jar in lib folder.

thank @sepehr GH,@Al1

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.