优化长连接

This commit is contained in:
duan
2024-06-06 14:55:23 +08:00
parent 1e9797f09a
commit 554217180e
4 changed files with 39 additions and 19 deletions

View File

@@ -125,10 +125,10 @@ class webSocketUtils {
start(data) {
console.log('开启心跳检测',data)
this.heartbeatInterval = setInterval(() => {
// this.send({
// data: '心跳检测',
// type: 'jc',
// });
this.send({
data: '心跳检测',
type: 'heartbeat',
});
}, this.timeout);
}
//重新连接

View File

@@ -16,12 +16,12 @@ const baseUrl = debug ? proxyApi : "https://wxcashiertest.sxczgkj.cn/cashierServ
const baseUrlwws = 'ws://cashier.sxczgkj.cn/cashierService'
// #endif
// #ifdef APP || MP-WEIXIN
const baseUrl = 'https://cashier.sxczgkj.cn/cashierService'// 线上
const baseUrlwws = 'wss://cashier.sxczgkj.cn/cashierService' // 线上
// const baseUrl = 'https://cashier.sxczgkj.cn/cashierService'// 线上
// const baseUrlwws = 'wss://cashier.sxczgkj.cn/cashierService' // 线上
// const baseUrl = 'http://192.168.2.42:9888/cashierService'// 测试-w
// const baseUrlwws ='wss://192.168.2.42:9999/cashierService' // 测试-w
// const baseUrl = 'https://wxcashiertest.sxczgkj.cn/cashierService'// 测试
// const baseUrlwws ='wss://wxcashiertest.sxczgkj.cn/cashierService' // 测试
// const baseUrlwws ='wss://192.168.2.42:9999/netty' // 测试-w
const baseUrl = 'https://wxcashiertest.sxczgkj.cn/cashierService'// 测试
const baseUrlwws ='wss://wxcashiertest.sxczgkj.cn/cashierService' // 测试
// #endif

View File

@@ -300,10 +300,20 @@
}
},
handlemessage() {
console.log(this.socketTicket,'detail页面')
this.socketTicket ? this.socketTicket.Close() : null //调用前先判断是否有socket正在进行 先关闭后链接
this.socketTicket = new webSocketUtils(
`${uni.conf.baseUrlwws}/websocket/table/${uni.cache.get('tableCode')}/${uni.cache.get('shopUser')}/${uni.cache.get('userInfo').id}`,
5000)
// this.socketTicket = new webSocketUtils(
// `${uni.conf.baseUrlwws}/websocket/table/${uni.cache.get('tableCode')}/${uni.cache.get('shopUser')}/${uni.cache.get('userInfo').id}`,
// 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,
"type": "connect",
})
},
// 数据处理

View File

@@ -60,7 +60,8 @@
</view>
<view class="shop-item" v-for="(item1,index1) in item.products" :key="item1.id">
<view class="cover" style="width:180rpx; height: 180rpx;">
<c-image :src="`${item1.coverImg}?x-oss-process=image/resize,m_lfit,w_180,h_180`" width="180" height="180"></c-image>
<c-image :src="`${item1.coverImg}?x-oss-process=image/resize,m_lfit,w_180,h_180`"
width="180" height="180"></c-image>
</view>
<view class="info " style="display: flex;flex-direction: column;justify-content: center;">
<view class="name">
@@ -310,6 +311,7 @@
// // ...这里写你的业务逻辑
// }
// });
// setTimeout()
setTimeout(() => {
console.log('调试1')
if (uni.cache.get('token') && uni.cache.get('tableCode')) {
@@ -328,10 +330,10 @@
console.log(time, '时间戳')
uni.cache.set('shopUser', res.data)
this.handlemessage()
}else{
setTimeout(()=>{
} else {
setTimeout(() => {
uni.pro.switchTab('index/index')
},1000)
}, 1000)
}
// try {
// if (res.data) {
@@ -428,10 +430,17 @@
},
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}/websocket/table?tableId=${uni.cache.get('tableCode')}&shopId=${uni.cache.get('shopUser')}&userId=${uni.cache.get('userInfo').id}`,
// `${uni.conf.baseUrlwws}/websocket/table/${uni.cache.get('tableCode')}/${uni.cache.get('shopUser')}/${uni.cache.get('userInfo').id}`,
5000)
`${uni.conf.baseUrlwws}`,
5000, {
tableId: uni.cache.get('tableCode'),
shopId: uni.cache.get('shopUser'),
userId: uni.cache.get('userInfo').id,
"type": "connect",
})
},
// 数据处理
socketSendMsg(data) {
@@ -488,6 +497,7 @@
},
async cartadd(item, index, index1, a, b, guge) { //列表添加
console.log(item, index, index1, a, b, guge)
if (guge == '单规格') { //没有规格为空
this.specifications.duoguge = ''
this.skuidname = []