简体   繁体   中英

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

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

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. I fixed it by adding a slash ( / ) at the end of the 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.

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.

确保您的队列运行正常,就我而言,这就是问题所在

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM