websocket复制使用


data()
{
  return{
   websockets:""
  }
},
methods:{
 //3.建立websockets连接数据
        initWebSockets() {
            if (typeof WebSocket === "undefined") {
                return false;
            }
            let wsuri;
            // websocket地址
            wsuri = 'ws://-------------------';
            // console.log('传送的ws是:',wsuri)
            this.websockets = new WebSocket(wsuri);
            this.websockets.onopen = this.websocketonopens;
            this.websockets.onmessage = this.websocketonmessages;
            this.websockets.onerror = this.websocketonerrors;
            this.websockets.onclose = this.websocketcloses;
        },
        //连接成功
        websocketonopens() {
            console.log("WebSocket连接成功");
            // let token = JSON.parse(sessionStorage.getItem('token'))
            // let actions = {
            //     "token": token,
            // };
            // if (this.websockets.readyState === 1) {
            //     this.websockets.send(JSON.stringify(actions))
            // }
            // console.log('JSON.stringify(actions)',JSON.stringify(actions))
            // console.log('actions',actions)
            // // 添加心跳检测,每30秒发一次数据,防止连接断开(这跟服务器的设置有关,如果服务器没有设置每隔多长时间不发消息断开,可以不进行心跳设置)
            // let self = this;
            // if (this.websockets.readyState === 1) {
            //     this.timerWS = setInterval(() => {
            //         try {
            //             let token = JSON.parse(sessionStorage.getItem('token'))
            //             let actions = {
            //                 "token": token,
            //             };
            //             self.websockets.send(JSON.stringify(actions));
            //         } catch (err) {
            //             console.log('断开了:' + err);
            //             self.connection()
            //         }
            //     }, 30000)
            // }
        },
        //接收后端返回的数据,可以根据需要进行处理
        websocketonmessages(e) {
            let data1Json = JSON.parse(e.data);
            new Promise((resolve) => {
                setTimeout(() => {
                    setTimeout(() => {
                        if (data1Json.id) {
                           //数据处理
                        }
                    }, 40)
                    resolve()
                }, 40)
            }).then(() => {
                return new Promise((resolve) => {
                    resolve()
                })
            })
        },
        //连接建立失败重连
        websocketonerrors(e) {
            console.log(`连接失败的信息:`, e);
            this.initWebSockets(); // 连接失败后尝试重新连接
        },
        //关闭连接
        websocketcloses(e) {
            console.log("断开连接", e);
        }
},
destroyed(){
if(this.websockets){
this.websockets.close()
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值