更新详情

This commit is contained in:
魏啾
2024-06-19 17:11:23 +08:00
parent cba4e6065e
commit 802cc2d721
9 changed files with 223 additions and 214 deletions

View File

@@ -28,7 +28,7 @@ class webSocketUtils {
this.socketTask = uni.connectSocket({
url: this.url,
success: () => {
console.log('正准备建立websocket中...');
// console.log('正准备建立websocket中...');
// uni.hideLoading();
// 返回实例
return this.socketTask;
@@ -37,7 +37,7 @@ class webSocketUtils {
this.socketTask.onOpen((res) => {
uni.hideLoading()
this.connectNum = 1;
console.log('WebSocket连接正常');
// console.log('WebSocket连接正常');
if (this.params) { //是否初始化请求
this.send(this.params);
}
@@ -52,7 +52,6 @@ class webSocketUtils {
let res = JSON.parse(e.data);
uni.$emit('message', res)
// 普通socket信息处理 TODO
});
});
// 监听连接失败这里代码我注释掉的原因是因为如果服务器关闭后和下面的onclose方法一起发起重连操作这样会导致重复连接
@@ -107,20 +106,20 @@ class webSocketUtils {
}
//发送消息
send(data) {
console.log("发送消息---------->", data);
// console.log("发送消息---------->", data);
// 注:只有连接正常打开中 ,才能正常成功发送消息
if (this.socketTask) {
this.socketTask.send({
data: JSON.stringify(data),
async success() {
console.log("消息发送成功");
// console.log("消息发送成功");
},
});
}
}
//开启心跳检测
start(data) {
console.log('开启心跳检测', data)
// console.log('开启心跳检测', data)
this.heartbeatInterval = setInterval(() => {
this.send({
data: '心跳检测',