2024-07-12 09:43:08 +00:00
|
|
|
|
import { onBeforeUnmount, reactive, ref } from 'vue';
|
2024-06-12 09:05:14 +00:00
|
|
|
|
import { baseUrl, websocketPath } from '@/sheep/config';
|
2024-07-11 09:19:21 +00:00
|
|
|
|
import { copyValueToTarget } from '@/sheep/util';
|
2024-10-02 06:19:23 +00:00
|
|
|
|
import { getRefreshToken } from '@/sheep/request';
|
2024-07-11 09:19:21 +00:00
|
|
|
|
|
2024-07-12 09:43:08 +00:00
|
|
|
|
/**
|
|
|
|
|
* WebSocket 创建 hook
|
|
|
|
|
* @param opt 连接配置
|
|
|
|
|
* @return {{options: *}}
|
|
|
|
|
*/
|
2024-07-11 09:19:21 +00:00
|
|
|
|
export function useWebSocket(opt) {
|
|
|
|
|
const options = reactive({
|
2024-10-02 06:19:23 +00:00
|
|
|
|
url: (baseUrl + websocketPath).replace('http', 'ws') + '?token=' + getRefreshToken(), // ws 地址
|
2024-07-11 09:19:21 +00:00
|
|
|
|
isReconnecting: false, // 正在重新连接
|
|
|
|
|
reconnectInterval: 3000, // 重连间隔,单位毫秒
|
|
|
|
|
heartBeatInterval: 5000, // 心跳间隔,单位毫秒
|
|
|
|
|
pingTimeoutDuration: 1000, // 超过这个时间,后端没有返回pong,则判定后端断线了。
|
|
|
|
|
heartBeatTimer: null, // 心跳计时器
|
|
|
|
|
destroy: false, // 是否销毁
|
2024-07-12 09:43:08 +00:00
|
|
|
|
pingTimeout: null, // 心跳检测定时器
|
|
|
|
|
reconnectTimeout: null, // 重连定时器ID的属性
|
2024-10-02 06:19:23 +00:00
|
|
|
|
onConnected: () => {}, // 连接成功时触发
|
|
|
|
|
onClosed: () => {}, // 连接关闭时触发
|
|
|
|
|
onMessage: (data) => {}, // 收到消息
|
2024-06-12 09:05:14 +00:00
|
|
|
|
});
|
2024-07-12 09:43:08 +00:00
|
|
|
|
const SocketTask = ref(null); // SocketTask 由 uni.connectSocket() 接口创建
|
2024-06-12 09:05:14 +00:00
|
|
|
|
|
2024-07-11 09:19:21 +00:00
|
|
|
|
const initEventListeners = () => {
|
2024-07-12 09:43:08 +00:00
|
|
|
|
// 监听 WebSocket 连接打开事件
|
|
|
|
|
SocketTask.value.onOpen(() => {
|
|
|
|
|
console.log('WebSocket 连接成功');
|
|
|
|
|
// 连接成功时触发
|
2024-07-11 09:19:21 +00:00
|
|
|
|
options.onConnected();
|
2024-07-12 09:43:08 +00:00
|
|
|
|
// 开启心跳检查
|
2024-07-11 09:19:21 +00:00
|
|
|
|
startHeartBeat();
|
2024-06-12 09:05:14 +00:00
|
|
|
|
});
|
2024-07-12 09:43:08 +00:00
|
|
|
|
// 监听 WebSocket 接受到服务器的消息事件
|
|
|
|
|
SocketTask.value.onMessage((res) => {
|
2024-07-11 09:19:21 +00:00
|
|
|
|
try {
|
2024-07-12 09:43:08 +00:00
|
|
|
|
if (res.data === 'pong') {
|
|
|
|
|
// 收到心跳重置心跳超时检查
|
|
|
|
|
resetPingTimeout();
|
2024-07-11 09:19:21 +00:00
|
|
|
|
} else {
|
2024-07-12 09:43:08 +00:00
|
|
|
|
options.onMessage(JSON.parse(res.data));
|
2024-07-11 09:19:21 +00:00
|
|
|
|
}
|
2024-07-12 09:43:08 +00:00
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error(error);
|
2024-07-11 09:19:21 +00:00
|
|
|
|
}
|
2024-06-12 09:05:14 +00:00
|
|
|
|
});
|
2024-07-12 09:43:08 +00:00
|
|
|
|
// 监听 WebSocket 连接关闭事件
|
|
|
|
|
SocketTask.value.onClose((event) => {
|
|
|
|
|
// 情况一:实例销毁
|
2024-07-11 09:19:21 +00:00
|
|
|
|
if (options.destroy) {
|
|
|
|
|
options.onClosed();
|
2024-10-02 06:19:23 +00:00
|
|
|
|
} else {
|
|
|
|
|
// 情况二:连接失败重连
|
2024-07-12 09:43:08 +00:00
|
|
|
|
// 停止心跳检查
|
|
|
|
|
stopHeartBeat();
|
|
|
|
|
// 重连
|
2024-07-11 09:19:21 +00:00
|
|
|
|
reconnect();
|
|
|
|
|
}
|
2024-06-12 09:05:14 +00:00
|
|
|
|
});
|
|
|
|
|
};
|
2024-07-11 09:19:21 +00:00
|
|
|
|
|
2024-07-12 09:43:08 +00:00
|
|
|
|
// 发送消息
|
2024-07-11 09:19:21 +00:00
|
|
|
|
const sendMessage = (message) => {
|
2024-07-12 09:43:08 +00:00
|
|
|
|
if (SocketTask.value && !options.destroy) {
|
|
|
|
|
SocketTask.value.send({ data: message });
|
2024-07-11 09:19:21 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
2024-07-12 09:43:08 +00:00
|
|
|
|
// 开始心跳检查
|
2024-07-11 09:19:21 +00:00
|
|
|
|
const startHeartBeat = () => {
|
|
|
|
|
options.heartBeatTimer = setInterval(() => {
|
2024-07-12 09:43:08 +00:00
|
|
|
|
sendMessage('ping');
|
2024-07-11 09:19:21 +00:00
|
|
|
|
options.pingTimeout = setTimeout(() => {
|
2024-07-12 09:43:08 +00:00
|
|
|
|
// 如果在超时时间内没有收到 pong,则认为连接断开
|
2024-07-11 09:19:21 +00:00
|
|
|
|
reconnect();
|
|
|
|
|
}, options.pingTimeoutDuration);
|
|
|
|
|
}, options.heartBeatInterval);
|
|
|
|
|
};
|
2024-07-12 09:43:08 +00:00
|
|
|
|
// 停止心跳检查
|
2024-07-11 09:19:21 +00:00
|
|
|
|
const stopHeartBeat = () => {
|
2024-07-12 09:43:08 +00:00
|
|
|
|
clearInterval(options.heartBeatTimer);
|
|
|
|
|
resetPingTimeout();
|
2024-06-12 09:05:14 +00:00
|
|
|
|
};
|
2024-07-11 09:19:21 +00:00
|
|
|
|
|
2024-07-12 09:43:08 +00:00
|
|
|
|
// WebSocket 重连
|
2024-07-11 09:19:21 +00:00
|
|
|
|
const reconnect = () => {
|
2024-07-12 09:43:08 +00:00
|
|
|
|
if (options.destroy || !SocketTask.value) {
|
|
|
|
|
// 如果WebSocket已被销毁或尚未完全关闭,不进行重连
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 重连中
|
2024-07-11 09:19:21 +00:00
|
|
|
|
options.isReconnecting = true;
|
2024-07-12 09:43:08 +00:00
|
|
|
|
|
|
|
|
|
// 清除现有的重连标志,以避免多次重连
|
|
|
|
|
if (options.reconnectTimeout) {
|
|
|
|
|
clearTimeout(options.reconnectTimeout);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 设置重连延迟
|
|
|
|
|
options.reconnectTimeout = setTimeout(() => {
|
|
|
|
|
// 检查组件是否仍在运行和WebSocket是否关闭
|
|
|
|
|
if (!options.destroy) {
|
|
|
|
|
// 重置重连标志
|
|
|
|
|
options.isReconnecting = false;
|
|
|
|
|
// 初始化新的WebSocket连接
|
|
|
|
|
initSocket();
|
|
|
|
|
}
|
2024-07-11 09:19:21 +00:00
|
|
|
|
}, options.reconnectInterval);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const resetPingTimeout = () => {
|
2024-07-12 09:43:08 +00:00
|
|
|
|
if (options.pingTimeout) {
|
|
|
|
|
clearTimeout(options.pingTimeout);
|
|
|
|
|
options.pingTimeout = null; // 清除超时ID
|
|
|
|
|
}
|
2024-07-11 09:19:21 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const close = () => {
|
|
|
|
|
options.destroy = true;
|
|
|
|
|
stopHeartBeat();
|
2024-07-12 09:43:08 +00:00
|
|
|
|
if (options.reconnectTimeout) {
|
|
|
|
|
clearTimeout(options.reconnectTimeout);
|
|
|
|
|
}
|
|
|
|
|
if (SocketTask.value) {
|
|
|
|
|
SocketTask.value.close();
|
|
|
|
|
SocketTask.value = null;
|
2024-07-11 09:19:21 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const initSocket = () => {
|
2024-07-12 09:43:08 +00:00
|
|
|
|
options.destroy = false;
|
2024-07-11 09:19:21 +00:00
|
|
|
|
copyValueToTarget(options, opt);
|
2024-07-12 09:43:08 +00:00
|
|
|
|
SocketTask.value = uni.connectSocket({
|
2024-07-11 09:19:21 +00:00
|
|
|
|
url: options.url,
|
2024-10-02 06:19:23 +00:00
|
|
|
|
complete: () => {},
|
|
|
|
|
success: () => {},
|
2024-07-11 09:19:21 +00:00
|
|
|
|
});
|
|
|
|
|
initEventListeners();
|
|
|
|
|
};
|
2024-07-12 09:43:08 +00:00
|
|
|
|
|
2024-07-11 09:19:21 +00:00
|
|
|
|
initSocket();
|
|
|
|
|
|
2024-07-12 09:43:08 +00:00
|
|
|
|
onBeforeUnmount(() => {
|
|
|
|
|
close();
|
|
|
|
|
});
|
|
|
|
|
return { options };
|
2024-06-12 09:05:14 +00:00
|
|
|
|
}
|