mirror of https://github.com/msgbyte/tailchat
feat: 增加socket上下文并增加模拟重连机制的调试
parent
ee0a0972b5
commit
04b92b9f17
@ -0,0 +1,30 @@
|
||||
import { Button } from 'antd';
|
||||
import React, { useEffect, useState } from 'react';
|
||||
import { useSocketContext } from '@/context/SocketContext';
|
||||
|
||||
export const SettingsDebug: React.FC = React.memo(() => {
|
||||
const socket = useSocketContext();
|
||||
const [socketConnected, setSocketConnected] = useState(true);
|
||||
|
||||
useEffect(() => {
|
||||
const id = setInterval(() => {
|
||||
setSocketConnected(socket.connected);
|
||||
}, 1000);
|
||||
|
||||
return () => {
|
||||
clearInterval(id);
|
||||
};
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<div>
|
||||
<p>当前Socket状态: {JSON.stringify(socketConnected)}</p>
|
||||
<div className="space-x-1">
|
||||
<Button type="primary" onClick={() => socket.mockReconnect()}>
|
||||
Socket 模拟重连
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
});
|
||||
SettingsDebug.displayName = 'SettingsDebug';
|
@ -0,0 +1,21 @@
|
||||
import React, { useContext } from 'react';
|
||||
import type { AppSocket } from 'tailchat-shared';
|
||||
|
||||
const SocketContext = React.createContext<AppSocket>({} as AppSocket);
|
||||
SocketContext.displayName = 'SocketContext';
|
||||
|
||||
export const SocketContextProvider: React.FC<{ socket: AppSocket }> =
|
||||
React.memo((props) => {
|
||||
return (
|
||||
<SocketContext.Provider value={props.socket}>
|
||||
{props.children}
|
||||
</SocketContext.Provider>
|
||||
);
|
||||
});
|
||||
SocketContextProvider.displayName = 'SocketContextProvider';
|
||||
|
||||
export function useSocketContext(): AppSocket {
|
||||
const context = useContext(SocketContext);
|
||||
|
||||
return context;
|
||||
}
|
Loading…
Reference in New Issue