190 lines
4.6 KiB
JavaScript
190 lines
4.6 KiB
JavaScript
class WebsocketUtil {
|
||
constructor(url, time) {
|
||
this.is_open_socket = false; // 避免重复连接
|
||
this.url = url; // 地址
|
||
this.data = null;
|
||
// 心跳检测
|
||
this.timeout = time; // 多少秒执行检测
|
||
this.heartbeatInterval = null; // 检测服务器端是否还活着
|
||
this.reconnectInterval = null; // 重连之后多久再次重连
|
||
this.messageCallbacks = []; // 存储消息回调函数
|
||
this.store = null; // 存储Vuex store引用
|
||
|
||
try {
|
||
return this.connectSocketInit();
|
||
} catch (e) {
|
||
this.is_open_socket = false;
|
||
}
|
||
}
|
||
|
||
// 设置store
|
||
setStore(store) {
|
||
this.store = store;
|
||
}
|
||
|
||
// 创建WebSocket连接
|
||
connectSocketInit() {
|
||
if (this.is_open_socket) return;
|
||
|
||
this.socketTask = uni.connectSocket({
|
||
url: this.url,
|
||
success: () => {
|
||
this.is_open_socket = true;
|
||
return this.socketTask;
|
||
},
|
||
});
|
||
|
||
this.socketTask.onOpen((res) => {
|
||
clearInterval(this.reconnectInterval);
|
||
clearInterval(this.heartbeatInterval);
|
||
this.is_open_socket = true;
|
||
this.start();
|
||
|
||
this.socketTask.onMessage((res) => {
|
||
try {
|
||
const data = JSON.parse(res.data);
|
||
// 如果有store,将数据存入store
|
||
if (this.store) {
|
||
this.store.commit("setWebSocketData", data);
|
||
}
|
||
// 执行所有注册的回调函数
|
||
this.messageCallbacks.forEach((callback) => callback(data));
|
||
} catch (e) {
|
||
console.error("WebSocket消息解析错误:", e);
|
||
}
|
||
});
|
||
});
|
||
|
||
this.socketTask.onError((res) => {
|
||
this.is_open_socket = false;
|
||
if (this.socketTask) {
|
||
this.socketTask.close();
|
||
}
|
||
});
|
||
|
||
this.socketTask.onClose(() => {
|
||
this.is_open_socket = false;
|
||
});
|
||
}
|
||
|
||
// 发送消息
|
||
send(value) {
|
||
if (!this.is_open_socket) return;
|
||
|
||
this.socketTask.send({
|
||
data: JSON.stringify(value),
|
||
success() {
|
||
console.log("消息发送成功", value);
|
||
},
|
||
fail(err) {
|
||
console.error("消息发送失败", err);
|
||
},
|
||
});
|
||
}
|
||
|
||
// 关闭连接
|
||
close() {
|
||
clearInterval(this.heartbeatInterval);
|
||
clearInterval(this.reconnectInterval);
|
||
this.is_open_socket = false;
|
||
if (this.socketTask) {
|
||
this.socketTask.close();
|
||
}
|
||
}
|
||
|
||
// 开启心跳检测
|
||
start() {
|
||
// this.heartbeatInterval = setInterval(() => {
|
||
// const heartbeatMsg = {
|
||
// type: 'heartbeat',
|
||
// userId: uni.getStorageSync('userinfo')?.user_id
|
||
// }
|
||
// this.send(heartbeatMsg)
|
||
// }, this.timeout)
|
||
}
|
||
|
||
// 重新连接
|
||
reconnect() {
|
||
clearInterval(this.heartbeatInterval);
|
||
if (!this.is_open_socket) {
|
||
this.reconnectInterval = setInterval(() => {
|
||
this.connectSocketInit();
|
||
}, 3000);
|
||
}
|
||
}
|
||
|
||
// 注册消息回调
|
||
onMessage(callback) {
|
||
this.messageCallbacks.push(callback);
|
||
}
|
||
|
||
// 移除消息回调
|
||
offMessage(callback) {
|
||
this.messageCallbacks = this.messageCallbacks.filter(
|
||
(cb) => cb !== callback
|
||
);
|
||
}
|
||
}
|
||
|
||
class WebSocketManager {
|
||
constructor() {
|
||
this.connections = {}; // 存储所有WebSocket连接
|
||
this.globalConnections = []; // 存储需要全局管理的连接名称
|
||
}
|
||
|
||
// 创建全局连接
|
||
createGlobalConnection(name, url, time, store) {
|
||
if (!this.connections[name]) {
|
||
this.connections[name] = new WebsocketUtil(url, time);
|
||
if (store) this.connections[name].setStore(store);
|
||
this.globalConnections.push(name);
|
||
}
|
||
return this.connections[name];
|
||
}
|
||
|
||
// 创建页面级连接
|
||
createPageConnection(name, url, time) {
|
||
if (!this.connections[name]) {
|
||
this.connections[name] = new WebsocketUtil(url, time);
|
||
}
|
||
return this.connections[name];
|
||
}
|
||
|
||
// 获取连接
|
||
getConnection(name) {
|
||
return this.connections[name];
|
||
}
|
||
|
||
// 关闭指定连接
|
||
closeConnection(name) {
|
||
console.log("关闭指定连接", name);
|
||
if (this.connections[name]) {
|
||
this.connections[name].close();
|
||
delete this.connections[name];
|
||
|
||
// 从全局连接列表中移除
|
||
this.globalConnections = this.globalConnections.filter((n) => n !== name);
|
||
}
|
||
}
|
||
|
||
// 关闭所有全局连接
|
||
closeAllGlobalConnections() {
|
||
this.globalConnections.forEach((name) => {
|
||
this.closeConnection(name);
|
||
});
|
||
}
|
||
|
||
// 关闭所有连接
|
||
closeAllConnections() {
|
||
Object.keys(this.connections).forEach((name) => {
|
||
this.connections[name].close();
|
||
});
|
||
this.connections = {};
|
||
this.globalConnections = [];
|
||
}
|
||
}
|
||
|
||
// 导出单例实例
|
||
export const webSocketManager = new WebSocketManager();
|
||
export default WebsocketUtil;
|