WebSocket 握手:意外的响应代码:404

WebSocket handshake: Unexpected response code: 404

我正在编写我的第一个 websocket 程序并在加载网页时遇到 "WebSocket handshake: Unexpected response code: 404" 错误。

我正在使用 JDK 1.7 和 jboss 8 (wildfly8.0)。

有人可以帮忙吗?

window.onload = init;
var socket = new WebSocket("ws://localhost:8080/WebsocketHome/actions"); 
socket.onmessage = onMessage; 

然后进入html

<head> 
 <title></title>
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 <script src="websocket.js" type="text/javascript"></script> 
 <link rel="stylesheet" type="text/css" href="style.css"> 
</head>

谢谢大家的建议,我找到了答案。

我复制的代码来自 http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/HomeWebsocket/WebsocketHome.html 站点。

问题出在js文件中提到的url,他们提议的项目名称是WebsocketHome。我已将项目名称更改为 Websocket,因此我的 url 应该是 ws://localhost:8080/Websocket/actions.

感谢您的支持。

实际上,这里的问题是 URL 中的区分大小写。您不需要更改项目名称。只需将 JavaScript 文件中的 Websocket URL 更改为

ws://localhost:8080/WebSocketHome/actions 

(大写S,如项目名称)可以解决问题。在你的例子中,改变它们两个消除了大小写不一致,所以它起作用了。

这是因为 /info=34424 的问题 - 出现 404 错误 - 我不得不放弃使用 other places. I have Spring 4.2 in my project and many SockJS Stomp implementations usually work well with Spring Boot implementations. This implementation from Baeldung 中建议的 xml 方法(对我来说没有从 Spring 4.2 至 5)。 Using the dependencies in his blog后,还是报了ClassNotFoundError。我添加了以下依赖项来修复它。

<dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>4.2.3.RELEASE</version>
    </dependency>

奇怪的是,Baeldung 的实现没有进行任何此类调用

flow/websocket/add/info?t=1540813753999

它的作用(发送和接收)如下。我只是粘贴它,以防精通这些库的人可以在这个论坛上进一步增加见解。

 >>> SEND
destination:/app/chat
content-length:38

{"from":"nicholas","text":"try again"}

<<< MESSAGE
destination:/topic/messages
content-type:application/json;charset=UTF-8
subscription:sub-0
message-id:m3p096zk-11
content-length:53

{"from":"nicholas","text":"try again","time":"13:46"}

我的猜测是您正在尝试使用普通浏览器联系 websocket。 这是不允许的,会出现 404 错误。您需要使用脚本或 curl 来解决 websockets。