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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29455986/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 03:23:33  来源:igfitidea点击:

WebSocket handshake: Unexpected response code: 404

javascriptwebsocketwildfly-8

提问by Saurabh Jhunjhunwala

Am writing my first websocket program and am getting "WebSocket handshake: Unexpected response code: 404", error while loading the webpage.

我正在编写我的第一个 websocket 程序,并且收到“WebSocket 握手:意外响应代码:404”,加载网页时出错。

I am using JDK 1.7 and jboss 8 (wildfly8.0).

我使用的是 JDK 1.7 和 jboss 8 (wildfly8.0)。

Could anyone please assist?

有人可以帮忙吗?

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

and head in html

并以 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>

采纳答案by Saurabh Jhunjhunwala

Thank you guys for your suggestion, I found the answer.

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

The code I copied is from http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/HomeWebsocket/WebsocketHome.htmlsite.

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

The problem was the url as mentioned in the js file and the project name they are proposing is WebsocketHome. I had changed the project name to Websocket thus my url should be ws://localhost:8080/Websocket/actions.

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

Thanks for your support.

谢谢您的支持。

回答by scaleSpace

Actually, the problem here is case-sensitivity in the URLs. You did not need to change the project name. Just changing the Websocket URL in JavaScript file to

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

ws://localhost:8080/WebSocketHome/actions 

(with capital S, as in the project name) would have solved the problem. In your case, changing both of them removed the case inconsistency, so it worked.

(大写 S,如项目名称中所示)就可以解决这个问题。在您的情况下,更改它们都消除了大小写不一致的情况,因此它起作用了。

回答by nomail

My guess is that you are trying to contact the websocket with a normal browser. That is not allowed and gives a 404 error. You need to use a script or curl to address websockets.

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

回答by veritas

It's because of the issue of /info=34424 - with 404 error - that I had to abandon using the xml approach suggested at otherplaces. I have Spring 4.2 in my project and many SockJS Stomp implementations usually work well with Spring Boot implementations. This implementation from Baeldungworked(for me without changing from Spring 4.2 to 5). After Using the dependencies mentioned in his blog, it still gave me ClassNotFoundError. I added the below dependency to fix it.

这是因为 /info=34424 的问题 - 带有 404 错误 - 我不得不放弃使用其他地方建议的 xml 方法。我的项目中有 Spring 4.2,许多 SockJS Stomp 实现通常与 Spring Boot 实现配合良好。Baeldung 的这个实现有效(对我来说没有从 Spring 4.2 更改为 5)。使用了他博客中提到的依赖后,还是给了我ClassNotFoundError。我添加了以下依赖项来修复它。

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

Baeldung's implementation curiously does not make any such calls

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

flow/websocket/add/info?t=1540813753999

What it does (on send and receive) is below. I am only pasting it in case people well-versed with these libraries can further add insights on this forum.

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

 >>> 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"}