我在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)中,一旦創建連接,它會一直打開,直到我離開頁面。我有火狐
檢查此[http://stackoverflow.com/questions/31627450/websocket-fails-in-firefox](http://stackoverflow.com/questions/31627450/websocket-fails-in-firefox) – kakajan
你好kakajan,這個鏈接對我很好,但它是一個自定義的動作。我需要它可以由代碼設置。可能嗎?你能幫我嗎?謝謝。 – Manel
** WebSockets API(以及底層協議)仍處於活躍的開發階段,目前在瀏覽器中存在很多兼容性問題(甚至在同一瀏覽器的不同版本之間)。**我從[mozilla的網站(https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_client_applications#Examples)檢查環節,有一些例子的WebSocket而 – kakajan