This commit is contained in:
魏啾
2024-06-07 17:21:52 +08:00
parent c8633827cc
commit 5a16a5199e
2 changed files with 166 additions and 192 deletions

View File

@@ -3,7 +3,7 @@ class webSocketUtils {
this.socketTask = null;
this.is_open_socket = false; //避免重复连接
this.url = url;
this.params = params ? params : null;////是否初始化请求
this.params = params ? params : null; ////是否初始化请求
this.connectNum = 1; // 重连次数
//这个参数是防止重连失败之后onClose方法会重复执行reconnect方法导致重连定时器出问题
//连接并打开之后可重连,且只执行重连方法一次
@@ -23,10 +23,9 @@ class webSocketUtils {
}
}
// 进入这个页面的时候创建websocket连接【整个页面随时使用】
connectSocketInit (data) {
console.log(data,"初始化")
connectSocketInit(data) {
this.data = data;
console.log('this.url==', this.url);
this.socketTask ? this.socketTask.Close() : null
this.socketTask = uni.connectSocket({
url: this.url,
success: () => {
@@ -36,11 +35,10 @@ class webSocketUtils {
return this.socketTask;
},
});
console.log('this.params==', this.params);
this.socketTask.onOpen((res) => {
this.connectNum = 1;
console.log('WebSocket连接正常');
if(this.params){//是否初始化请求
if (this.params) { //是否初始化请求
this.send(this.params);
}
@@ -97,10 +95,10 @@ class webSocketUtils {
});
}
// 主动关闭socket连接
Close () {
Close() {
this.is_open_socket = true;
this.canReconnect = false;
if(this.socketTask){
if (this.socketTask) {
this.socketTask.close({
success(res) {
console.log('手动关闭成功');
@@ -123,7 +121,7 @@ class webSocketUtils {
}
//开启心跳检测
start(data) {
console.log('开启心跳检测',data)
console.log('开启心跳检测', data)
this.heartbeatInterval = setInterval(() => {
this.send({
data: '心跳检测',
@@ -138,7 +136,7 @@ class webSocketUtils {
//如果不是人为关闭的话,进行重连
if (!this.is_open_socket) {
console.log('进行重连');
// this.canReconnect = true;
this.canReconnect = true;
this.reconnectTimeOut = setInterval(() => {
this.connectSocketInit(this.data);
}, this.timeout);

View File

@@ -283,42 +283,32 @@
uni.reLaunch({
url: '/pages/login/login?types=' + 0
});
return false
}
uni.$on('message', this.getMessage)
setTimeout(() => {
if (uni.cache.get('token') && uni.cache.get('tableCode')) {
this.productqueryShopIdByTableCode() //获取shop User id
}
}, 500)
this.$nextTick(() => {
this.countTitleTopNum(); //导航栏
});
},
onUnload() {
try {
this.socketSendMsg({ //定义socket数据传参
"type": "close", //“addcart:添加购物车create0rder:生成订单clearCart:庆康购物车”,
})
console.log(this.socketTicket, '关闭长连接')
} catch (e) {
//TODO handle the exception
}
this.socketTicket.Close()
uni.$off('message')
},
onShow() {
// console.log(this.socketTicket,'进入页面')
// if (this.socketTicket) {
// this.socketTicket.Close()
// uni.$off('message')
// }
// uni.onNetworkStatusChange((res) => { //监听网络状态变化
// if (!res.isConnected) {
// // 检查网络是否连接
// } else {
// console.log(res, 111);
// // ...这里写你的业务逻辑
// }
// });
// setTimeout()
this.handlemessage()
setTimeout(() => {
console.log('调试1')
if (uni.cache.get('token') && uni.cache.get('tableCode')) {
this.productqueryShopIdByTableCode() //获取shop User id
}
}, 500)
},
methods: {
// 单独获取他的shopUserid
@@ -337,16 +327,6 @@
uni.pro.switchTab('index/index')
}, 1000)
}
// try {
// if (res.data) {
// let time = new Date
// console.log(time, '时间戳')
// uni.cache.set('shopUser', res.data)
// this.handlemessage()
// }
// } catch (e) {
// //TODO handle the exception
// }
},
getMessage(msg) { //wss 回显数据
if (msg.status != 'success') {
@@ -432,12 +412,7 @@
},
handlemessage() {
this.socketTicket ? this.socketTicket.Close() : null //调用前先判断是否有socket正在进行 先关闭后链接
// this.socketTicket = new webSocketUtils(
// `${uni.conf.baseUrlwws}/websocket/table?tableId=${uni.cache.get('tableCode')}&shopId=${uni.cache.get('shopUser')}&userId=${uni.cache.get('userInfo').id}`,
// 5000)
this.socketTicket = new webSocketUtils(
`${uni.conf.baseUrlwws}`,
5000, {
this.socketTicket = new webSocketUtils(`${uni.conf.baseUrlwws}`, 5000, {
tableId: uni.cache.get('tableCode'),
shopId: uni.cache.get('shopUser'),
userId: uni.cache.get('userInfo').id,
@@ -665,6 +640,7 @@
switch (t) {
case 1:
// 返回
this.socketTicket.Close()
uni.switchTab({
url: '/pages/index/index'
})