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

3

6

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.

Faradic answered 9/12, 2015 at 17:54 Comment(2)
did you try in different browsers?Bladdernose
Yes I have tried Chrome and Firefox.Faradic
K
2

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.

Kafir answered 9/12, 2015 at 17:58 Comment(6)
I dono if this could be the issue but from what I know the server side is using sys/socket.h. I am still learning C but that should work I would think. Let me know if I am totally off base here.Faradic
Are you sure you are getting the headers right? I tend to use nodejs + nodejs websocket (npmjs.com/package/websocket).Kafir
Websocket is supported in all major browsers. The sys/socket.h is from my understanding the linux basic socket header file. With testing on the server side I was able to use another program to connect to the server and send messages. I tried the ws://echo.websocket.org you stated and that seemed to work fine for the client side test. My knowledge of sockets is limited at best. I am trying to get a good idea of notdejs but don't fully understand how it differs from the native websocket where basic connections are concerned.Faradic
Nodejs just is a different aproach (creating the server in a different enviroment). The benefit to it is that the WebSocket handshake, etc. are taken care of by the library.Kafir
I have just did a little bit of reading on nodejs. It looks like it may not be quite what I am looking for. I have a very low power system that the server software is running on and limited ability to install anything new on it. I am a bit confused as to how websocket would be causing issues. Isn't it widely used for this sort of application? Or are you saying the C program is maybe not set up correctly?Faradic
Yes, that is what I would suspect. WebSockets indeed are the go to option for realtime web communication, nothing wrong with them.Kafir
S
3

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/");
Storekeeper answered 28/5, 2020 at 14:28 Comment(0)
K
2

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.

Kafir answered 9/12, 2015 at 17:58 Comment(6)
I dono if this could be the issue but from what I know the server side is using sys/socket.h. I am still learning C but that should work I would think. Let me know if I am totally off base here.Faradic
Are you sure you are getting the headers right? I tend to use nodejs + nodejs websocket (npmjs.com/package/websocket).Kafir
Websocket is supported in all major browsers. The sys/socket.h is from my understanding the linux basic socket header file. With testing on the server side I was able to use another program to connect to the server and send messages. I tried the ws://echo.websocket.org you stated and that seemed to work fine for the client side test. My knowledge of sockets is limited at best. I am trying to get a good idea of notdejs but don't fully understand how it differs from the native websocket where basic connections are concerned.Faradic
Nodejs just is a different aproach (creating the server in a different enviroment). The benefit to it is that the WebSocket handshake, etc. are taken care of by the library.Kafir
I have just did a little bit of reading on nodejs. It looks like it may not be quite what I am looking for. I have a very low power system that the server software is running on and limited ability to install anything new on it. I am a bit confused as to how websocket would be causing issues. Isn't it widely used for this sort of application? Or are you saying the C program is maybe not set up correctly?Faradic
Yes, that is what I would suspect. WebSockets indeed are the go to option for realtime web communication, nothing wrong with them.Kafir
W
0

Make sure that your queue is running properly, in my case that was the issue

Wriggly answered 18/5, 2021 at 10:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.