refactor: 退出登录时断开socket连接

pull/13/head
moonrailgun 5 years ago
parent 1ace22201d
commit 698da6b27b

@ -68,6 +68,10 @@ export class AppSocket {
this.listener.push([`notify:${eventName}`, callback as any]); this.listener.push([`notify:${eventName}`, callback as any]);
} }
close() {
this.socket.close();
}
/** /**
* Socket * Socket
*/ */

@ -4,6 +4,7 @@ import {
DefaultFullModalInputEditorRender, DefaultFullModalInputEditorRender,
FullModalField, FullModalField,
} from '@/components/FullModal/Field'; } from '@/components/FullModal/Field';
import { getGlobalSocket } from '@/utils/global-state-helper';
import { setUserJWT } from '@/utils/jwt-helper'; import { setUserJWT } from '@/utils/jwt-helper';
import { Button, Divider } from 'antd'; import { Button, Divider } from 'antd';
import React, { useCallback } from 'react'; import React, { useCallback } from 'react';
@ -55,6 +56,7 @@ export const SettingsAccount: React.FC = React.memo(() => {
// 登出 // 登出
const handleLogout = useCallback(async () => { const handleLogout = useCallback(async () => {
await setUserJWT(null); await setUserJWT(null);
getGlobalSocket()?.close();
history.push('/'); history.push('/');
}, []); }, []);

@ -15,7 +15,7 @@ import { getUserJWT } from '../../utils/jwt-helper';
import { useHistory } from 'react-router'; import { useHistory } from 'react-router';
import { SidebarContextProvider } from './SidebarContext'; import { SidebarContextProvider } from './SidebarContext';
import { PortalHost } from '@/components/Portal'; import { PortalHost } from '@/components/Portal';
import { setGlobalStore } from '@/utils/global-state-helper'; import { setGlobalSocket, setGlobalStore } from '@/utils/global-state-helper';
/** /**
* hooks * hooks
@ -48,6 +48,7 @@ function useAppState() {
// 创建 websocket 连接 // 创建 websocket 连接
const socket = await createSocket(userLoginInfo.token); const socket = await createSocket(userLoginInfo.token);
setGlobalSocket(socket);
// 初始化Redux // 初始化Redux
setupRedux(socket, store); setupRedux(socket, store);

@ -1,4 +1,4 @@
import type { AppStore, AppState } from 'tailchat-shared'; import type { AppStore, AppState, AppSocket } from 'tailchat-shared';
let _store: AppStore; let _store: AppStore;
export function setGlobalStore(store: AppStore) { export function setGlobalStore(store: AppStore) {
@ -11,3 +11,14 @@ export function getGlobalState(): AppState | null {
} }
return _store.getState(); return _store.getState();
} }
let _socket: AppSocket;
export function setGlobalSocket(socket: AppSocket) {
_socket = socket;
}
export function getGlobalSocket(): AppSocket | null {
if (!_socket) {
return null;
}
return _socket;
}

Loading…
Cancel
Save