WebSocket 404错误 [英] WebSocket 404 error
问题描述
我正在尝试在我的网站上实现WebSocket。以下基本教程我添加了这个类:
I'm trying to implement WebSocket on my site. Following basic tutorials 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); }
}
这个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...");
};
我将js添加到我的一个页面,只有 ws.onclose调用
,在控制台中我收到此错误:
I added the js to one of my pages, and only ws.onclose
is called, in console I get this error:
Firefox
:Firefox无法在
建立与服务器的连接ws:// localhost:8080 / Mysite / websocketendpoint
Firefox
: Firefox can't establish a connection to the server at ws://localhost:8080/Mysite/websocketendpoint
Chrome
:WebSocket握手期间出错:意外的响应代码:
404
Chrome
: Error during WebSocket handshake: Unexpected response code:
404
我试过使用'ws://echo.websocket.org'为 wsUri
并且它有效,所以我猜问题出在服务器端。
I tried using 'ws://echo.websocket.org' as wsUri
and it works, so I guess the problem is on server side.
我正在使用以下库: javaee-api-7.0,javax.websocket-api-1.0
我的浏览器与websockets兼容(我检查过)
I'm using the following libraries: javaee-api-7.0, javax.websocket-api-1.0 My browsers are compatible with websockets (I checked)
类似的主题对我没有帮助,所以我要求你提供如何解决问题的消息
Similar topics didn't help me, so I'm asking you for sugestions on how to fix the problem
推荐答案
您可能想检查这是否是您的错误的根源: tomcat 7.0.50 java webscoket实现给出了404错误
You might want to check if this is the root of your error: tomcat 7.0.50 java webscoket implementation gives 404 error
Tomcat有一个JSR-356(即自7.0.47版以来的websocket API)实现。它还提供 javax.websocket-api-1.0.jar
库,因此您的应用程序不需要这样做。您可能在编译时需要它,但服务器将在运行时提供它(如果您不熟悉该工具,那么提供
范围在maven中意味着什么)
Tomcat has a JSR-356 (i.e. the websocket API) implementation since version 7.0.47. It provides also the javax.websocket-api-1.0.jar
library so this is not needed in you application. You might need it at compile time but the server will provide it at runtime (that's what provided
scope means in maven if you are not familiar with the tool)
确保 javax.websocket-api-1.0.jar
未在WAR中部署,在Eclipse服务器中右键单击view, Clean ...
后跟清理Tomcat工作目录...
然后发布
然后重试。
Make sure javax.websocket-api-1.0.jar
does not get deployed in your WAR, do a right click in your Eclipse server view, Clean...
followed by Clean Tomcat work directory...
then a Publish
and try again.
这篇关于WebSocket 404错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!