VUE实现websoket心跳

<script>

export default {

name: "listBasePageList",

created() {

this.initWebSocket();

},

methods: {

initWebSocket() {

//输入服务端创建的websoket端口

const wsuri = "ws://172.0.0.1:9999/";

//建立连接

this.websock = new WebSocket(wsuri);

//连接成功

this.websock.onopen = this.websocketonopen;

//连接错误

this.websock.onerror = this.websocketonerror;

//接收信息

this.websock.onmessage = this.websocketonmessage;

//连接关闭

this.websock.onclose = this.websocketclose;

},

reconnect() {

//重新连接

var that = this;

if (that.lockReconnect) {

return;

}

that.lockReconnect = true;

//没连接上会一直重连,设置延迟避免请求过多

that.timeoutnum && clearTimeout(that.timeoutnum);

that.timeoutnum = setTimeout(function () {

that.initWebSocket();//新连接

that.lockReconnect = false;

}, 5000);

},

reset() {

var that = this; //重置心跳

//清除时间

clearTimeout(that.timeoutObj);

clearTimeout(that.serverTimeoutObj);

that.start(); //重启心跳

},

start() {

//开启心跳

var self = this;

self.timeoutObj && clearTimeout(self.timeoutObj);

self.serverTimeoutObj && clearTimeout(self.serverTimeoutObj);

self.timeoutObj = setTimeout(function () {

//这里发送一个心跳,后端收到后,返回一个心跳消息

if (self.websock.readyState == 1) {

self.websock.send("heartbeat"); //如果连接正常

} else {

self.reconnect();//否则重连

}

self.serverTimeoutObj = setTimeout(function () {

self.websock.close();//超时关闭

}, self.timeout);

}, self.timeout);

},

websocketonopen() {

this.websocketsend("发送数据"); //连接成功事件 并且告诉服务端已经收到

//提示成功

console.log("连接成功", 3);

//开启心跳

this.start();

},

websocketonerror(e) {

//连接失败事件

//错误

console.log("WebSocket连接发生错误");

//重连

this.reconnect();

},

websocketclose(e) {

//连接关闭事件

//提示关闭

console.log("连接已关闭");

//重连

this.reconnect();

},

websocketonmessage(event) {

//接收服务器推送的信息

//打印收到服务器的内容

console.log("收到服务器信息", event.data);

//收到服务器信息,心跳重置

this.reset();

},

websocketsend(msg) {

this.websock.send(msg);//向服务器发送信息

},

},

};

</script>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
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心跳机制了。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值