最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Can not connect web socket with javascript - Stack Overflow

programmeradmin2浏览0评论

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 :

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("Wele - status " + this.readyState);
                        log("Wele - 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);

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 :

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("Wele - status " + this.readyState);
                        log("Wele - 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);
Share Improve this question edited May 20, 2016 at 5:29 Satanand Tiwari asked May 18, 2016 at 7:11 Satanand TiwariSatanand Tiwari 4965 silver badges22 bronze badges 2
  • Could you ping ip from machine you get connection error? Also check websocket server is running – Ramil Amerzyanov Commented May 18, 2016 at 11:44
  • websocket is running and also get connected with google rest client app, you can see on screen shot it gives error only with webpage. – Satanand Tiwari Commented May 19, 2016 at 9:09
Add a ment  | 

1 Answer 1

Reset to default 3

I'd try your code and for me is working just fine, I'd test it with this webpage: https://www.websocket/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", 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"; 
    try {
      var socket = new WebSocket(host);
       alert('WebSocket - status ' + socket.readyState);
        socket.onopen = function (msg) {
            alert('open');
            alert("Wele - 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.

发布评论

评论列表(0)

  1. 暂无评论