Javascript 套接字(失败:WebSocket 握手期间出错:net::ERR_CONNECTION_RESET)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34185707/
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 16:01:08  来源:igfitidea点击:

Javascript Sockets (failed: Error during WebSocket handshake: net::ERR_CONNECTION_RESET)

javascriptwebsocketconnection-reset

提问by mando222

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. 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. My code is as follows:

我正在尝试创建一个简单的程序,以纯文本形式向套接字发送消息。发件人是使用 websocket 的 javascript 网页,服务器是 C 程序。我对 C 代码没有任何控制权,但代码所有者向我保证,我可以使用简单的 javascript 来发送消息。我的代码如下:

<!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.

从我有限的知识和我所做的阅读来看,这看起来是正确的,但欢迎任何帮助。

采纳答案by Paul B.

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. This strongly suggests that the websocket client is not source of the problem.

因为在针对 ws://echo.websocket.org 测试客户端时,一切似乎都运行良好。这强烈表明 websocket 客户端不是问题的根源。

回答by werewolf_65

This is probably super late but I just ran into a similar problem while using Django channels. I fixed it by adding a slash (/) at the end of the URL.

这可能太晚了,但我在使用 Django 频道时遇到了类似的问题。我通过/在 URL 末尾添加斜杠 ( ) 来修复它。

ws = new WebSocket("ws://192.168.6.222:11000/echo/");