无法使用javascript连接网络套接字 [英] Can not connect web socket with javascript

查看:49
本文介绍了无法使用javascript连接网络套接字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想连接到我的网络套接字,该套接字使用一些 ip 放在 amazone 实例上.我可以使用 google rest 客户端应用程序将我的网络套接字与一些 ip 和端口连接起来,并且它工作得很好.截屏 :

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 :

但是如果我想用java脚本连接它,它就无法连接.这在 2-3 个月前工作正常.我没有改变和事情,但它现在不工作.如果我想与 Firefox 连接,它会产生错误.这是我的代码:-

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();
            }

这是警告状态 0 并在控制台中显示错误:-

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);

推荐答案

我会尝试你的代码,对我来说工作得很好,我会用这个网页测试它:https://www.websocket.org/echo.html ,可能有助于测试目的.但我也发现了这个问题:websocket-rails, websocket握手错误,也许也有帮助.但是,我只是将代码中的主机更改为:ws://echo.websocket.org",一切正常.希望您找到解决方案,并且此信息对您有所帮助.这是我用于测试的代码:

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();
}

*抱歉我的英语不好.

这篇关于无法使用javascript连接网络套接字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆