2015-09-22 28 views
1

我在JavaScript中有下面的代碼:的WebSockets不工作

function ConnectWebSocket() { 
if ("WebSocket" in window) { 
    myWebsocket = new WebSocket("wss://myserver/mychannel"); 
    myWebsocket.onmessage = function(evt) { 
     alert("onmessage"); 
    } 
    myWebsocket.onopen = function() { 
     alert("onopen"); 
     myWebsocket.send("msg0"); 
     myWebsocket.send("msg1"); 
     myWebsocket.send("msg2"); 
    } 
    myWebsocket.onclose = function() { 
     alert("onclose"); 
     ConnectWebSocket(); 
    } 
    } else { 
    // Do something if there is no websockets support 
    } 
} 
ConnectWebSocket(); 

的問題是,在Firefox中,該連接發送消息後關閉,並重新打開由於對OnClose事件的命令。如果我嘗試在onopen上只發送一條消息,則連接會保持打開狀態,但如果我嘗試發送多條消息,則會關閉連接。此問題僅在Firefox中出現,而不在Chrome中,不在IE中,不在Safari中。

有人可以幫助我嗎?在其他瀏覽器(如IE或Chrome)中,一旦創建連接,它會一直打開,直到我離開頁面。我有火狐

+0

檢查此[http://stackoverflow.com/questions/31627450/websocket-fails-in-firefox](http://stackoverflow.com/questions/31627450/websocket-fails-in-firefox) – kakajan

+0

你好kakajan,這個鏈接對我很好,但它是一個自定義的動作。我需要它可以由代碼設置。可能嗎?你能幫我嗎?謝謝。 – Manel

+0

** WebSockets API(以及底層協議)仍處於活躍的開發階段,目前在瀏覽器中存在很多兼容性問題(甚至在同一瀏覽器的不同版本之間)。**我從[mozilla的網站(https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_client_applications#Examples)檢查環節,有一些例子的WebSocket而 – kakajan

回答

0

的40.0.3v這可能是因爲你希望你的support變種沒有表現。下面的代碼在Firefox而不關閉連接:

function ConnectWebSocket() { 

    if ("WebSocket" in window) { 
     myWebsocket = new WebSocket("ws://echo.websocket.org/"); 
     myWebsocket.onmessage = function (evt) { 
      alert("onmessage"); 
     } 
     myWebsocket.onopen = function() { 
      alert("onopen"); 
      myWebsocket.send("a test message"); 
     } 
     myWebsocket.onclose = function() { 
      alert("onclose"); 
      ConnectWebSocket(); 
     } 
    } else { 
     // Do something if there is no websockets support 
    } 
} 
ConnectWebSocket(); 

​​


2

試試這個例子:

var url = "ws://echo.websocket.org"; 

if (!window.WebSocket) alert("WebSocket not supported by this browser"); 

var myWebSocket = { 
    connect: function() { 
     var location = url 
     this._ws = new WebSocket(location); 
     this._ws.onopen = this._onopen; 
     this._ws.onmessage = this._onmessage; 
     this._ws.onclose = this._onclose; 
     this._ws.onerror = this._onerror; 
    }, 

    _onopen: function() { 
     console.debug("WebSocket Connected"); 
    }, 

    _onmessage: function (message) { 
     console.debug("Message Recieved: " + message.data); 
    }, 

    _onclose: function() { 
     console.debug("WebSocket Closed"); 
     kiosk.connect(); 
    }, 

    _onerror: function (e) { 
     console.debug("Error occured: " + e); 
    }, 

    _send: function (message) { 
     console.debug("Message Send: " + message); 
     if (this._ws) this._ws.send(message); 
    } 
}; 

myWebSocket.connect(); 
setInterval(function() { 
    myWebSocket._send('msg1'); 
}, 5000); 

這裏是一個JSFidlle

+0

你好kakjan,在這裏你可以看到我的控制檯日誌:套接字已打開。狀態= 1 套接字已關閉。 State = 3 連接到wss:// myWebsocketServer/MyChannel在頁面加載時中斷。這3個句子每2秒重複一次。 – Manel

+0

@Manel我更新了我的答案,嘗試一下,我添加了我自己的項目中使用的websocket示例,還有一個jsfiddle鏈接,我在firefox,chrome,safari中嘗試了它,沒有任何問題。 – kakajan

+0

你好kakajan,謝謝你的回答,但是恐怕仍然不能用於firefox。你可以用這個URL「wss://pre-ws.sureact.com:443/myChannel」來嘗試你的代碼,你會看到我正在獲取的console.log。請注意,我在「about:config」處有「network.websocket.extensions.permessage-deflate; true」。請注意,Manel – Manel

0

試試吧。

var WS = window.WebSocket || window.MozWebSocket;; 

if (WS){ 
    var websocket = new WS("wss://myserver/mychannel"); 
} 
相關問題