ie11 websocket连接建立时报SecurityError问题


这段代码在chrome和firefox下是没有问题的,但是换成ie11之后,在for循环那块,前6个次都成功了,但是从第7次到第10次全都失败了, new WebSocket 返回异常 SecurityError


 function Socket(wsUrl){
        try {
            this._connect(wsUrl);
        } catch (e) {
            console.warn(e);
            try {
                this._connect(wsUrl);
            } catch (e) {
                console.error(e);
            }
        }
    }

    Socket.prototype = {
        _connect: function(wsUrl) {
            if("WebSocket" in window){
                this.io = new WebSocket(wsUrl);
            }else if("MozWebSocket" in window){
                this.io = new MozWebSocket(wsUrl);
            }
            if (this.io) {
                this.io.binaryType = "arraybuffer";// talking binary
                this._addEvent();
            }
        },
        _addEvent : function() {
            this.io.onopen = function() {
                console.log("WebSocket connection opened!");
            };
            this.io.onclose = function(){
                console.log("WebSocket closed!");
            };
            this.io.onerror = function(evt){
                console.log(evt.toLocaleString());
            };
        }
    };
    var lserver = 'websocket服务器域名';
    for(var i=0;i<10;i++) {
        setTimeout(function(){
            var socket = new Socket('ws://'+lserver+':8110/ws/conn');
        },i*1000);
    }

websocket JavaScript

已有该昵称 10 years, 4 months ago

IE10/11限制了到单个服务器最大并发websocket的数量,这个数字的缺省值是6,所以当你创建第7个websocket连接时就会报SecurityError。
这个数字可以在注册表里改,具体信息在 这里
不过话说回来,什么应用需要同时建立这么多WebSocket连接泥?

百野栞的创可贴 answered 10 years, 3 months ago

Your Answer