细节调整
This commit is contained in:
@@ -13,19 +13,21 @@ const RECONNECT_CONFIG = {
|
||||
/**
|
||||
* 建立 WebSocket 连接
|
||||
*/
|
||||
function createWebSocket(token, onMessage) {
|
||||
function createWebSocket(token, onUpdate, onMessage) {
|
||||
const url = `wss://api.shelingxingqiu.com/socket?authorization=${token}`;
|
||||
socket = uni.connectSocket({
|
||||
url,
|
||||
success: () => {
|
||||
console.log("websocket 连接成功");
|
||||
onUpdate(true);
|
||||
reconnectCount = 0; // 重置重连次数
|
||||
},
|
||||
});
|
||||
|
||||
// 接收消息
|
||||
uni.onSocketMessage((res) => {
|
||||
if (onMessage) onMessage(res.data);
|
||||
const data = JSON.parse(res.data);
|
||||
if (onMessage) onMessage(data.data.updates);
|
||||
});
|
||||
|
||||
// 错误处理
|
||||
@@ -36,6 +38,7 @@ function createWebSocket(token, onMessage) {
|
||||
|
||||
uni.onSocketClose((result) => {
|
||||
console.log("WebSocket 已关闭", result);
|
||||
onUpdate(false);
|
||||
stopHeartbeat();
|
||||
reconnect(token, onMessage);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user