Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 233
mando222 Avatar asked Dec 09 '15 17:12

mando222


People also ask

How do I fix WebSocket connection failed?

Solution 1Check that all the Bot Insight services are running. Check that your firewall settings are configured to accept incoming websocket data. Try to use a different web browser. Restart the Bot Insight Visualization and Bot Insight Scheduler services.

What causes a WebSocket to fail?

The error event is fired when a connection with a WebSocket has been closed due to an error (some data couldn't be sent for example).

What is WebSocket handshake?

WebSockets - Overview In computer science, handshaking is a process that ensures the server is in sync with its clients. Handshaking is the basic concept of Web Socket protocol. The following diagram shows the server handshake with various clients −

What does WebSocket error mean?

A WebSocket error indicates a problem with the connection between your Ledger device and the Ledger Live application. Some users have reported facing this error message by using Ledger Live in places with restricted internet access.


2 Answers

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/");
like image 133
moonshadowolf13 Avatar answered Sep 30 '22 16:09

moonshadowolf13


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.

like image 32
Paul B. Avatar answered Sep 30 '22 16:09

Paul B.