Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can not connect web socket with javascript

I want to connect to my web socket that put on amazone instance with some ip. I can connect my web socket with some ip and port with google rest client app and its working very well. Screen Shot : enter image description here

But if i want to connect this with java script it can not connect. This is working fine before 2-3 month. i have not change and thing but its not working now. If i want to connect with firefox it produce an error. Here is my code :-

function init() {
           var host = "ws://XX.XX.XXX.XXX:XXXX"; // SET THIS TO YOUR SERVER


                try {
                  var socket = new WebSocket(host);
                   // alert('WebSocket - status ' + socket.readyState);
                    log('WebSocket - status ' + socket.readyState);

                    socket.onopen = function (msg) {
                        alert('open');
                        alert("Welcome - status " + this.readyState);
                        log("Welcome - status " + this.readyState);

                        if (this.readyState != 1)
                        {
                            reconnect();
                        }
                    };
                    socket.onmessage = function (msg) {
                      //  alert("Received: " + msg.data);
                        log("Received: " + msg.data);

                    };
                    socket.onclose = function (msg) {
                     //   alert("Disconnected - status " + this.readyState);
                        log("Disconnected - status " + this.readyState);
                    };
                } catch (ex) {
                    alert(ex);
                    log(ex);
                }
                $("msg").focus();
            }

This is alerting status 0 and error show in console :-

Firefox can't establish a connection to the server at ws://XX.XX.XXX.XXX:XXXX.


var socket = new WebSocket(host);
like image 809
Satanand Tiwari Avatar asked May 18 '16 07:05

Satanand Tiwari


People also ask

Why WebSocket is not working?

Confirm that you're using a supported browser. If not, try using one of those and then check that WebSocket is working (see above). Proxy servers and firewalls. Check if you have a proxy server or firewall that blocks WebSocket access (you might need your system administrator's help).

How do I run a WebSocket server in JavaScript?

To open a websocket connection, we need to create new WebSocket using the special protocol ws in the url: let socket = new WebSocket("ws://javascript.info"); There's also encrypted wss:// protocol. It's like HTTPS for websockets.

How do I connect to WebSockets?

In order to communicate using the WebSocket protocol, you need to create a WebSocket object; this will automatically attempt to open the connection to the server. The URL to which to connect; this should be the URL to which the WebSocket server will respond.


1 Answers

I'd try your code and for me is working just fine, I'd test it with this webpage: https://www.websocket.org/echo.html , maybe could be helpful for testing purposes. But also i found this question: websocket-rails, websocket handshake error , maybe also help. However i'd just change the host in your code to this:"ws://echo.websocket.org", and everything works without problems. Hope you find a solution and that this info was of any help. Here's your code that i used for the test:

function init() {
var host = "ws://echo.websocket.org"; 
    try {
      var socket = new WebSocket(host);
       alert('WebSocket - status ' + socket.readyState);
        socket.onopen = function (msg) {
            alert('open');
            alert("Welcome - status " + this.readyState);

            if (this.readyState != 1)
            {
                reconnect();
            }
        };
        socket.onmessage = function (msg) {
          alert("Received: " + msg.data);
        };
        socket.onclose = function (msg) {
           alert("Disconnected - status " + this.readyState);
        };
    } catch (ex) {
        alert(ex);

    }
    $("msg").focus();
}

*Sorry for my bad english.

like image 75
Maxter Avatar answered Sep 30 '22 04:09

Maxter