把socket放到最外面

This commit is contained in:
kron
2025-06-05 21:32:15 +08:00
parent 219fdc54ad
commit 58bd5d9bb2
6 changed files with 110 additions and 73 deletions

View File

@@ -36,33 +36,34 @@ const createPractise = async (arrows) => {
const result = await createPractiseAPI(arrows);
};
onMounted(() => {
const token = uni.getStorageSync("token");
websocket.createWebSocket(token, (content) => {
const messages = JSON.parse(content).data.updates || [];
messages.forEach((msg) => {
if (msg.constructor === MESSAGETYPES.ShootSyncMeArrowID) {
scores.value.push(msg.target);
if (scores.value.length === total) {
showScore.value = true;
}
// if (step.value === 2 && msg.target.dst / 100 > 5) {
if (step.value === 2 && msg.target.dst > 5) {
btnDisabled.value = false;
}
async function onReceiveMessage(content) {
const messages = JSON.parse(content).data.updates || [];
messages.forEach((msg) => {
if (msg.constructor === MESSAGETYPES.ShootSyncMeArrowID) {
scores.value.push(msg.target);
if (scores.value.length === total) {
showScore.value = true;
}
if (msg.constructor === MESSAGETYPES.ShootSyncMePracticeID) {
practiseResult.value = {
...msg.practice,
arrows: JSON.parse(msg.practice.arrows),
};
// if (step.value === 2 && msg.target.dst / 100 > 5) {
if (step.value === 2 && msg.target.dst > 5) {
btnDisabled.value = false;
}
});
}
if (msg.constructor === MESSAGETYPES.ShootSyncMePracticeID) {
practiseResult.value = {
...msg.practice,
arrows: JSON.parse(msg.practice.arrows),
};
}
});
}
onMounted(() => {
uni.$on("socket-inbox", onReceiveMessage);
});
onUnmounted(() => {
websocket.closeWebSocket();
uni.$off("socket-inbox", onReceiveMessage);
});
const nextStep = async () => {