Files
shoot-miniprograms/src/websocket.js

139 lines
3.3 KiB
JavaScript
Raw Normal View History

2025-07-03 21:13:55 +08:00
import { MESSAGETYPES } from "@/constants";
2025-05-28 23:49:17 +08:00
let socket = null;
2025-05-29 23:45:44 +08:00
let heartbeatInterval = null;
2025-06-18 00:35:16 +08:00
let reconnectCount = 0;
let reconnectTimer = null;
// 重连配置
const RECONNECT_CONFIG = {
2025-06-24 13:18:03 +08:00
MAX_COUNT: 999, // 最大重连次数
2025-06-18 00:35:16 +08:00
INITIAL_DELAY: 2000, // 初始重连延迟2秒
2025-06-24 13:18:03 +08:00
MAX_DELAY: 5000, // 最大重连延迟5秒
2025-06-18 00:35:16 +08:00
};
2025-05-28 23:49:17 +08:00
/**
2025-05-29 23:45:44 +08:00
* 建立 WebSocket 连接
2025-05-28 23:49:17 +08:00
*/
function createWebSocket(token, onMessage) {
2025-06-14 22:45:16 +08:00
const url = `wss://api.shelingxingqiu.com/socket?authorization=${token}`;
2025-05-28 23:49:17 +08:00
socket = uni.connectSocket({
2025-06-14 22:45:16 +08:00
url,
2025-06-18 00:35:16 +08:00
success: () => {
console.log("websocket 连接成功");
reconnectCount = 0; // 重置重连次数
// 启动心跳
startHeartbeat();
},
fail: () => {
reconnect(onMessage);
2025-06-18 00:35:16 +08:00
},
2025-05-28 23:49:17 +08:00
});
// 接收消息
uni.onSocketMessage((res) => {
2025-06-19 21:03:33 +08:00
const data = JSON.parse(res.data);
2025-07-05 18:51:06 +08:00
if(data.event === 'pong' || !data.data.updates) return;
2025-06-19 21:03:33 +08:00
if (onMessage) onMessage(data.data.updates);
2025-07-03 21:13:55 +08:00
const msg = data.data.updates[0];
if (msg && msg.constructor === MESSAGETYPES.BackToGame) {
const { battleInfo } = msg;
uni.setStorageSync(`battle-${battleInfo.id}`, battleInfo);
// 约战
if (battleInfo.config.battleMode === 1) {
uni.navigateTo({
url: `/pages/team-match?battleId=${battleInfo.id}`,
});
}
// 排位
if (battleInfo.config.battleMode === 2) {
if (battleInfo.config.mode === 1) {
uni.navigateTo({
url: `/pages/team-match?battleId=${battleInfo.id}`,
});
} else if (battleInfo.config.mode === 2) {
uni.navigateTo({
url: `/pages/melee-match?battleId=${battleInfo.id}`,
});
}
}
}
2025-05-28 23:49:17 +08:00
});
// 错误处理
uni.onSocketError((err) => {
console.error("WebSocket 错误", err);
2025-06-18 00:35:16 +08:00
reconnect(token, onMessage);
2025-05-28 23:49:17 +08:00
});
2025-06-05 17:43:22 +08:00
uni.onSocketClose((result) => {
console.log("WebSocket 已关闭", result);
2025-06-18 00:35:16 +08:00
stopHeartbeat();
reconnect(onMessage);
2025-05-28 23:49:17 +08:00
});
2025-05-29 23:45:44 +08:00
}
2025-06-18 00:35:16 +08:00
/**
* 重连机制
*/
function reconnect(onMessage) {
2025-06-18 00:35:16 +08:00
if (reconnectCount >= RECONNECT_CONFIG.MAX_COUNT) return;
reconnectTimer && clearTimeout(reconnectTimer);
2025-06-22 02:39:03 +08:00
const token = uni.getStorageSync("token");
if (!token) return;
2025-06-18 00:35:16 +08:00
// 计算重连延迟(指数退避)
const delay = Math.min(
RECONNECT_CONFIG.INITIAL_DELAY * Math.pow(2, reconnectCount),
RECONNECT_CONFIG.MAX_DELAY
);
reconnectTimer = setTimeout(() => {
console.log("reconnecting...");
createWebSocket(token, onMessage);
2025-06-18 00:35:16 +08:00
reconnectCount++;
}, delay);
}
2025-05-29 23:45:44 +08:00
function closeWebSocket() {
2025-05-30 13:58:43 +08:00
if (socket) {
2025-06-18 00:35:16 +08:00
// 清理重连定时器
reconnectCount = 0;
2025-06-22 02:39:03 +08:00
reconnectTimer && clearTimeout(reconnectTimer);
stopHeartbeat();
socket.close();
2025-05-30 13:58:43 +08:00
}
2025-05-28 23:49:17 +08:00
}
/**
2025-05-29 23:45:44 +08:00
* 启动心跳
2025-05-28 23:49:17 +08:00
*/
2025-05-29 23:45:44 +08:00
function startHeartbeat() {
stopHeartbeat(); // 防止重复启动
heartbeatInterval = setInterval(() => {
2025-07-05 18:51:06 +08:00
if (socket) {
2025-05-29 23:45:44 +08:00
uni.sendSocketMessage({
2025-07-05 18:51:06 +08:00
data: JSON.stringify({ event: "ping", data: {} }),
2025-05-29 23:45:44 +08:00
fail: (err) => {
console.error("发送心跳失败", err);
},
});
}
2025-07-05 18:51:06 +08:00
}, 10000);
2025-05-28 23:49:17 +08:00
}
/**
2025-05-29 23:45:44 +08:00
* 停止心跳
2025-05-28 23:49:17 +08:00
*/
2025-05-29 23:45:44 +08:00
function stopHeartbeat() {
if (heartbeatInterval) {
clearInterval(heartbeatInterval);
heartbeatInterval = null;
2025-05-28 23:49:17 +08:00
}
}
export default {
2025-05-29 23:45:44 +08:00
createWebSocket,
2025-05-28 23:49:17 +08:00
closeWebSocket,
};