websocket连接以及心跳机制在vue下的实现

本文介绍了WebSocket协议的基本原理,展示了在Vue中如何通过getRandomNumber函数确保连接ID唯一性,并详细讲解了如何创建WebSocket连接、处理消息和实现心跳检测机制,以防网络断开时的数据丢失。
摘要由CSDN通过智能技术生成

WebSocket

1.什么是webSocket

webSocket协议是HTML5的一种通信协议,该协议兼容我们常用的浏览器。例如Chrome、Firefox、IE等。它可以使客户端和服务端双向数据传输更加简单快捷,并且在TCP连接进行一次握手后,就可以持久性连接,同时允许服务端对客户端推送数据。外加传统模式的协议一般HTTP请求可能会包含较长的头部,但真正有效的可能只有小部分,从而占用了很多资源和带宽。因此WebSocket协议不仅实时通讯,支持扩展;也可以压缩节省服务器资源和带宽。WS协议和WSS协议两个均是WebSocket协议的SCHEM,两者一个是非安全的,一个是安全的。也是统一的资源标识符。其中WSS表示TLS之上的WebSocket。WS一般默认是80端口,而WSS默认是443端口。

2.vue如何实现webSocket

首先每次连接都会对应一个连接ID,这里我称为connectId,编写一个随机id生成函数

function getRandomNumber(){
    this.connectId = Math.floor(Math.random()*1000)
    //如果两台客户端使用了相同的connectId,发送消息时只会有一台收到,所以要询问后台id是否已经存在连接
    postAction(this.url.queryCId,{connectId:this.connectId}).then(res => {
        if(res.success && res.result===0){
            this.openSocket()
        }else{
            this.getRandomNumber()
        }
    })
}

接着我们建立一个websocket连接

function openSocket(){
    if(typrof WebSocket == 'undefined'){
    	console.log('您的浏览器不支持WebSocket')
	}else{
    	console.log('您的浏览器支持WebSocket')
    	let socketUrl = process.env.VUE_APP_BASEURL + this.connectId
        socketUrl = socketUrl.replace('https','ws').repalce('http','ws')
        if(this.socket != null){
            this.socket.close()
            this.socket = null
        }
        this.socket = new WebSocket(socketUrl)
        //打开事件
        this.socket.onopen = function(){
            console.log('websocket已经打开')
        }
        //获得消息
        this.socket.onmessage = function(msg){
            //对返回数据进行处理
        }
        //关闭事件
        this.socket.onclose = function(){
            console.log('websocket已关闭')
        }
        //发生了错误事件
        this.socket.onerror = function(e){
            console,log('websocket发生了错误')
            console.log(e)
        }
	}
}

在使用websocket的过程中,有时候会遇到网络断开的情况,但是在网络断开的时候服务器端并没有触发onclose的事件。这样可能会发生服务器会继续向客户端发送数据,并且这些数据会丢失。所以需要一种机制来检测客户端和服务端是否处于正常的链接状态。因此就有了websocket的心跳,检测链接的状态。

let lockReconnect = false; //避免重复连接
const heartCheck = {
    timeout:3000,
    timeoutobj:null,
    serverTimeoutObj:null,
    start:function(){
        let that = this
        clearTimeout(this.timeoutObj)
        clearTimeout(this.serverTimeoutObj)
        this.timeObj = setTimeout(function(){
            //这里发送一个心跳,后端收到后,返回一个心跳,这里用的ping-pong
            window.socket.send('ping')
            that.serverTimeoutObj = setTimeout(function(){
                window.socket.close()
            },that.timeout)
        },this.timeout)
    }
}
function createWebSocket(){
    try{
       let socketUrl = process.env.VUE_APP_BASEURL + this.connectId
		socketUrl = socketUrl.replace('https','ws').repalce('http','ws')
        this.socket = new WebSocket(socketUrl)
        init(socketUrl)
    }catch(e){
        console.log('catch');
        reconnect(socketUrl)
    }
}
function init(socketUrl){
    //打开事件
    this.socket.onopen = function(){
        console.log('websocket已经打开')
        //心跳检测重置
        heartCheck.start()
    }
    //获得消息
    this.socket.onmessage = function(msg){
        //对返回数据进行处理
        console.log(msg)
        heartCheck.start()
    }
    //关闭事件
    this.socket.onclose = function(){
        console.log('websocket已关闭')
        reconnect(socketUrl)
    }
    //发生了错误事件
    this.socket.onerror = function(e){
        console,log('websocket发生了错误'+e)
        reconnect(socketUrl)
    }
}
function reconnect(){
    if(lockReconnect){
        return
    }
    lockReconnect = true
    //没连接上会一直重连,设置延迟避免请求过多
    setTimeout(function(){
        this.createWebSocket()
        this.lockReconnect = false
    },2000)
}
Vue实现WebSocket心跳机制可以通过以下步骤进行: 1. 首先,安装WebSocket库。可以使用npm或yarn来安装,例如:`npm install vue-native-websocket`。 2. 在Vue组件中引入WebSocket库,并创建WebSocket实例。可以在Vue组件的`created`或`mounted`生命周期钩子函数中创建WebSocket实例。例如: ```javascript import VueNativeSock from 'vue-native-websocket'; export default { created() { Vue.use(VueNativeSock, 'ws://localhost:8080', { reconnection: true, // 是否自动重连 reconnectionAttempts: 5, // 重连尝试次数 reconnectionDelay: 3000, // 重连延迟时间(毫秒) format: 'json', // 数据格式 }); }, }; ``` 3. 添加心跳机制。可以使用Vue的定时器函数`setInterval`来定时发送心跳包。例如: ```javascript export default { created() { // 创建WebSocket实例 Vue.use(VueNativeSock, 'ws://localhost:8080', { reconnection: true, reconnectionAttempts: 5, reconnectionDelay: 3000, format: 'json', }); // 发送心跳包 setInterval(() => { this.$socket.sendObj({ type: 'heartbeat' }); }, 5000); // 每隔5秒发送一次心跳包 }, }; ``` 4. 监听心跳回复。在Vue组件中监听WebSocket的消息事件,当接收到心跳回复时,可以根据需要进行相应的处理。例如: ```javascript export default { created() { // 创建WebSocket实例 Vue.use(VueNativeSock, 'ws://localhost:8080', { reconnection: true, reconnectionAttempts: 5, reconnectionDelay: 3000, format: 'json', }); // 发送心跳包 setInterval(() => { this.$socket.sendObj({ type: 'heartbeat' }); }, 5000); // 监听WebSocket消息事件 this.$socket.onMessage((message) => { const data = JSON.parse(message.data); if (data.type === 'heartbeat_reply') { // 处理心跳回复 console.log('Received heartbeat reply'); } }); }, }; ``` 这样,你就可以在Vue实现WebSocket心跳机制了。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值