Firefox Websocket双连接

时间:2012-10-19 04:17:40

标签: javascript html5 firefox websocket

背景:编写websocket。适用于Chrome。单连接。在Firefox中运行良好,但它有两个连接,我不知道为什么。相同的javascript,相同的网页,相同的套接字服务器。

客户端JS:

var ws;
doWebSocketSetup();
function doWebSocketSetup(){
    console.log('Connecting...');
    ws=new WebSocket('ws://mysocketserver.com:9300/demo');

    ws.onopen=function(){
        console.log('Connected!')
        socketSend('connect',sessionStorage.user);
        if(!sessionStorage.announce){
            socketSend('login',sessionStorage.user);
            sessionStorage.announce=true;
        }
        socketSend('view',window.location.href);
        setTimeout(function(){
            processQueue();
        },100);
    }
    ws.onmessage=function(e){
        if(e.data!='ok'){
            $.msg(e.data,{header:'Server Message',live:10000});
            console.log('Server: '+e.data);
        }
        processQueue();
    }
    ws.onclose=function(){
        console.log('Disconnected');
    }
}
var sendQueue=new Array();
function socketSend(action,data){
        var payload = new Object()
        payload.action = action
        payload.client = sessionStorage.user
        payload.data = data
        sendQueue.unshift(payload);
}
function processQueue(){
    if(sendQueue.length==0)
        return;
    var payload=sendQueue.pop()
    console.log('Sending: '+JSON.stringify(payload));
    ws.send(JSON.stringify(payload))
}

现在Firefox上的服务器控制台输出:

2012-10-18 20:58:53 [info] [client 68.99.226.57:53079] Connected 94e568176299729fa8669c512fdb107d 
2012-10-18 20:58:53 [info] [client 68.99.226.57:53080] Connected 457eb971eabaeba6b6afd637755ce53c 
2012-10-18 20:58:53 [info] [client 68.99.226.57:53080] Performing handshake 
2012-10-18 20:58:53 [info] [client 68.99.226.57:53080] Handshake sent 
2012-10-18 20:58:54 [info] [client 68.99.226.57:53080] _actionConnect 
2012-10-18 20:58:54 [info] [client 68.99.226.57:53080] _actionView 
2012-10-18 20:58:58 [info] [client 68.99.226.57:53079] Disconnected - 94e5681762 99729fa8669c512fdb107d

在Chrome上:

2012-10-18 21:09:17 [info] [client 68.99.226.57:53161] Connected 3906f16fa4037cbb08a8a5d1d6094cea
2012-10-18 21:09:17 [info] [client 68.99.226.57:53161] Performing handshake
2012-10-18 21:09:17 [info] [client 68.99.226.57:53161] Handshake sent
2012-10-18 21:09:17 [info] [client 68.99.226.57:53161] _actionConnect
2012-10-18 21:09:17 [info] [client 68.99.226.57:53161] _actionView

您在Chrome上看到一个SINGLE连接,并调用相应的握手和操作。在Firefox上,您可以在同一时间完成两个连接,一个握手并执行操作(在浏览器上执行操作),但第二个连接在5秒后启动。这只是一个Firefox的东西吗?我没有在Firebug中显示两个连接,只是单个连接。如果它只是一个Firefox的东西,我可以忍受它,因为正确的数据确实被发送和接收,但如果是这样的话,我会对它们皱眉头。

1 个答案:

答案 0 :(得分:2)

这是Firefox 15中的一个错误,它会创建一个从不使用的推测连接(事实上它不能,因为它是一个WebSocket连接而不是HTTP连接)。

它已在Firefox 16中修复。