javascript Websocket 不工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32721263/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 15:39:41  来源:igfitidea点击:

Websockets not working

javascriptjqueryfirefoxwebsocket

提问by Manel

I have the following code in javascript:

我在javascript中有以下代码:

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();

The problem is that in Firefox, the connection is closed after sending the messages, and reopened due to the command on the onclose event. If I try to send only one message on onopen, the connection keeps opened, but if I try to send more than one message, the connection shut down. This issue appears only in Firefox, not in Chrome, not in IE, not in Safari.

问题是在 Firefox 中,连接在发送消息后关闭,并由于 onclose 事件上的命令重新打开。如果我尝试在 onopen 上只发送一条消息,连接将保持打开状态,但如果我尝试发送多条消息,连接将关闭。此问题仅在 Firefox 中出现,在 Chrome 中没有,在 IE 中没有,在 Safari 中也没有。

Can someone help me? In other browsers like IE or Chrome, once the connection is created, it keep opened until I leave the page. I have the 40.0.3v of Firefox

有人能帮我吗?在 IE 或 Chrome 等其他浏览器中,一旦创建连接,它就会一直打开,直到我离开页面。我有 40.0.3v 的 Firefox

回答by kakajan

Try this example:

试试这个例子:

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);

Here is a JSFidlle

这是一个JSFidlle

回答by Michael Doye

It may be that your supportvar is not behaving as you expect. The following code works in FireFox without closing the connection:

可能是您的supportvar 行为不符合您的预期。以下代码可在 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();

Example Fiddle

示例小提琴



  • 您可以使用Websocket.org上的工具来确保 websockets 在您的浏览器中正常工作。
  • 或者(尽管您的问题与 FF 相关)您可以使用此处列出步骤来调试 websocket。

回答by toto

Try it.

试试看。

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

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