关于html:html-h5-vue-websocket-断链-心跳-个人笔记

let webSocket = null;
let mAudio = null;
let webSocketStateInterval = null;
let heartInterval = null;
let number = 3;
let _closeType = false; // true被动挂断 false被动挂断

export default {
    initWebsocket() {
        let _that = this;
        let userInfo = JSON.parse(localStorage.getItem("userInfo"));
        let _url = process.env.VUE_APP_socketURL + userInfo.id;
        webSocket = new WebSocket( _url);
        webSocket.onopen = function(event) {
            // console.log("webSocket onopen ----->", event);

            heartInterval = setInterval(function(){
                webSocket.send(JSON.stringify({cmd: 100}));
            }, 10000);

            webSocketStateInterval = setInterval(function(){
                switch (webSocket.readyState) {
                    case WebSocket.CONNECTING:
                        console.log("webSocket连贯中---->");
                        break;
                    case WebSocket.OPEN:
                        console.log("webSocket关上中---->");
                        break;
                    case WebSocket.CLOSING:
                        console.log("webSocket敞开中---->");
                        break;
                    case WebSocket.CLOSED:
                        console.log("webSocket敞开胜利---->");
                        if(webSocketStateInterval){
                            clearInterval(webSocketStateInterval);
                        }
                        if(_closeType == false){
                            _that.initWebsocket();
                            // number--; if(number === 0) {webSocket.close();}
                        }
                        break;
                    default:
                        console.log("webSocket-------");
                        break;
                }
            }, 2000);
        };
        webSocket.onmessage = function(event) {
            // console.log("webSocket onmessage ----->", event);
            let message = JSON.parse(event.data);
            // console.log("message----->", message);
            if (message.cmd == 100) {
                console.log("100-心跳中------", message.msg);
            }
        };
        webSocket.onerror = function(event) {
            console.log("webSocket onerror ----->", event);
            _that.initWebsocket();
        };
        webSocket.onclose = function(event) {
            console.log("webSocket onclose --->", event);
            if(_closeType){
                if(webSocketStateInterval){
                    clearInterval(webSocketStateInterval);
                }
            }
            if(heartInterval){
                clearInterval(heartInterval);
            }
            _closeType = false;
        };
        // return new Promise((resolve, reject) => {resolve()});
    },
    sendWebSocket(text) {
        webSocket.send(JSON.stringify(text));
    },
    closeWebSocket(closeType) {
        _closeType = closeType;
        webSocket.close();
    },
    closeAudio() {
        mAudio.pause();
    }
}

【腾讯云】云产品限时秒杀,爆款1核2G云服务器,首年99元

阿里云限时活动-1核2G-1M带宽-40-100G ,特惠价87.12元/年(原价1234.2元/年,可以直接买3年),速抢

本文由乐趣区整理发布,转载请注明出处,谢谢。

You may also like...

发表评论

邮箱地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据