[英]Javascript Sockets (failed: Error during WebSocket handshake: net::ERR_CONNECTION_RESET)
I am trying to create a simple program to send a message to a socket in plain text.我正在尝试创建一个简单的程序,以纯文本形式向套接字发送消息。 The sender is a webpage in javascript using websocket and the server is a C program.
发件人是使用 websocket 的 javascript 网页,服务器是 C 程序。 I don't have any control over the C code but I have been assured by the codes owners that I can use simple javascript to send the message.
我对 C 代码没有任何控制权,但代码所有者向我保证,我可以使用简单的 javascript 来发送消息。 My code is as follows:
我的代码如下:
<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript">
//initialize the websocket ws
var ws;
//Open the socket connection
function openSock() {
//test for Websocket compatibility in the browser
if ("WebSocket" in window) {
// log socket attempt
console.log("Attempting to open socket!");
//open web socket ws
ws = new WebSocket("ws://192.168.6.222:11000/echo");
} else { // else the browser doesn't support WebSocket
//Websocket is not supported
alert("APS NOT supported by your Browser!");
}
}
//send the command to socket ws
function sendCommand() {
console.log("Attempting to Send Message");
ws.send("your message here");
}
//close the socket ws
function closeSock() {
console.log("Closing Socket");
// close socket ws
ws.close();
}
</script>
</head>
<body>
<div id="sse">
<a href="javascript:openSock()"><input type=submit value="open"></a>
<a href="javascript:sendCommand()"><input type=submit value="send"></a>
<a href="javascript:closeSock()"><input type=submit value="close"></a>
</div>
</body>
</html>
When I click the connect button I get this error:当我单击连接按钮时,出现此错误:
WebSocket connection to 'ws://192.168.6.222:11000/echo' failed: Error during WebSocket handshake: net::ERR_CONNECTION_RESET
WebSocket 连接到“ws://192.168.6.222:11000/echo”失败:WebSocket 握手期间出错:net::ERR_CONNECTION_RESET
I have checked the firewall and the port seems to be open.我检查了防火墙,端口似乎是打开的。 After spending a fair amount of time troubleshooting the server (making sure it is running and on the network, etc.) I am wondering if I did something wrong with this client side code.
在花费了大量时间对服务器进行故障排除(确保它正在运行并在网络上等)之后,我想知道我是否对这个客户端代码做错了什么。
From my limited knowledge and the reading I have done this looks correct but any help is welcome.从我有限的知识和我所做的阅读来看,这看起来是正确的,但欢迎任何帮助。
This is probably super late but I just ran into a similar problem while using Django channels.这可能太晚了,但我在使用 Django 频道时遇到了类似的问题。 I fixed it by adding a slash (
/
) at the end of the URL.我通过在 URL 末尾添加斜杠 (
/
) 来修复它。
ws = new WebSocket("ws://192.168.6.222:11000/echo/");
How does the server side of the connection look like?连接的服务器端是什么样子的? Maybe you are using a faulty WebSocket Lib which does not provide a valid handshake.
也许您使用的是错误的 WebSocket Lib,它不提供有效的握手。
Because when testing the client against ws://echo.websocket.org everything seems to work perfectly fine.因为在针对 ws://echo.websocket.org 测试客户端时,一切似乎都运行良好。 This strongly suggests that the websocket client is not source of the problem.
这强烈表明 websocket 客户端不是问题的根源。
确保您的队列运行正常,就我而言,这就是问题所在
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.