求教 WebSocket 可否设置超时时长?

拔网线这种情况是没有办法的,你得自己心跳包判断超时主动断连。

问题就在于主动断开 花费的时间太长 没有onclose之前 你都没办法建立新的别的连接

我没理解没有办法建立新的连接是具体什么问题?

如果能贴一些代码片段,帮助我们理清楚你目前的使用逻辑,对我们重现与修复这个问题会有很大帮助,谢谢。

    connect(func) {
        console.log('---> connect');
        var self = this;
        this.getIp((a) => {
            console.log('--->> connect to ' + a);
            var connectURL = 'ws://' + a;
            self.ws = new WebSocket(connectURL);

            self.ws.onopen = (event) => {
                console.log("--->>> WS was opened.");
                if (func) { func() };
            };
            self.ws.onmessage = function (event) {
                var data = JSON.parse(event.data);
                console.log("--->>> Recv CMD:" + data.cmd);
                msgList.push(data);
            };
            self.ws.onerror = function (event) {
                console.log("--->>> Send Text fired an error.");
            };
            self.ws.onclose = function (event) {
                console.log("--->>> WebSocket instance closed.");
                if (isLogin) { self.login() }
            };
        })
    },

    send(data) {
        var self = this;
        if (!cc.sys.isObjectValid(this.ws)) {
            this.connect((e) => {
                console.log('---> ws is none. Reconnet Send CMD:' + data.cmd);
                self.ws.send(JSON.stringify(data));
                console.log(data);
            });
        }
        else if (this.ws.readyState == WebSocket.OPEN) {
            console.log('---> ws is ok. Send CMD:' + data.cmd);
            this.ws.send(JSON.stringify(data));
            console.log(data);
        }
        else {
            this.ws.close();
            this.connect((e) => {
                console.log('---> ws is bad, Reconnet Send CMD:' + data.cmd);
                self.ws.send(JSON.stringify(data));
                console.log(data);
            });
        }
    },

调用 ws.close() 立即new WebSocket(connectURL); 会报错 要等到 ws.onclose 才能正常new 就这么简单的意思

 var id = 0;

        function myWebSocketTest() {
            var currentId = id++;

            window.ws = new WebSocket("ws://echo.websocket.org");
            log("new WebSocket: " + currentId);

            var oncloseRetry = true;

            window.ws.onopen = function() {
                log("onopen: " + currentId);
                var cancel = false;
                
                function sendHello() {
                    if (cancel)
                        return;
                    ws.send("hello");
                    setTimeout(sendHello, 1000);
                };
                setTimeout(sendHello, 1000);

                setTimeout(function() {
                    cancel = true;

                    if (window.ws != null) {
                        window.ws.close();
                        window.ws = null;

                        oncloseRetry = false;
                    }

                    myWebSocketTest();
                }, 10000);
            };

            window.ws.onmessage = function(e) {
                log("onmessage: " + e.data + ", " + currentId);
            };
            window.ws.onerror = function() {
                log("onerror: " + currentId);
            };
            window.ws.onclose = function() {
                log("onclose: " + currentId + ", oncloseRetry:" + oncloseRetry);
                if (!oncloseRetry)
                    return;

                window.ws = null;
                setTimeout(function(){
                    myWebSocketTest();
                }, 2000);
            };
        }

        myWebSocketTest();

我用如上测试代码,验证在onopen调用后,断网的情况下,去new 新的websocket实例,是没问题的。没碰到错误与崩溃,当网络连接上后,会立马重新onopen的。
以下是日志:

JS: Cocos2d-JS v3.14
JS: Success to load scene: db://assets/TestList.fire
JS: Please set label text key in Text Key property.
JS: Please set label text key in Text Key property.
JS: Please set label text key in Text Key property.
JS: Please set label text key in Text Key property.
JS: new WebSocket: 0
JS: onopen: 0
JS: onmessage: hello, 0
JS: onmessage: hello, 0
JS: onmessage: hello, 0
JS: onmessage: hello, 0
JS: onmessage: hello, 0
JS: onmessage: hello, 0
JS: onmessage: hello, 0
JS: onmessage: hello, 0
JS: onmessage: hello, 0
JS: new WebSocket: 1
JS: onclose: 0, oncloseRetry:false
JS: onopen: 1
JS: onmessage: hello, 1
JS: onmessage: hello, 1
JS: onmessage: hello, 1
JS: onmessage: hello, 1
JS: onmessage: hello, 1
JS: onmessage: hello, 1
JS: onmessage: hello, 1
JS: onmessage: hello, 1
JS: onmessage: hello, 1
JS: new WebSocket: 2
JS: onclose: 1, oncloseRetry:false
JS: onopen: 2
JS: onmessage: hello, 2
JS: onmessage: hello, 2
JS: new WebSocket: 3
JS: onerror: 3
JS: onclose: 3, oncloseRetry:true
JS: onclose: 2, oncloseRetry:false
JS: new WebSocket: 4
JS: onerror: 4
JS: onclose: 4, oncloseRetry:true
JS: new WebSocket: 5
JS: onerror: 5
JS: onclose: 5, oncloseRetry:true
JS: new WebSocket: 6
JS: onerror: 6
JS: onclose: 6, oncloseRetry:true
JS: new WebSocket: 7
JS: onerror: 7
JS: onclose: 7, oncloseRetry:true
JS: new WebSocket: 8
JS: onerror: 8
JS: onclose: 8, oncloseRetry:true
JS: new WebSocket: 9
JS: onerror: 9
JS: onclose: 9, oncloseRetry:true
JS: new WebSocket: 10
JS: onerror: 10
JS: onclose: 10, oncloseRetry:true
JS: new WebSocket: 11
JS: onerror: 11
JS: onclose: 11, oncloseRetry:true
JS: new WebSocket: 12
JS: onerror: 12
JS: onclose: 12, oncloseRetry:true
JS: new WebSocket: 13
JS: onopen: 13
JS: onmessage: hello, 13
JS: onmessage: hello, 13
JS: onmessage: hello, 13
JS: onmessage: hello, 13
JS: onmessage: hello, 13
JS: onmessage: hello, 13

你可以跑这个测试例验证一把。

以我的经验,不同的路由器可能出现不一样的问题。

不可能不能new新的WebSocket吧?那我们同时连好几个服务器呢,自然要new好几个WebSocket对象。

close->onclose 之间new 会报错… 其他时候 可以随便new

我们也有几个用户碰到这个情况,就是close后,好长一段时间都连接不上去,但大部分人又没有这个问题,平台是ios.

我上面发的测试例,覆盖到了close和onclose回调之间去new WebSocket实例的情况。
所以我这里还是没法重现出你的问题。

close->onclose 之间new 会报错… --》请问这个问题解决了吗,我可能也碰到了

我也想问,求问!!!

请问兄弟这个问题有解决吗??

的确有这个问题,在close 没回调onclose之前,无法new websocket
以下就是new的新socket 报的错误
WebSocket connection to ‘ws://192.168.1.113:8081/’ failed: WebSocket is closed before the connection is established.