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

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

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

@ -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('/');
}, []);

@ -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);

@ -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;
}

Loading…
Cancel
Save