[英]java-server websocket failed: Error during WebSocket handshake: Unexpected response code: 404
I am learning to create the web socket server in java with tomcat, 我正在学习使用tomcat在Java中创建Web套接字服务器,
but when I run the tomcat9.0 with the sample code, 但是当我使用示例代码运行tomcat9.0时,
I always get the below error in the chrome developer tool response. 我总是在chrome开发人员工具响应中收到以下错误。
**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 . 我使用的是tomcat9.0&jre 1.8.0,请参阅Blog 。
I create the server code(project -> new -> Servlet file) is 我创建服务器代码(项目->新-> Servlet文件)是
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: 我的项目树结构如下:
In my chrome developer tool I set the code 在我的Chrome开发人员工具中,我设置了代码
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: 但是我得到了错误:
Have anyone known how to resolve the problem? 有谁知道如何解决这个问题?
should the "web.xml" had to set any something? “ web.xml”是否必须设置任何内容?
my web.xml is default value, and I had check the project have websocket-api.jar file in the library. 我的web.xml是默认值,并且我检查了项目在库中有websocket-api.jar文件。
Thank you very much. 非常感谢你。
-- edit-- -编辑-
my WebContent/Web-INF/web.xml content below: 我的WebContent / Web-INF / web.xml内容如下:
<?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>
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. 404
中的WebSocket是不是从任何不同404
在HTTP,这意味着你要访问的地址不能在服务器上找到。 In your case I think its about /WebsocketDemo
part. 在您的情况下,我认为它是关于/WebsocketDemo
部分。 The /endpoint
is available in your endpoint configuration but its missing /WebsocketDemo
. /endpoint
在端点配置中可用,但缺少/WebsocketDemo
。 Perhaps you should deploy your WAR under /WebsocketDemo
in webapps, or remove this part of the address. 也许您应该将WAR部署在webapps的/WebsocketDemo
下,或删除地址的这一部分。
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. 使用websocket并不是要放置其他jar文件,tomcat8,9在lib文件夹中有websocket jar。
thank @sepehr GH,@Al1 谢谢@sepehr GH,@ Al1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.