websocket优化
This commit is contained in:
@@ -32,6 +32,7 @@ function createWebSocket(token, onMessage) {
|
|||||||
// 接收消息
|
// 接收消息
|
||||||
uni.onSocketMessage((res) => {
|
uni.onSocketMessage((res) => {
|
||||||
const data = JSON.parse(res.data);
|
const data = JSON.parse(res.data);
|
||||||
|
if(data.event === 'pong' || !data.data.updates) return;
|
||||||
if (onMessage) onMessage(data.data.updates);
|
if (onMessage) onMessage(data.data.updates);
|
||||||
const msg = data.data.updates[0];
|
const msg = data.data.updates[0];
|
||||||
if (msg && msg.constructor === MESSAGETYPES.BackToGame) {
|
if (msg && msg.constructor === MESSAGETYPES.BackToGame) {
|
||||||
@@ -110,18 +111,15 @@ function startHeartbeat() {
|
|||||||
stopHeartbeat(); // 防止重复启动
|
stopHeartbeat(); // 防止重复启动
|
||||||
|
|
||||||
heartbeatInterval = setInterval(() => {
|
heartbeatInterval = setInterval(() => {
|
||||||
if (socket && uni.sendSocketMessage) {
|
if (socket) {
|
||||||
uni.sendSocketMessage({
|
uni.sendSocketMessage({
|
||||||
data: {
|
data: JSON.stringify({ event: "ping", data: {} }),
|
||||||
event: "ping",
|
|
||||||
data: {},
|
|
||||||
},
|
|
||||||
fail: (err) => {
|
fail: (err) => {
|
||||||
console.error("发送心跳失败", err);
|
console.error("发送心跳失败", err);
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}, 5000);
|
}, 10000);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user