javascript - Socket.IO 和 IE8 - jsonp 轮询连接总是失败

标签 javascript jquery sockets internet-explorer-8 socket.io

值得注意:以下是通过 https 跨域完成的。老实说,我不认为这是问题所在,因为在 IE10、Chrome 和 FF 中一切正常。我的猜测是它可能是 IE8 中的 XDomainRequest 对象变体?虽然不确定。

下面的sendLoginRequest方法是最先调用的方法。下面还提供了所有其他支持代码。

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

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 中看到的错误 轮询实际上正在发生,但每次轮询都会出现相同的失败:

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

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

(你明白了,这在轮询时一遍又一遍地发生。)

同样,您可以看到每个请求一个接一个地触发,来自服务器的所有 200 个响应都导致 CONNECT_FAILED 和 socket.io.js 文件中的 JS 错误。

最后,这里是客户端上的 socket.io.js 文件中的代码,它打破了上面在控制台屏幕截图中看到的错误(“f.parentNode 为 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!! ***
  });

.........

最佳答案

根据 this answer ,我不认为 IE8 支持 XMLHttpRequest() 对象或 XDomainRequest()(或大多数其他对象)的 .addEventListener() 方法,它似乎是后来添加的)。

尝试将该部分代码重写为:

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

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

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

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

MDN 说 "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." .同样,我不确定,因为他们没有说明哪些浏览器以及何时使用,但我最好能告诉您 IE8 不支持它。

关于javascript - Socket.IO 和 IE8 - jsonp 轮询连接总是失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18637953/

相关文章:

javascript - Maphilight - 需要它来停用新鼠标单击时的突出显示

c - 套接字如何利用100%的网络带宽?

javascript - firefox window.onerror 事件问题 - 仅警报 "Script error!"

javascript - 动画 html 元素 - flexslider

javascript - 使用正则 exp 将字符串中的每个单词用 span 包裹起来

ios - 让 GCDAsyncSocket 快速获取 SSL

linux - 简单的原始数据包捕获和发送程序

javascript - "Bada"- 这个 javascript 片段发生了什么

javascript - jquery改变一个div类的样式属性

javascript - 使用 Highcharts 获取 Solid Guage 的刻度位置和标签