// @/utils/websocket.js
import { isJSON } from "@/utils/json.js"
class WebSocketClass {
constructor(url) {
this.lockReconnect = false; // 是否开始重连
this.wsUrl = "ws://82.157.123.54:9010"; // ws 地址
this.globalCallback = null; // 回调方法
this.userClose = false; // 是否主动关闭
this.createWebSocket(url);
}
createWebSocket(url) {
// #ifdef H5
if (typeof (WebSocket) === 'undefined') {
this.writeToScreen("您的浏览器不支持WebSocket,无法获取数据");
return false
}
// #endif
// #ifdef APP-PLUS
if (typeof (uni.connectSocket) === 'undefined') {
this.writeToScreen("您的浏览器不支持WebSocket,无法获取数据");
return false
}
// #endif
this.wsUrl = url;
try {
// 创建一个this.ws对象【发送、接收、关闭socket都由这个对象操作】
// #ifdef H5
this.ws = new WebSocket(this.wsUrl);
this.initEventHandle();
// #endif
// #ifdef APP-PLUS
let that = this
this.ws = uni.connectSocket({
url: this.wsUrl,
success(data) {
console.log("websocket连接成功");
that.initEventHandle();
},
});
// #endif
} catch (e) {
this.reconnect(url);
}
}
// 初始化
initEventHandle() {
/**
* 监听WebSocket连接打开成功
*/
// #ifdef H5
this.ws.onopen = (event) => {
console.log("WebSocket连接打开");
};
// #endif
// #ifdef APP-PLUS
this.ws.onOpen(res => {
console.log('WebSocket连接打开');
});
// #endif
/**
* 连接关闭后的回调函数
*/
// #ifdef H5
this.ws.onclose = (event) => {
if (!this.userClose) {
this.reconnect(this.wsUrl); //重连
}
};
// #endif
// #ifdef APP-PLUS
this.ws.onClose(() => {
if (!this.userClose) {
this.reconnect(this.wsUrl); //重连
}
});
// #endif
/**
* 报错时的回调函数
*/
// #ifdef H5
this.ws.onerror = (event) => {
if (!this.userClose) {
this.reconnect(this.wsUrl); //重连
}
};
// #endif
// #ifdef APP-PLUS
this.ws.onError(() => {
if (!this.userClose) {
this.reconnect(this.wsUrl); //重连
}
});
// #endif
/**
* 收到服务器数据后的回调函数
*/
// #ifdef H5
this.ws.onmessage = (event) => {
if(isJSON(event.data)) {
const jsonobject = JSON.parse(event.data)
this.globalCallback(jsonobject)
}else {
this.globalCallback(event.data)
}
};
// #endif
// #ifdef APP-PLUS
this.ws.onMessage(event => {
if(isJSON(event.data)) {
const jsonobject = JSON.parse(event.data)
this.globalCallback(jsonobject)
}else {
this.globalCallback(event.data)
}
});
// #endif
}
// 关闭ws连接回调
reconnect(url) {
if (this.lockReconnect) return;
this.ws.close();
this.lockReconnect = true; // 关闭重连,没连接上会一直重连,设置延迟避免请求过多
setTimeout(() => {
this.createWebSocket(url);
this.lockReconnect = false;
}, 1000);
}
// 发送信息方法
webSocketSendMsg(msg) {
this.ws && this.ws.send({
data: msg,
success() {
console.log("消息发送成功");
},
fail(err) {
console.log("关闭失败", err)
}
});
}
// 获取ws返回的数据方法
getWebSocketMsg(callback) {
this.globalCallback = callback
}
// 关闭ws方法
closeSocket() {
if (this.ws) {
this.userClose = true;
this.ws.close({
success(res) {
console.log("关闭成功", res)
},
fail(err) {
console.log("关闭失败", err)
}
});
}
}
writeToScreen(massage) {
console.log(massage);
}
}
export default WebSocketClass;
uniapp websocket 里面有三种 可以试试
5星 · 超过95%的资源 需积分: 18 63 浏览量
2022-09-22
19:02:06
上传
评论 1
收藏 7KB ZIP 举报
呱嗨喵
- 粉丝: 11
- 资源: 18
最新资源
- 2023-04-06-项目笔记 - 第一百零七阶段 - 4.4.2.105全局变量的作用域-105 -2024.04.18
- 新笔记 2023-10-31 16-07-02 go4 .pdf
- 2211502233 陈钧维 实验3.topo
- 第1章 计算机系统基础知识 1.xmind
- 比例-积分-谐振(PIR)详解
- 斐讯 k2p 集客ap 7.2 版本固件 K4
- 一阶RC低通滤波器详解
- Screenshot_20240418_225414_com.tencent.mm.jpg
- 基于JAVA毕业设计-JAVA图书管理系统毕业设计(源代码+论文).rar
- 基于JAVA毕业设计-JAVA图书馆书库管理系统设计(论文+源代码).rar
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
评论10