Compare commits
1 Commits
60a0740c31
...
new-race-m
| Author | SHA1 | Date | |
|---|---|---|---|
| 8c0a24fd44 |
23
src/App.vue
23
src/App.vue
@@ -21,10 +21,7 @@
|
|||||||
} = storeToRefs(store);
|
} = storeToRefs(store);
|
||||||
const {
|
const {
|
||||||
updateUser,
|
updateUser,
|
||||||
updateOnline,
|
updateOnline
|
||||||
updateDevice,
|
|
||||||
updateGame,
|
|
||||||
updateRoomNumber
|
|
||||||
} = store;
|
} = store;
|
||||||
|
|
||||||
watch(
|
watch(
|
||||||
@@ -49,22 +46,6 @@
|
|||||||
updateUser(value);
|
updateUser(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
function onSessionKickedOut() {
|
|
||||||
uni.removeStorageSync(
|
|
||||||
`${uni.getAccountInfoSync().miniProgram.envVersion}_token`
|
|
||||||
);
|
|
||||||
updateUser();
|
|
||||||
updateDevice("", "");
|
|
||||||
updateOnline(false);
|
|
||||||
updateGame(false, "");
|
|
||||||
updateRoomNumber("");
|
|
||||||
uni.showModal({
|
|
||||||
title: "提示",
|
|
||||||
content: "账号已在其他设备登录",
|
|
||||||
showCancel: false,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
async function emitUpdateOnline() {
|
async function emitUpdateOnline() {
|
||||||
const data = await getDeviceBatteryAPI();
|
const data = await getDeviceBatteryAPI();
|
||||||
updateOnline(data.online);
|
updateOnline(data.online);
|
||||||
@@ -84,7 +65,6 @@
|
|||||||
onShow(() => {
|
onShow(() => {
|
||||||
uni.$on("update-user", emitUpdateUser);
|
uni.$on("update-user", emitUpdateUser);
|
||||||
uni.$on("update-online", emitUpdateOnline);
|
uni.$on("update-online", emitUpdateOnline);
|
||||||
uni.$on("session-kicked-out", onSessionKickedOut);
|
|
||||||
const token = uni.getStorageSync(
|
const token = uni.getStorageSync(
|
||||||
`${uni.getAccountInfoSync().miniProgram.envVersion}_token`
|
`${uni.getAccountInfoSync().miniProgram.envVersion}_token`
|
||||||
);
|
);
|
||||||
@@ -97,7 +77,6 @@
|
|||||||
onHide(() => {
|
onHide(() => {
|
||||||
uni.$off("update-user", emitUpdateUser);
|
uni.$off("update-user", emitUpdateUser);
|
||||||
uni.$off("update-online", emitUpdateOnline);
|
uni.$off("update-online", emitUpdateOnline);
|
||||||
uni.$off("session-kicked-out", onSessionKickedOut);
|
|
||||||
websocket.closeWebSocket();
|
websocket.closeWebSocket();
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
@@ -46,8 +46,6 @@ function request(method, url, data = {}) {
|
|||||||
`${uni.getAccountInfoSync().miniProgram.envVersion}_token`
|
`${uni.getAccountInfoSync().miniProgram.envVersion}_token`
|
||||||
);
|
);
|
||||||
uni.$emit("update-user");
|
uni.$emit("update-user");
|
||||||
reject({ type: "AUTH_INVALID", message });
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
if (message === "ROOM_FULL") {
|
if (message === "ROOM_FULL") {
|
||||||
resolve({full: true});
|
resolve({full: true});
|
||||||
|
|||||||
@@ -68,29 +68,28 @@ onShow(async () => {
|
|||||||
const token = uni.getStorageSync(`${env}_token`);
|
const token = uni.getStorageSync(`${env}_token`);
|
||||||
|
|
||||||
if (!user.value.id && !token) {
|
if (!user.value.id && !token) {
|
||||||
// showModal.value = true;
|
try {
|
||||||
// try {
|
const wxResult = await uni.login({provider: "weixin"});
|
||||||
// const wxResult = await uni.login({provider: "weixin"});
|
const bindResult = await checkUserBindAPI(wxResult.code);
|
||||||
// const bindResult = await checkUserBindAPI(wxResult.code);
|
if (bindResult.binded) {
|
||||||
// if (bindResult.binded) {
|
const newResult = await uni.login({provider: "weixin"});
|
||||||
// const newResult = await uni.login({provider: "weixin"});
|
const silentResult = await silentLoginAPI(newResult.code);
|
||||||
// const silentResult = await silentLoginAPI(newResult.code);
|
if (silentResult.user) updateUser(silentResult.user);
|
||||||
// if (silentResult.user) updateUser(silentResult.user);
|
const devices = await getMyDevicesAPI();
|
||||||
// const devices = await getMyDevicesAPI();
|
if (devices.bindings && devices.bindings.length) {
|
||||||
// if (devices.bindings && devices.bindings.length) {
|
updateDevice(
|
||||||
// updateDevice(
|
devices.bindings[0].deviceId,
|
||||||
// devices.bindings[0].deviceId,
|
devices.bindings[0].deviceName
|
||||||
// devices.bindings[0].deviceName
|
);
|
||||||
// );
|
const data = await getDeviceBatteryAPI();
|
||||||
// const data = await getDeviceBatteryAPI();
|
updateOnline(data.online);
|
||||||
// updateOnline(data.online);
|
}
|
||||||
// }
|
} else {
|
||||||
// } else {
|
showModal.value = true;
|
||||||
// showModal.value = true;
|
}
|
||||||
// }
|
} catch (e) {
|
||||||
// } catch (e) {
|
console.log("检查绑定状态失败", e);
|
||||||
// console.log("检查绑定状态失败", e);
|
}
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const promises = [getScoreRankList(undefined, 1, 10)];
|
const promises = [getScoreRankList(undefined, 1, 10)];
|
||||||
@@ -476,7 +475,7 @@ onShareTimeline(() => {
|
|||||||
|
|
||||||
.top-theme {
|
.top-theme {
|
||||||
position: absolute;
|
position: absolute;
|
||||||
display: none;
|
display: flex;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
justify-content: center;
|
justify-content: center;
|
||||||
width: 100%;
|
width: 100%;
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
<script setup>
|
<script setup>
|
||||||
import { ref, onMounted, onBeforeUnmount } from "vue";
|
import { ref, onMounted, onBeforeUnmount, nextTick } from "vue";
|
||||||
import { onLoad } from "@dcloudio/uni-app";
|
import { onLoad } from "@dcloudio/uni-app";
|
||||||
import Container from "@/components/Container.vue";
|
import Container from "@/components/Container.vue";
|
||||||
import ShootProgress from "@/components/ShootProgress.vue";
|
import ShootProgress from "@/components/ShootProgress.vue";
|
||||||
@@ -31,6 +31,8 @@ const { user } = storeToRefs(store);
|
|||||||
const start = ref(false);
|
const start = ref(false);
|
||||||
const scores = ref([]);
|
const scores = ref([]);
|
||||||
const total = 12;
|
const total = 12;
|
||||||
|
/** 当前练习中连续 X 环计数,用于触发 tententen 音效 */
|
||||||
|
const xRingStreak = ref(0);
|
||||||
const practiseResult = ref({});
|
const practiseResult = ref({});
|
||||||
const practiseId = ref("");
|
const practiseId = ref("");
|
||||||
const showGuide = ref(false);
|
const showGuide = ref(false);
|
||||||
@@ -46,6 +48,7 @@ onLoad((options) => {
|
|||||||
const onReady = async () => {
|
const onReady = async () => {
|
||||||
await startPractiseAPI();
|
await startPractiseAPI();
|
||||||
scores.value = [];
|
scores.value = [];
|
||||||
|
xRingStreak.value = 0; // 新一局开始,重置 X 环连续计数
|
||||||
start.value = true;
|
start.value = true;
|
||||||
audioManager.play("练习开始");
|
audioManager.play("练习开始");
|
||||||
};
|
};
|
||||||
@@ -55,9 +58,33 @@ const onOver = async () => {
|
|||||||
start.value = false;
|
start.value = false;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检测连续 X 环是否达到 3 箭,达到则播放 tententen 音效
|
||||||
|
* @param {boolean} isXRing - 本次射击是否为 X 环
|
||||||
|
*/
|
||||||
|
function checkAndPlayTententen(isXRing) {
|
||||||
|
if (isXRing) {
|
||||||
|
xRingStreak.value += 1;
|
||||||
|
// 连续 3 箭均为 X 环,在环数播报入队后追加 tententen,避免播放顺序颠倒
|
||||||
|
if (xRingStreak.value >= 3) {
|
||||||
|
xRingStreak.value = 0;
|
||||||
|
nextTick(() => audioManager.play("tententen", false));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// 非 X 环则重置连续计数
|
||||||
|
xRingStreak.value = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async function onReceiveMessage(msg) {
|
async function onReceiveMessage(msg) {
|
||||||
if (msg.type === MESSAGETYPESV2.ShootResult) {
|
if (msg.type === MESSAGETYPESV2.ShootResult) {
|
||||||
|
const prevLen = scores.value.length;
|
||||||
scores.value = msg.details;
|
scores.value = msg.details;
|
||||||
|
// 有新箭时取最后一箭判断是否 X 环并检测连续计数
|
||||||
|
if (scores.value.length > prevLen) {
|
||||||
|
const latestArrow = scores.value[scores.value.length - 1];
|
||||||
|
checkAndPlayTententen(!!(latestArrow?.ringX && latestArrow?.ring));
|
||||||
|
}
|
||||||
} else if (msg.type === MESSAGETYPESV2.BattleEnd) {
|
} else if (msg.type === MESSAGETYPESV2.BattleEnd) {
|
||||||
// setTimeout(onOver, 1500);
|
// setTimeout(onOver, 1500);
|
||||||
}
|
}
|
||||||
@@ -74,6 +101,7 @@ async function onComplete() {
|
|||||||
practiseResult.value = {};
|
practiseResult.value = {};
|
||||||
start.value = false;
|
start.value = false;
|
||||||
scores.value = [];
|
scores.value = [];
|
||||||
|
xRingStreak.value = 0; // 重新开始练习,重置 X 环连续计数
|
||||||
const result = await createPractiseAPI(total, 120);
|
const result = await createPractiseAPI(total, 120);
|
||||||
if (result) practiseId.value = result.id;
|
if (result) practiseId.value = result.id;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
<script setup>
|
<script setup>
|
||||||
import { ref, onMounted, onBeforeUnmount } from "vue";
|
import { ref, onMounted, onBeforeUnmount, nextTick } from "vue";
|
||||||
import Container from "@/components/Container.vue";
|
import Container from "@/components/Container.vue";
|
||||||
import ShootProgress from "@/components/ShootProgress.vue";
|
import ShootProgress from "@/components/ShootProgress.vue";
|
||||||
import BowTarget from "@/components/BowTarget.vue";
|
import BowTarget from "@/components/BowTarget.vue";
|
||||||
@@ -31,6 +31,8 @@ const { user } = storeToRefs(store);
|
|||||||
const start = ref(false);
|
const start = ref(false);
|
||||||
const scores = ref([]);
|
const scores = ref([]);
|
||||||
const total = 36;
|
const total = 36;
|
||||||
|
/** 当前练习中连续 X 环计数,用于触发 tententen 音效 */
|
||||||
|
const xRingStreak = ref(0);
|
||||||
const practiseResult = ref({});
|
const practiseResult = ref({});
|
||||||
const practiseId = ref("");
|
const practiseId = ref("");
|
||||||
const showGuide = ref(false);
|
const showGuide = ref(false);
|
||||||
@@ -45,6 +47,7 @@ onLoad((options) => {
|
|||||||
const onReady = async () => {
|
const onReady = async () => {
|
||||||
await startPractiseAPI();
|
await startPractiseAPI();
|
||||||
scores.value = [];
|
scores.value = [];
|
||||||
|
xRingStreak.value = 0; // 新一局开始,重置 X 环连续计数
|
||||||
start.value = true;
|
start.value = true;
|
||||||
audioManager.play("练习开始");
|
audioManager.play("练习开始");
|
||||||
};
|
};
|
||||||
@@ -54,9 +57,33 @@ const onOver = async () => {
|
|||||||
start.value = false;
|
start.value = false;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检测连续 X 环是否达到 3 箭,达到则播放 tententen 音效
|
||||||
|
* @param {boolean} isXRing - 本次射击是否为 X 环
|
||||||
|
*/
|
||||||
|
function checkAndPlayTententen(isXRing) {
|
||||||
|
if (isXRing) {
|
||||||
|
xRingStreak.value += 1;
|
||||||
|
// 连续 3 箭均为 X 环,在环数播报入队后追加 tententen,避免播放顺序颠倒
|
||||||
|
if (xRingStreak.value >= 3) {
|
||||||
|
xRingStreak.value = 0;
|
||||||
|
nextTick(() => audioManager.play("tententen", false));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// 非 X 环则重置连续计数
|
||||||
|
xRingStreak.value = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async function onReceiveMessage(msg) {
|
async function onReceiveMessage(msg) {
|
||||||
if (msg.type === MESSAGETYPESV2.ShootResult) {
|
if (msg.type === MESSAGETYPESV2.ShootResult) {
|
||||||
|
const prevLen = scores.value.length;
|
||||||
scores.value = msg.details;
|
scores.value = msg.details;
|
||||||
|
// 有新箭时取最后一箭判断是否 X 环并检测连续计数
|
||||||
|
if (scores.value.length > prevLen) {
|
||||||
|
const latestArrow = scores.value[scores.value.length - 1];
|
||||||
|
checkAndPlayTententen(!!(latestArrow?.ringX && latestArrow?.ring));
|
||||||
|
}
|
||||||
} else if (msg.type === MESSAGETYPESV2.BattleEnd) {
|
} else if (msg.type === MESSAGETYPESV2.BattleEnd) {
|
||||||
setTimeout(onOver, 1500);
|
setTimeout(onOver, 1500);
|
||||||
}
|
}
|
||||||
@@ -89,7 +116,8 @@ async function onComplete() {
|
|||||||
practiseResult.value = {};
|
practiseResult.value = {};
|
||||||
start.value = false;
|
start.value = false;
|
||||||
scores.value = [];
|
scores.value = [];
|
||||||
const result = await createPractiseAPI(total, 3600);
|
xRingStreak.value = 0; // 重新开始练习,重置 X 环连续计数
|
||||||
|
const result = await createPractiseAPI(total, 360);
|
||||||
if (result) practiseId.value = result.id;
|
if (result) practiseId.value = result.id;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -105,7 +133,7 @@ onMounted(async () => {
|
|||||||
});
|
});
|
||||||
uni.$on("socket-inbox", onReceiveMessage);
|
uni.$on("socket-inbox", onReceiveMessage);
|
||||||
uni.$on("share-image", onClickShare);
|
uni.$on("share-image", onClickShare);
|
||||||
const result = await createPractiseAPI(total, 3600, targetType.value);
|
const result = await createPractiseAPI(total, 360, targetType.value);
|
||||||
if (result) practiseId.value = result.id;
|
if (result) practiseId.value = result.id;
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -132,7 +160,7 @@ onBeforeUnmount(() => {
|
|||||||
<ShootProgress
|
<ShootProgress
|
||||||
:tips="`请连续射${total}支箭`"
|
:tips="`请连续射${total}支箭`"
|
||||||
:start="start"
|
:start="start"
|
||||||
:total="3600"
|
:total="360"
|
||||||
:onStop="onOver"
|
:onStop="onOver"
|
||||||
/>
|
/>
|
||||||
<view class="user-row">
|
<view class="user-row">
|
||||||
|
|||||||
@@ -323,7 +323,6 @@ onShow(async () => {
|
|||||||
>
|
>
|
||||||
<text>{{ seasonName }}</text>
|
<text>{{ seasonName }}</text>
|
||||||
<image
|
<image
|
||||||
class="triangle-icon"
|
|
||||||
v-show="seasonData.length > 1"
|
v-show="seasonData.length > 1"
|
||||||
src="../static/rank/triangle.png"
|
src="../static/rank/triangle.png"
|
||||||
mode="widthFix"
|
mode="widthFix"
|
||||||
@@ -593,7 +592,6 @@ onShow(async () => {
|
|||||||
.ranking-season > image {
|
.ranking-season > image {
|
||||||
width: 12px;
|
width: 12px;
|
||||||
height: 12px;
|
height: 12px;
|
||||||
transform: rotateX(180deg);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
.ranking-season > text {
|
.ranking-season > text {
|
||||||
@@ -809,18 +807,18 @@ onShow(async () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
.season-list {
|
.season-list {
|
||||||
background-color: rgba(0, 0, 0, 0.8);
|
background-color: #000c;
|
||||||
border-radius: 15px;
|
border-radius: 15px;
|
||||||
color: #fff;
|
color: #fff;
|
||||||
font-size: 12px;
|
font-size: 12px;
|
||||||
padding: 12px 0;
|
padding: 5px 0;
|
||||||
position: absolute;
|
position: absolute;
|
||||||
width: 220rpx;
|
width: 220rpx;
|
||||||
height: auto;
|
height: auto;
|
||||||
max-height: 400rpx;
|
max-height: 400rpx;
|
||||||
overflow: hidden;
|
overflow: hidden;
|
||||||
overflow-y: auto;
|
overflow-y: auto;
|
||||||
top: -42rpx;
|
top: -44rpx;
|
||||||
right: -30rpx;
|
right: -30rpx;
|
||||||
letter-spacing: 2px;
|
letter-spacing: 2px;
|
||||||
z-index: 10;
|
z-index: 10;
|
||||||
@@ -831,7 +829,7 @@ onShow(async () => {
|
|||||||
display: flex;
|
display: flex;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
word-break: keep-all;
|
word-break: keep-all;
|
||||||
padding: 20rpx 10rpx;
|
padding: 20rpx 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
.season-list > view > text {
|
.season-list > view > text {
|
||||||
|
|||||||
156
src/websocket.js
156
src/websocket.js
@@ -1,22 +1,12 @@
|
|||||||
import { MESSAGETYPES, getMessageTypeName } from "@/constants";
|
import { MESSAGETYPES, getMessageTypeName } from "@/constants";
|
||||||
import { getUserGameState } from "@/apis";
|
|
||||||
|
|
||||||
let socket = null;
|
let socket = null;
|
||||||
let heartbeatInterval = null;
|
let heartbeatInterval = null;
|
||||||
let reconnectTimer = null;
|
let reconnectTimer = null;
|
||||||
let manualClose = false;
|
|
||||||
let checkingSession = false;
|
|
||||||
let kickedOut = false;
|
|
||||||
let isConnecting = false;
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 建立 WebSocket 连接
|
||||||
|
*/
|
||||||
function createWebSocket(token, onMessage) {
|
function createWebSocket(token, onMessage) {
|
||||||
if (!token) return;
|
|
||||||
if (kickedOut) kickedOut = false;
|
|
||||||
if (socket || isConnecting) return;
|
|
||||||
|
|
||||||
manualClose = false;
|
|
||||||
isConnecting = true;
|
|
||||||
|
|
||||||
let url = "wss://api.shelingxingqiu.com/socket";
|
let url = "wss://api.shelingxingqiu.com/socket";
|
||||||
try {
|
try {
|
||||||
const accountInfo = uni.getAccountInfoSync();
|
const accountInfo = uni.getAccountInfoSync();
|
||||||
@@ -30,65 +20,42 @@ function createWebSocket(token, onMessage) {
|
|||||||
case "trial": // 体验版
|
case "trial": // 体验版
|
||||||
url = "wss://apitest.shelingxingqiu.com/socket";
|
url = "wss://apitest.shelingxingqiu.com/socket";
|
||||||
break;
|
break;
|
||||||
case "trial":
|
case "release": // 正式版
|
||||||
url = "wss://apitest.shelingxingqiu.com/socket";
|
|
||||||
break;
|
|
||||||
case "release":
|
|
||||||
url = "wss://api.shelingxingqiu.com/socket";
|
url = "wss://api.shelingxingqiu.com/socket";
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
|
// 保持默认值
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error("获取 WebSocket 环境信息失败,使用默认正式环境", e);
|
console.error("获取环境信息失败,使用默认正式环境", e);
|
||||||
}
|
}
|
||||||
|
|
||||||
url += `?authorization=${token}`;
|
url += `?authorization=${token}`;
|
||||||
const socketTask = uni.connectSocket({
|
socket = uni.connectSocket({
|
||||||
url,
|
url,
|
||||||
success: () => {
|
success: () => {
|
||||||
console.log("WebSocket 已发起连接");
|
console.log("websocket 连接成功");
|
||||||
|
// 启动心跳
|
||||||
|
startHeartbeat(onMessage);
|
||||||
},
|
},
|
||||||
fail: (err) => {
|
fail: () => {
|
||||||
if (socket !== socketTask) return;
|
|
||||||
console.error("WebSocket 连接失败", err);
|
|
||||||
socket = null;
|
|
||||||
isConnecting = false;
|
|
||||||
reconnect(onMessage);
|
reconnect(onMessage);
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
socket = socketTask;
|
// 接收消息
|
||||||
|
uni.onSocketMessage((res) => {
|
||||||
socketTask.onOpen(() => {
|
|
||||||
if (socket !== socketTask) return;
|
|
||||||
console.log("WebSocket 连接成功");
|
|
||||||
isConnecting = false;
|
|
||||||
startHeartbeat(onMessage);
|
|
||||||
});
|
|
||||||
|
|
||||||
socketTask.onMessage((res) => {
|
|
||||||
if (socket !== socketTask) return;
|
|
||||||
|
|
||||||
const { data, event } = JSON.parse(res.data);
|
const { data, event } = JSON.parse(res.data);
|
||||||
if (event === "pong") return;
|
if (event === "pong") return;
|
||||||
if (data.type) {
|
if (data.type) {
|
||||||
console.log(
|
console.log("收到消息:", getMessageTypeName(data.type), data.data);
|
||||||
"收到 WebSocket 消息",
|
|
||||||
getMessageTypeName(data.type),
|
|
||||||
data.data
|
|
||||||
);
|
|
||||||
if (onMessage) onMessage({ ...(data.data || {}), type: data.type });
|
if (onMessage) onMessage({ ...(data.data || {}), type: data.type });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (onMessage && data.updates) onMessage(data.updates);
|
if (onMessage && data.updates) onMessage(data.updates);
|
||||||
const msg = data.updates[0];
|
const msg = data.updates[0];
|
||||||
if (msg) {
|
if (msg) {
|
||||||
console.log(
|
console.log("收到消息:", getMessageTypeName(msg.constructor), msg);
|
||||||
"收到 WebSocket 更新",
|
|
||||||
getMessageTypeName(msg.constructor),
|
|
||||||
msg
|
|
||||||
);
|
|
||||||
if (msg.constructor === MESSAGETYPES.RankUpdate) {
|
if (msg.constructor === MESSAGETYPES.RankUpdate) {
|
||||||
uni.setStorageSync("latestRank", msg.lvl);
|
uni.setStorageSync("latestRank", msg.lvl);
|
||||||
} else if (msg.constructor === MESSAGETYPES.LvlUpdate) {
|
} else if (msg.constructor === MESSAGETYPES.LvlUpdate) {
|
||||||
@@ -101,109 +68,84 @@ function createWebSocket(token, onMessage) {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
socketTask.onError((err) => {
|
// 错误处理
|
||||||
if (socket !== socketTask) return;
|
uni.onSocketError((err) => {
|
||||||
console.error("WebSocket 错误", err);
|
console.error("WebSocket 错误", err);
|
||||||
|
reconnect(onMessage);
|
||||||
});
|
});
|
||||||
|
|
||||||
socketTask.onClose(async (result) => {
|
uni.onSocketClose((result) => {
|
||||||
if (socket !== socketTask) return;
|
|
||||||
console.log("WebSocket 已关闭", result);
|
console.log("WebSocket 已关闭", result);
|
||||||
stopHeartbeat();
|
stopHeartbeat();
|
||||||
socket = null;
|
reconnect(onMessage);
|
||||||
isConnecting = false;
|
|
||||||
|
|
||||||
if (manualClose || kickedOut) return;
|
|
||||||
await handleUnexpectedClose(onMessage);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async function handleUnexpectedClose(onMessage) {
|
/**
|
||||||
if (checkingSession || manualClose || kickedOut) return;
|
* 重连机制
|
||||||
|
*/
|
||||||
|
function reconnect(onMessage) {
|
||||||
|
reconnectTimer && clearTimeout(reconnectTimer);
|
||||||
|
closeWebSocket(); // 确保关闭旧连接
|
||||||
|
|
||||||
const token = uni.getStorageSync(
|
const token = uni.getStorageSync(
|
||||||
`${uni.getAccountInfoSync().miniProgram.envVersion}_token`
|
`${uni.getAccountInfoSync().miniProgram.envVersion}_token`
|
||||||
);
|
);
|
||||||
if (!token) return;
|
if (!token) return;
|
||||||
|
|
||||||
checkingSession = true;
|
|
||||||
try {
|
|
||||||
await getUserGameState();
|
|
||||||
if (!manualClose && !kickedOut) reconnect(onMessage);
|
|
||||||
} catch (err) {
|
|
||||||
if (err?.type === "AUTH_INVALID") {
|
|
||||||
kickedOut = true;
|
|
||||||
manualClose = true;
|
|
||||||
reconnectTimer && clearTimeout(reconnectTimer);
|
|
||||||
uni.$emit("session-kicked-out");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (!manualClose && !kickedOut) reconnect(onMessage);
|
|
||||||
} finally {
|
|
||||||
checkingSession = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function reconnect(onMessage) {
|
|
||||||
reconnectTimer && clearTimeout(reconnectTimer);
|
|
||||||
|
|
||||||
const token = uni.getStorageSync(
|
|
||||||
`${uni.getAccountInfoSync().miniProgram.envVersion}_token`
|
|
||||||
);
|
|
||||||
if (!token || manualClose || kickedOut || socket || isConnecting) return;
|
|
||||||
|
|
||||||
reconnectTimer = setTimeout(() => {
|
reconnectTimer = setTimeout(() => {
|
||||||
if (manualClose || kickedOut || socket || isConnecting) return;
|
console.log("reconnecting...");
|
||||||
console.log("WebSocket 正在重连...");
|
|
||||||
createWebSocket(token, onMessage);
|
createWebSocket(token, onMessage);
|
||||||
}, 1000);
|
}, 1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
function closeWebSocket(isManual = true) {
|
function closeWebSocket() {
|
||||||
manualClose = isManual;
|
if (socket) {
|
||||||
reconnectTimer && clearTimeout(reconnectTimer);
|
reconnectTimer && clearTimeout(reconnectTimer);
|
||||||
stopHeartbeat();
|
stopHeartbeat();
|
||||||
isConnecting = false;
|
|
||||||
|
|
||||||
if (socket) {
|
|
||||||
const currentSocket = socket;
|
|
||||||
socket = null;
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
currentSocket.close();
|
socket.close();
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error("关闭 WebSocket 失败", err);
|
console.error("关闭WebSocket连接失败", err);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
socket = null; // 清除socket引用
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function sendHeartbeat(onMessage) {
|
function sendHeartbeat(onMessage) {
|
||||||
if (!socket) return;
|
uni.sendSocketMessage({
|
||||||
|
|
||||||
const currentSocket = socket;
|
|
||||||
currentSocket.send({
|
|
||||||
data: JSON.stringify({ event: "ping", data: {} }),
|
data: JSON.stringify({ event: "ping", data: {} }),
|
||||||
success: () => {},
|
success: () => {
|
||||||
|
// console.log("发送心跳成功");
|
||||||
|
},
|
||||||
fail: (err) => {
|
fail: (err) => {
|
||||||
if (socket !== currentSocket) return;
|
console.error("发送心跳失败", err);
|
||||||
console.error("心跳发送失败", err);
|
|
||||||
stopHeartbeat();
|
stopHeartbeat();
|
||||||
closeWebSocket(false);
|
closeWebSocket(); // 关闭失效的连接
|
||||||
reconnect(onMessage);
|
reconnect(onMessage); // 触发重连
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 启动心跳
|
||||||
|
*/
|
||||||
function startHeartbeat(onMessage) {
|
function startHeartbeat(onMessage) {
|
||||||
stopHeartbeat();
|
stopHeartbeat(); // 防止重复启动
|
||||||
|
|
||||||
heartbeatInterval = setInterval(() => {
|
heartbeatInterval = setInterval(() => {
|
||||||
if (socket) {
|
if (socket && socket.readyState === 1) {
|
||||||
|
// 检查连接状态
|
||||||
sendHeartbeat(onMessage);
|
sendHeartbeat(onMessage);
|
||||||
}
|
}
|
||||||
}, 10000);
|
}, 10000);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 停止心跳
|
||||||
|
*/
|
||||||
function stopHeartbeat() {
|
function stopHeartbeat() {
|
||||||
if (heartbeatInterval) {
|
if (heartbeatInterval) {
|
||||||
clearInterval(heartbeatInterval);
|
clearInterval(heartbeatInterval);
|
||||||
|
|||||||
Reference in New Issue
Block a user