From 698da6b27bdccfbbc85c4287f9bf071d5f58cff8 Mon Sep 17 00:00:00 2001 From: moonrailgun Date: Sun, 12 Sep 2021 15:55:10 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E9=80=80=E5=87=BA=E7=99=BB?= =?UTF-8?q?=E5=BD=95=E6=97=B6=E6=96=AD=E5=BC=80socket=E8=BF=9E=E6=8E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- shared/api/socket.ts | 4 ++++ web/src/components/modals/SettingsView/Account.tsx | 2 ++ web/src/routes/Main/Provider.tsx | 3 ++- web/src/utils/global-state-helper.ts | 13 ++++++++++++- 4 files changed, 20 insertions(+), 2 deletions(-) diff --git a/shared/api/socket.ts b/shared/api/socket.ts index 0d109e0f..7cd49703 100644 --- a/shared/api/socket.ts +++ b/shared/api/socket.ts @@ -68,6 +68,10 @@ export class AppSocket { this.listener.push([`notify:${eventName}`, callback as any]); } + close() { + this.socket.close(); + } + /** * 初始Socket状态管理提示 */ diff --git a/web/src/components/modals/SettingsView/Account.tsx b/web/src/components/modals/SettingsView/Account.tsx index c75f7472..afcd3410 100644 --- a/web/src/components/modals/SettingsView/Account.tsx +++ b/web/src/components/modals/SettingsView/Account.tsx @@ -4,6 +4,7 @@ import { DefaultFullModalInputEditorRender, FullModalField, } from '@/components/FullModal/Field'; +import { getGlobalSocket } from '@/utils/global-state-helper'; import { setUserJWT } from '@/utils/jwt-helper'; import { Button, Divider } from 'antd'; import React, { useCallback } from 'react'; @@ -55,6 +56,7 @@ export const SettingsAccount: React.FC = React.memo(() => { // 登出 const handleLogout = useCallback(async () => { await setUserJWT(null); + getGlobalSocket()?.close(); history.push('/'); }, []); diff --git a/web/src/routes/Main/Provider.tsx b/web/src/routes/Main/Provider.tsx index d7cf71ea..747ae260 100644 --- a/web/src/routes/Main/Provider.tsx +++ b/web/src/routes/Main/Provider.tsx @@ -15,7 +15,7 @@ import { getUserJWT } from '../../utils/jwt-helper'; import { useHistory } from 'react-router'; import { SidebarContextProvider } from './SidebarContext'; import { PortalHost } from '@/components/Portal'; -import { setGlobalStore } from '@/utils/global-state-helper'; +import { setGlobalSocket, setGlobalStore } from '@/utils/global-state-helper'; /** * 应用状态管理hooks @@ -48,6 +48,7 @@ function useAppState() { // 创建 websocket 连接 const socket = await createSocket(userLoginInfo.token); + setGlobalSocket(socket); // 初始化Redux setupRedux(socket, store); diff --git a/web/src/utils/global-state-helper.ts b/web/src/utils/global-state-helper.ts index 7005dcf4..a2ef7d9e 100644 --- a/web/src/utils/global-state-helper.ts +++ b/web/src/utils/global-state-helper.ts @@ -1,4 +1,4 @@ -import type { AppStore, AppState } from 'tailchat-shared'; +import type { AppStore, AppState, AppSocket } from 'tailchat-shared'; let _store: AppStore; export function setGlobalStore(store: AppStore) { @@ -11,3 +11,14 @@ export function getGlobalState(): AppState | null { } return _store.getState(); } + +let _socket: AppSocket; +export function setGlobalSocket(socket: AppSocket) { + _socket = socket; +} +export function getGlobalSocket(): AppSocket | null { + if (!_socket) { + return null; + } + return _socket; +}