1
背景:寫一個websocket。在Chrome中運行良好。單一連接。在Firefox中運行良好,但它建立了兩個連接,我不知道爲什麼。相同的JavaScript,相同的網頁,相同的套接字服務器。Firefox Websocket雙連接
客戶端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看到一個連接,並調用適當的握手和操作。在Firefox上,您可以同時進行兩個連接,一次握手並執行操作(它在瀏覽器上執行的操作),但第二個連接在5秒鐘後啓動。這只是一個Firefox的事情?我沒有在Firebug中顯示兩個連接,只是單個連接。如果它只是一個Firefox的東西,我可以和它一起生活,因爲正確的數據會被髮送和接收,但是如果是這樣的話,我會在他們身上咧嘴一笑。
嗯,你說得對。我甚至沒有意識到我沒有運行最新版本。謝謝! –