Socket.IO和IE8 - jsonp-polling连接总是失败 [英] Socket.IO and IE8 - jsonp-polling connections always failing

查看:343
本文介绍了Socket.IO和IE8 - jsonp-polling连接总是失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

值得注意的是:以下是通过https跨域进行的。老实说,我认为这不是问题,因为在IE10,Chrome和FF中一切正常。我的猜测是它可能是IE8中的XDomainRequest对象差异?但不确定。

Worth noting: the following is being done cross domain via https. I honestly do not think this is the issue as everything works just fine in IE10, Chrome, and FF. My guess is that it may be an XDomainRequest object variance in IE8? Not sure though.

下面的 sendLoginRequest 方法是首先调用的方法。所有其他支持代码也在下面提供。

The sendLoginRequest method below is the method called first. All the other supporting code is provided below as well.

这一切都非常简单,但不确定为什么IE8会失败。

This is all very simple, but not sure why IE8 fails as it does.

function WrappedSocket(data, session_string) {
    var clientSocket = io.connect('https://xxxxxxxx/socketio', { query: "session=" +       encodeURIComponent(session_string), transports: ['jsonp-polling'] });
    clientSocket.socket.on("connect", function () { console.log("CONNECT_SUCCEED"); });
    clientSocket.socket.on("connect_failed", function () { console.log("CONNECT_FAILED");    });
    clientSocket.socket.on("reconnect_failed", function () {    console.log("RECONNECT_FAILED"); });
    clientSocket.socket.on("error", function (eobj) { console.log("Socket error " + eobj);    });
    console.log("Made a socket that is talking");
}

var my_socket;


function set_up_socket(data, sessionString) {
    setSession(data.responseText);
    my_socket = new WrappedSocket(data, sessionString);
    my_socket.socket.emit("message", "Howdy!");
}

function sendLoginRequest(loginCode, nextRequest) {
    var xhr = createCORSRequest('POST', 'https://xxxxx/login', false);
    var sessionString = 'xxxx';

    if ("withCredentials" in xhr) {
        xhr.addEventListener("load", function () {
            set_up_socket(this, sessionString);
        }, false);
    }
    else {
        xhr.onload = function () {
            set_up_socket(this, sessionString);
        };
    }

    xhr.send();
    }

function createCORSRequest(method, url, onload) {
    xhrObj = new XMLHttpRequest();
    if ("withCredentials" in xhrObj) {
        // Check if the XMLHttpRequest object has a "withCredentials" property.
        // "withCredentials" only exists on XMLHTTPRequest2 objects. 
        if (onload) {
            xhrObj.addEventListener("load", onload, false);
        }
        xhrObj.open(method, url, true);
        xhrObj.withCredentials = true;
    } else if (typeof XDomainRequest != "undefined") {
        // Otherwise, check if XDomainRequest.
        // XDomainRequest only exists in IE, and is IE's way of making CORS requests.

        xhrObj = new XDomainRequest();
        xhrObj.open(method, url);
        if (onload) {
            xhrObj.onload = onload;
        }
    } else {
        // Otherwise, CORS is not supported by the browser.
        xhrObj = null;
    }
    return xhrObj;
    }

我在控制台和Fiddler中看到的错误
实际上正在进行轮询,但每次轮询都会出现同样的失败:

Errors I am seeing in both console and Fiddler Polling is in fact occurring, but the same failures keep occur on each poll:

LOG:CONNECT_FAILED
'f.parentNode' is null or not an object
'f.parentNode' is null or not an object
LOG:CONNECT_FAILED
'f.parentNode' is null or not an object
'f.parentNode' is null or not an object
LOG:CONNECT_FAILED
'f.parentNode' is null or not an object
'f.parentNode' is null or not an object
LOG:CONNECT_FAILED
'f.parentNode' is null or not an object
'f.parentNode' is null or not an object
LOG:CONNECT_FAILED
'f.parentNode' is null or not an object
'f.parentNode' is null or not an object
LOG:CONNECT_FAILED
'f.parentNode' is null or not an object
'f.parentNode' is null or not an object

......... ......

............

(你明白了,这种情况一遍又一遍地发生。)

(you get the idea, this happens over and over as it polls.)

再一次,你可以看到每个请求都是一个接一个地发生,来自服务器的所有200个响应,但都导致来自socket.io.js文件的CONNECT_FAILED和JS错误。

Again, you can see each request firing through one after another, all 200 responses from the server but all results in CONNECT_FAILED and JS errors from the socket.io.js file.

最后,这是来自socket.io的代码客户端上的.js文件打破了上面在控制台屏幕截图中看到的错误(f.parentNode为null或不是对象)。我理解该对象为null,我没有得到的是为什么它是null。

Lastly, here is the code from the socket.io.js file on the client that is breaking with the errors seen above in the console screen shot ("f.parentNode is null or not an object"). I understand the object is null, what I don't get is WHY it is null.

........

if (this.isXDomain() && !io.util.ua.hasCORS) {
  var insertAt = document.getElementsByTagName('script')[0]
    , script = document.createElement('script');

  script.src = url + '&jsonp=' + io.j.length;
  insertAt.parentNode.insertBefore(script, insertAt);

  io.j.push(function (data) {
      complete(data);
      script.parentNode.removeChild(script);  // *** BREAKS HERE!! ***
  });

.........


推荐答案

根据这个答案,我不相信IE8支持 XMLHttpRequest()对象的 .addEventListener()方法或 XDomainRequest()(或大多数其他人,似乎稍后已添加)。

As per this answer, I don't believe IE8 supports the .addEventListener() method for the XMLHttpRequest() object or the XDomainRequest() (or most others, it appears to have been added later).

尝试将代码的这一部分重写为:

Try rewriting that portion of your code to:

xhr.onload=function () {
        set_up_socket(this, sessionString);
    };

如果你想为其他现代浏览器保留相同的语法,你可以通过将其包装回来填充条件:

If you want to keep the same syntax for other modern browsers, you could back fill by wrapping it in a conditional:

if(typeof xhr.addEventListener === undefined)
{
    xhr.onload=function () {
        set_up_socket(this, sessionString);
    };
}

不知道它是否能解决问题,但可能会有帮助。

Don't know if it will solve the problem, but may help.

MDN说 除了将*属性设置为处理函数之外,更新的浏览器(包括Firefox)还支持通过标准的addEventListener API监听XMLHttpRequest事件。。再说一次,我不确定,因为他们没有说出哪些浏览器,以及什么时候,但最好我可以告诉IE8不支持它。

MDN says "More recent browsers, including Firefox, also support listening to the XMLHttpRequest events via standard addEventListener APIs in addition to setting on* properties to a handler function.". Again, I'm not sure because they don't say which browsers, and when, but best I can tell IE8 does not support it.

这篇关于Socket.IO和IE8 - jsonp-polling连接总是失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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