Browse Source

ws断线重连优化

master
xsx 2 years ago
parent
commit
ed0a9a1772
  1. 24
      im-uniapp/App.vue
  2. 2
      im-uniapp/common/request.js

24
im-uniapp/App.vue

@ -9,7 +9,8 @@
export default {
data() {
return {
audioTip: null
audioTip: null,
reconnecting: false //
}
},
methods: {
@ -28,6 +29,14 @@
wsApi.init();
wsApi.connect(UNI_APP.WS_URL, loginInfo.accessToken);
wsApi.onConnect(() => {
//
if(this.reconnecting){
this.reconnecting = false;
uni.showToast({
title: "已重新连接",
icon: 'none'
})
}
// 线
this.pullPrivateOfflineMessage(store.state.chatStore.privateMsgMaxId);
this.pullGroupOfflineMessage(store.state.chatStore.groupMsgMaxId);
@ -279,18 +288,23 @@
return loginInfo.expireTime < new Date().getTime();
},
reconnectWs() {
// 线token
//
this.reconnecting = true;
// token
this.reloadUserInfo().then((userInfo) => {
store.commit("setUserInfo", userInfo);
//
uni.showToast({
title: '连接已断开,尝试重新连接...',
icon: 'none',
})
store.commit("setUserInfo", userInfo);
//
let loginInfo = uni.getStorageSync("loginInfo")
wsApi.reconnect(UNI_APP.WS_URL, loginInfo.accessToken);
}).catch(() => {
this.exit();
// 5s
setTimeout(()=>{
this.reconnectWs();
},5000)
})
},
reloadUserInfo() {

2
im-uniapp/common/request.js

@ -61,7 +61,7 @@ const request = (options) => {
},
fail(error) {
uni.showToast({
title: "网络似乎有点不给力,请稍后重试",
title: "网络似乎有点不给力",
icon: "none",
duration: 1500
})

Loading…
Cancel
Save