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