Compare commits
13 Commits
new-race-m
...
b168fd54a4
| Author | SHA1 | Date | |
|---|---|---|---|
| b168fd54a4 | |||
| da023c60f5 | |||
| cb4027418d | |||
| 96cc69f041 | |||
| 860cdbe332 | |||
| 8b9c862b96 | |||
| 19391808ef | |||
| 9cc6ef4b88 | |||
| fc14489ab3 | |||
| aba2da56d7 | |||
| 24314e5ec8 | |||
| 4dcfdeda68 | |||
| a6becf67ff |
23
src/App.vue
23
src/App.vue
@@ -21,7 +21,10 @@
|
|||||||
} = storeToRefs(store);
|
} = storeToRefs(store);
|
||||||
const {
|
const {
|
||||||
updateUser,
|
updateUser,
|
||||||
updateOnline
|
updateOnline,
|
||||||
|
updateDevice,
|
||||||
|
updateGame,
|
||||||
|
updateRoomNumber
|
||||||
} = store;
|
} = store;
|
||||||
|
|
||||||
watch(
|
watch(
|
||||||
@@ -46,6 +49,22 @@
|
|||||||
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);
|
||||||
@@ -65,6 +84,7 @@
|
|||||||
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`
|
||||||
);
|
);
|
||||||
@@ -77,6 +97,7 @@
|
|||||||
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,6 +46,8 @@ 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});
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
<script setup>
|
<script setup>
|
||||||
import { ref, onMounted, onBeforeUnmount } from "vue";
|
import { ref, onMounted, onBeforeUnmount, watch } from "vue";
|
||||||
import { onLoad, onShow, onHide } from "@dcloudio/uni-app";
|
import { onLoad, onShow, onHide } from "@dcloudio/uni-app";
|
||||||
import Container from "@/components/Container.vue";
|
import Container from "@/components/Container.vue";
|
||||||
import BowTarget from "@/components/BowTarget.vue";
|
import BowTarget from "@/components/BowTarget.vue";
|
||||||
@@ -10,19 +10,31 @@ import SButton from "@/components/SButton.vue";
|
|||||||
import Avatar from "@/components/Avatar.vue";
|
import Avatar from "@/components/Avatar.vue";
|
||||||
import ScreenHint from "@/components/ScreenHint.vue";
|
import ScreenHint from "@/components/ScreenHint.vue";
|
||||||
import TestDistance from "@/components/TestDistance.vue";
|
import TestDistance from "@/components/TestDistance.vue";
|
||||||
|
import SModal from "@/components/SModal.vue";
|
||||||
import audioManager from "@/audioManager";
|
import audioManager from "@/audioManager";
|
||||||
import { getBattleAPI, laserCloseAPI } from "@/apis";
|
import { getBattleAPI, laserCloseAPI } from "@/apis";
|
||||||
import { MESSAGETYPESV2 } from "@/constants";
|
import { MESSAGETYPESV2 } from "@/constants";
|
||||||
import useStore from "@/store";
|
import useStore from "@/store";
|
||||||
import { storeToRefs } from "pinia";
|
import { storeToRefs } from "pinia";
|
||||||
const store = useStore();
|
const store = useStore();
|
||||||
const { user } = storeToRefs(store);
|
const { user, online } = storeToRefs(store);
|
||||||
const title = ref("");
|
const title = ref("");
|
||||||
const start = ref(null);
|
const start = ref(null);
|
||||||
const battleId = ref("");
|
const battleId = ref("");
|
||||||
/** 对战模式:1=好友约战 2=排位赛,用于结算页跳转判断 */
|
/** 对战模式:1=好友约战 2=排位赛,用于结算页跳转判断 */
|
||||||
const way = ref(0);
|
const way = ref(0);
|
||||||
const currentRound = ref(1);
|
const currentRound = ref(1);
|
||||||
|
/** 控制设备离线提示弹窗的显示状态 */
|
||||||
|
const showOfflineModal = ref(false);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 监听设备在线状态,大乱斗比赛进行中设备离线时弹窗提示用户
|
||||||
|
*/
|
||||||
|
watch(online, (newVal, oldVal) => {
|
||||||
|
if (!newVal && oldVal && start.value === true) {
|
||||||
|
showOfflineModal.value = true;
|
||||||
|
}
|
||||||
|
});
|
||||||
const tips = ref("即将开始...");
|
const tips = ref("即将开始...");
|
||||||
const players = ref([]);
|
const players = ref([]);
|
||||||
const playersSorted = ref([]);
|
const playersSorted = ref([]);
|
||||||
@@ -208,6 +220,19 @@ onShow(async () => {
|
|||||||
<text>20秒后开始下半场</text>
|
<text>20秒后开始下半场</text>
|
||||||
</view>
|
</view>
|
||||||
</ScreenHint>
|
</ScreenHint>
|
||||||
|
<!-- 设备离线提示弹窗 -->
|
||||||
|
<SModal
|
||||||
|
:show="showOfflineModal"
|
||||||
|
:noBg="true"
|
||||||
|
height="360rpx"
|
||||||
|
:onClose="() => (showOfflineModal = false)"
|
||||||
|
>
|
||||||
|
<view class="offline-modal">
|
||||||
|
<text class="offline-title">设备已离线</text>
|
||||||
|
<text class="offline-desc">检测到设备已断开连接,请检查设备后继续比赛</text>
|
||||||
|
<SButton @click="showOfflineModal = false">我知道了</SButton>
|
||||||
|
</view>
|
||||||
|
</SModal>
|
||||||
</view>
|
</view>
|
||||||
</Container>
|
</Container>
|
||||||
</template>
|
</template>
|
||||||
@@ -217,4 +242,25 @@ onShow(async () => {
|
|||||||
width: 100%;
|
width: 100%;
|
||||||
height: 100%;
|
height: 100%;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.offline-modal {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
align-items: center;
|
||||||
|
padding: 60rpx 40rpx 40rpx;
|
||||||
|
gap: 24rpx;
|
||||||
|
}
|
||||||
|
|
||||||
|
.offline-title {
|
||||||
|
font-size: 36rpx;
|
||||||
|
font-weight: bold;
|
||||||
|
color: #FED847;
|
||||||
|
}
|
||||||
|
|
||||||
|
.offline-desc {
|
||||||
|
font-size: 28rpx;
|
||||||
|
color: #CCCCCC;
|
||||||
|
text-align: center;
|
||||||
|
line-height: 1.6;
|
||||||
|
}
|
||||||
</style>
|
</style>
|
||||||
|
|||||||
@@ -89,7 +89,7 @@ async function onComplete() {
|
|||||||
practiseResult.value = {};
|
practiseResult.value = {};
|
||||||
start.value = false;
|
start.value = false;
|
||||||
scores.value = [];
|
scores.value = [];
|
||||||
const result = await createPractiseAPI(total, 360);
|
const result = await createPractiseAPI(total, 3600);
|
||||||
if (result) practiseId.value = result.id;
|
if (result) practiseId.value = result.id;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -105,7 +105,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, 360, targetType.value);
|
const result = await createPractiseAPI(total, 3600, targetType.value);
|
||||||
if (result) practiseId.value = result.id;
|
if (result) practiseId.value = result.id;
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -132,7 +132,7 @@ onBeforeUnmount(() => {
|
|||||||
<ShootProgress
|
<ShootProgress
|
||||||
:tips="`请连续射${total}支箭`"
|
:tips="`请连续射${total}支箭`"
|
||||||
:start="start"
|
:start="start"
|
||||||
:total="360"
|
:total="3600"
|
||||||
:onStop="onOver"
|
:onStop="onOver"
|
||||||
/>
|
/>
|
||||||
<view class="user-row">
|
<view class="user-row">
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
<script setup>
|
<script setup>
|
||||||
import {ref, onMounted, onBeforeUnmount, nextTick} from "vue";
|
import {ref, onMounted, onBeforeUnmount, nextTick, watch} from "vue";
|
||||||
import {onLoad, onShow, onHide} from "@dcloudio/uni-app";
|
import {onLoad, onShow, onHide} from "@dcloudio/uni-app";
|
||||||
import Container from "@/components/Container.vue";
|
import Container from "@/components/Container.vue";
|
||||||
import BattleHeader from "@/components/BattleHeader.vue";
|
import BattleHeader from "@/components/BattleHeader.vue";
|
||||||
@@ -11,6 +11,7 @@ import RoundEndTip from "@/components/RoundEndTip.vue";
|
|||||||
import TestDistance from "@/components/TestDistance.vue";
|
import TestDistance from "@/components/TestDistance.vue";
|
||||||
import TeamAvatars from "@/components/TeamAvatars.vue";
|
import TeamAvatars from "@/components/TeamAvatars.vue";
|
||||||
import ShootProgress2 from "@/components/ShootProgress2.vue";
|
import ShootProgress2 from "@/components/ShootProgress2.vue";
|
||||||
|
import SModal from "@/components/SModal.vue";
|
||||||
import {laserCloseAPI, getBattleAPI} from "@/apis";
|
import {laserCloseAPI, getBattleAPI} from "@/apis";
|
||||||
import {MESSAGETYPESV2} from "@/constants";
|
import {MESSAGETYPESV2} from "@/constants";
|
||||||
import audioManager from "@/audioManager";
|
import audioManager from "@/audioManager";
|
||||||
@@ -18,7 +19,7 @@ import useStore from "@/store";
|
|||||||
import {storeToRefs} from "pinia";
|
import {storeToRefs} from "pinia";
|
||||||
|
|
||||||
const store = useStore();
|
const store = useStore();
|
||||||
const {user} = storeToRefs(store);
|
const {user, online} = storeToRefs(store);
|
||||||
const start = ref(null);
|
const start = ref(null);
|
||||||
const tips = ref("");
|
const tips = ref("");
|
||||||
const battleId = ref("");
|
const battleId = ref("");
|
||||||
@@ -44,6 +45,17 @@ const updateRemainSecond = ref(0);
|
|||||||
const battleWay = ref(0);
|
const battleWay = ref(0);
|
||||||
/** 上一次处理的 ToSomeoneShoot 关键字段,用于去重过滤重复消息 */
|
/** 上一次处理的 ToSomeoneShoot 关键字段,用于去重过滤重复消息 */
|
||||||
const lastToSomeoneShootKey = ref("");
|
const lastToSomeoneShootKey = ref("");
|
||||||
|
/** 控制设备离线提示弹窗的显示状态 */
|
||||||
|
const showOfflineModal = ref(false);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 监听设备在线状态,比赛进行中设备离线时弹窗提示用户
|
||||||
|
*/
|
||||||
|
watch(online, (newVal, oldVal) => {
|
||||||
|
if (!newVal && oldVal && start.value === true) {
|
||||||
|
showOfflineModal.value = true;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
const recoverData = (battleInfo, {force = false, arrowOnly = false} = {}) => {
|
const recoverData = (battleInfo, {force = false, arrowOnly = false} = {}) => {
|
||||||
try {
|
try {
|
||||||
@@ -314,6 +326,19 @@ onShow(async () => {
|
|||||||
:onAutoClose="()=>{ showRoundTip = false}"
|
:onAutoClose="()=>{ showRoundTip = false}"
|
||||||
/>
|
/>
|
||||||
</ScreenHint>
|
</ScreenHint>
|
||||||
|
<!-- 设备离线提示弹窗 -->
|
||||||
|
<SModal
|
||||||
|
:show="showOfflineModal"
|
||||||
|
:noBg="true"
|
||||||
|
height="360rpx"
|
||||||
|
:onClose="() => (showOfflineModal = false)"
|
||||||
|
>
|
||||||
|
<view class="offline-modal">
|
||||||
|
<text class="offline-title">设备已离线</text>
|
||||||
|
<text class="offline-desc">检测到设备已断开连接,请检查设备后继续比赛</text>
|
||||||
|
<SButton @click="showOfflineModal = false">我知道了</SButton>
|
||||||
|
</view>
|
||||||
|
</SModal>
|
||||||
</view>
|
</view>
|
||||||
</Container>
|
</Container>
|
||||||
</template>
|
</template>
|
||||||
@@ -331,4 +356,25 @@ onShow(async () => {
|
|||||||
margin-top: -2%;
|
margin-top: -2%;
|
||||||
margin-bottom: 6%;
|
margin-bottom: 6%;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.offline-modal {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
align-items: center;
|
||||||
|
padding: 60rpx 40rpx 40rpx;
|
||||||
|
gap: 24rpx;
|
||||||
|
}
|
||||||
|
|
||||||
|
.offline-title {
|
||||||
|
font-size: 36rpx;
|
||||||
|
font-weight: bold;
|
||||||
|
color: #FED847;
|
||||||
|
}
|
||||||
|
|
||||||
|
.offline-desc {
|
||||||
|
font-size: 28rpx;
|
||||||
|
color: #CCCCCC;
|
||||||
|
text-align: center;
|
||||||
|
line-height: 1.6;
|
||||||
|
}
|
||||||
</style>
|
</style>
|
||||||
|
|||||||
157
src/websocket.js
157
src/websocket.js
@@ -1,61 +1,91 @@
|
|||||||
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();
|
||||||
const envVersion = accountInfo.miniProgram.envVersion;
|
const envVersion = accountInfo.miniProgram.envVersion;
|
||||||
|
|
||||||
switch (envVersion) {
|
switch (envVersion) {
|
||||||
case "develop": // 开发版
|
case "develop":
|
||||||
// url = "ws://localhost:8000/socket";
|
// url = "ws://localhost:8000/socket";
|
||||||
url = "wss://apitest.shelingxingqiu.com/socket";
|
url = "wss://apitest.shelingxingqiu.com/socket";
|
||||||
break;
|
break;
|
||||||
case "trial": // 体验版
|
case "trial":
|
||||||
url = "wss://apitest.shelingxingqiu.com/socket";
|
url = "wss://apitest.shelingxingqiu.com/socket";
|
||||||
break;
|
break;
|
||||||
case "release": // 正式版
|
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("获取环境信息失败,使用默认正式环境", e);
|
console.error("获取 WebSocket 环境信息失败,使用默认正式环境", e);
|
||||||
}
|
}
|
||||||
|
|
||||||
url += `?authorization=${token}`;
|
url += `?authorization=${token}`;
|
||||||
socket = uni.connectSocket({
|
const socketTask = uni.connectSocket({
|
||||||
url,
|
url,
|
||||||
success: () => {
|
success: () => {
|
||||||
console.log("websocket 连接成功");
|
console.log("WebSocket 已发起连接");
|
||||||
// 启动心跳
|
|
||||||
startHeartbeat(onMessage);
|
|
||||||
},
|
},
|
||||||
fail: () => {
|
fail: (err) => {
|
||||||
|
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("收到消息:", getMessageTypeName(data.type), data.data);
|
console.log(
|
||||||
|
"收到 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("收到消息:", getMessageTypeName(msg.constructor), msg);
|
console.log(
|
||||||
|
"收到 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) {
|
||||||
@@ -68,84 +98,109 @@ function createWebSocket(token, onMessage) {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// 错误处理
|
socketTask.onError((err) => {
|
||||||
uni.onSocketError((err) => {
|
if (socket !== socketTask) return;
|
||||||
console.error("WebSocket 错误", err);
|
console.error("WebSocket 错误", err);
|
||||||
reconnect(onMessage);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
uni.onSocketClose((result) => {
|
socketTask.onClose(async (result) => {
|
||||||
|
if (socket !== socketTask) return;
|
||||||
console.log("WebSocket 已关闭", result);
|
console.log("WebSocket 已关闭", result);
|
||||||
stopHeartbeat();
|
stopHeartbeat();
|
||||||
reconnect(onMessage);
|
socket = null;
|
||||||
|
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(() => {
|
||||||
console.log("reconnecting...");
|
if (manualClose || kickedOut || socket || isConnecting) return;
|
||||||
|
console.log("WebSocket 正在重连...");
|
||||||
createWebSocket(token, onMessage);
|
createWebSocket(token, onMessage);
|
||||||
}, 1000);
|
}, 1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
function closeWebSocket() {
|
function closeWebSocket(isManual = true) {
|
||||||
if (socket) {
|
manualClose = isManual;
|
||||||
reconnectTimer && clearTimeout(reconnectTimer);
|
reconnectTimer && clearTimeout(reconnectTimer);
|
||||||
stopHeartbeat();
|
stopHeartbeat();
|
||||||
|
isConnecting = false;
|
||||||
|
|
||||||
|
if (socket) {
|
||||||
|
const currentSocket = socket;
|
||||||
|
socket = null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
socket.close();
|
currentSocket.close();
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error("关闭WebSocket连接失败", err);
|
console.error("关闭 WebSocket 失败", err);
|
||||||
}
|
}
|
||||||
|
|
||||||
socket = null; // 清除socket引用
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function sendHeartbeat(onMessage) {
|
function sendHeartbeat(onMessage) {
|
||||||
uni.sendSocketMessage({
|
if (!socket) return;
|
||||||
|
|
||||||
|
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) => {
|
||||||
console.error("发送心跳失败", err);
|
if (socket !== currentSocket) return;
|
||||||
|
console.error("心跳发送失败", err);
|
||||||
stopHeartbeat();
|
stopHeartbeat();
|
||||||
closeWebSocket(); // 关闭失效的连接
|
closeWebSocket(false);
|
||||||
reconnect(onMessage); // 触发重连
|
reconnect(onMessage);
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 启动心跳
|
|
||||||
*/
|
|
||||||
function startHeartbeat(onMessage) {
|
function startHeartbeat(onMessage) {
|
||||||
stopHeartbeat(); // 防止重复启动
|
stopHeartbeat();
|
||||||
|
|
||||||
heartbeatInterval = setInterval(() => {
|
heartbeatInterval = setInterval(() => {
|
||||||
if (socket && socket.readyState === 1) {
|
if (socket) {
|
||||||
// 检查连接状态
|
|
||||||
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