nicer listeners
This commit is contained in:
@@ -21,6 +21,23 @@ export function SocketProvider({ children }: { children: React.ReactNode }) {
|
|||||||
|
|
||||||
const [flowState, setFlowState] = useState<boolean>(false);
|
const [flowState, setFlowState] = useState<boolean>(false);
|
||||||
|
|
||||||
|
const initiateSocketStabilityListeners = useCallback((socketConnection: Socket) => {
|
||||||
|
socketConnection.on('connect', () => {
|
||||||
|
toast.success('sockets connected');
|
||||||
|
});
|
||||||
|
|
||||||
|
socketConnection.io.on('close', () => {
|
||||||
|
console.error('SOCKET | connection closed likely due to idling or manual disconnect');
|
||||||
|
toast('unplugging');
|
||||||
|
});
|
||||||
|
|
||||||
|
// client-side errors
|
||||||
|
socketConnection.on('connect_error', (err) => {
|
||||||
|
console.error(`SOCKETS | ${err.message}`);
|
||||||
|
toast.error('sorry...this is our bad...please refresh with cmd + r');
|
||||||
|
});
|
||||||
|
}, []);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!jwtToken) {
|
if (!jwtToken) {
|
||||||
set$ws(undefined);
|
set$ws(undefined);
|
||||||
@@ -37,30 +54,14 @@ export function SocketProvider({ children }: { children: React.ReactNode }) {
|
|||||||
reconnection: false, // ! TESTING THE PROBLEM WITH RECONNECTION CLIENT LISTENERS NOT ACTIVATING
|
reconnection: false, // ! TESTING THE PROBLEM WITH RECONNECTION CLIENT LISTENERS NOT ACTIVATING
|
||||||
});
|
});
|
||||||
|
|
||||||
socketConnection.on('connect', () => {
|
initiateSocketStabilityListeners(socketConnection);
|
||||||
toast.success('sockets connected');
|
|
||||||
});
|
|
||||||
|
|
||||||
socketConnection.io.on('close', () => {
|
|
||||||
console.error(
|
|
||||||
'SOCKET | there was a problem with your app...connection closed likely due to idling or manual disconnect',
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
// client-side errors
|
|
||||||
socketConnection.on('connect_error', (err) => {
|
|
||||||
console.error(`SOCKETS | ${err.message}`);
|
|
||||||
toast.error('sorry...this is our bad...please refresh with cmd + r');
|
|
||||||
set$ws(null);
|
|
||||||
});
|
|
||||||
|
|
||||||
set$ws(socketConnection);
|
set$ws(socketConnection);
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
socketConnection.disconnect();
|
socketConnection.disconnect();
|
||||||
socketConnection.removeAllListeners();
|
socketConnection.removeAllListeners();
|
||||||
};
|
};
|
||||||
}, [set$ws, jwtToken]);
|
}, [set$ws, jwtToken, initiateSocketStabilityListeners]);
|
||||||
|
|
||||||
const handleFlowState = useCallback(() => {
|
const handleFlowState = useCallback(() => {
|
||||||
setFlowState(true);
|
setFlowState(true);
|
||||||
@@ -72,8 +73,9 @@ export function SocketProvider({ children }: { children: React.ReactNode }) {
|
|||||||
const handleReconnect = useCallback(() => {
|
const handleReconnect = useCallback(() => {
|
||||||
setFlowState(false);
|
setFlowState(false);
|
||||||
|
|
||||||
|
initiateSocketStabilityListeners($ws);
|
||||||
$ws.connect();
|
$ws.connect();
|
||||||
}, [setFlowState, $ws]);
|
}, [setFlowState, $ws, initiateSocketStabilityListeners]);
|
||||||
|
|
||||||
if (!$ws) {
|
if (!$ws) {
|
||||||
return (
|
return (
|
||||||
|
|||||||
Reference in New Issue
Block a user