diff --git a/shared/api/socket.ts b/shared/api/socket.ts index 872eb1db..f807950c 100644 --- a/shared/api/socket.ts +++ b/shared/api/socket.ts @@ -23,7 +23,7 @@ type SocketEventRespones = * 封装后的 Socket */ export class AppSocket { - listener: [string, (data: unknown) => void][] = []; + private listener: [string, (data: unknown) => void][] = []; constructor(private socket: Socket) { socket.onAny((eventName: string, data: unknown) => { @@ -70,6 +70,13 @@ export class AppSocket { this.socket.close(); } + /** + * 断线重连后触发 + */ + onReconnect(cb: () => void) { + this.socket.on('reconnect', cb); + } + /** * 初始Socket状态管理提示 */ diff --git a/shared/redux/setup.ts b/shared/redux/setup.ts index bc64bbfe..ce639e7b 100644 --- a/shared/redux/setup.ts +++ b/shared/redux/setup.ts @@ -22,6 +22,12 @@ import { appendUserDMConverse } from '../model/user'; export function setupRedux(socket: AppSocket, store: AppStore) { initial(socket, store); listenNotify(socket, store); + + // 断线重连重新初始化信息 + socket.onReconnect(() => { + console.warn('因为断线重连触发重新同步远程数据'); + initial(socket, store); + }); } /** diff --git a/shared/redux/slices/group.ts b/shared/redux/slices/group.ts index 0c89a9eb..f682049b 100644 --- a/shared/redux/slices/group.ts +++ b/shared/redux/slices/group.ts @@ -13,6 +13,9 @@ const groupSlice = createSlice({ name: 'group', initialState, reducers: { + /** + * 追加或更新群组信息 + */ appendGroups(state, action: PayloadAction) { const groups = action.payload;