Search
 
SCRIPT & CODE EXAMPLE
 

TYPESCRIPT

react native websocket disconnect handler

// eventHandlers.ts
export const useConnectionPauseHandler(
  state: IGameData,
  dispatch: React.Dispatch<any>
) => {
  const [connectFn, setConnectFn] = useState<ConnectFN>(
    null as unknown as ConnectFN
  );

  const disconnectCallback = useCallback(() => {
    if (state.connectionStatus !== ConnectionLifecycle.RESUMED)
      dispatch({ type: 'DISCONNECTED' });
  }, [dispatch, state.connectionStatus]);

  const pauseCallback = useCallback(() => {
    if (...) {
      // disconnection is expected, or an error is prevting the connection from reconnecting
      console.log('expected disconnection');
      dispatch({ type: 'DISCONNECTED' });
    } else if (...) {
      // connection is unexpected, and not attempting reconnection
      console.log('unexpected disconnection');
      dispatch('SESSION_PAUSED');
      if (connectFn) connectFn(state.gameCode!, null, state.playerId);
      setTimeout(disconnectCallback, 30 * 1000);
    }
  }, [
    disconnectCallback,
    dispatch,
    connectFn,
    state.gameCode,
    state.playerId,
    state.connectionStatus,
    state.gameStatus,
  ]);

  const registerConnectFunction = useCallback((fn: ConnectFN) => {
    setConnectFn(() => fn); // do this to avoid confusing the react dispatch function
  }, []);

  return [registerConnectFunction, pauseCallback];
}

// GameContextProvider.tsx
  const [setConnectFn, onClose] = useConnectionPauseHandler(state, dispatch);
  const [connect, sendMessage] = useSession(
    onOpen,
    onMessage,
    onClose
  );

  useEffect(() => {
    console.log('wiring everything...');
    setConnectFn(connect);
  }, [setConnectFn, connect]);
Comment

PREVIOUS NEXT
Code Example
Typescript :: What is the aim of an ARP spoofing attack? 
Typescript :: exclude redults after theninclude 
Typescript :: HOC Generic using React Typescript 
Typescript :: when to test analysis 
Typescript :: teken aja 
Typescript :: laravel orm fetures 
Typescript :: Adding API request 
Typescript :: subscripts list c# 
Typescript :: alternative for .include in typescript 
Typescript :: e.target.value typescript 
Typescript :: How to exclude a particular test group from a test case execution? 
Typescript :: how to validate if all characters enetred in a string are alphabets and then reprompt user 
Typescript :: how to find nuber of tweets per day using python 
Typescript :: ionic iosa app not making requests to server 
Typescript :: Tailwin navbar structure 
Typescript :: typescript for vue 
Typescript :: check if breckets clossing properly 
Typescript :: TypeScript interface for object with arbitrary numeric property names? 
Typescript :: ngsw pwa how to check version update 
Typescript :: custom function with condition in google sheet 
Typescript :: nestjs called every X second method 
Typescript :: how to read web page in type script 
Typescript :: server sent events httpclient java.net 
Typescript :: typescript directory structure 
Typescript :: how should a developer write unit tests for a private method in an apex class 
Typescript :: js Validating maps 
Typescript :: is brackets a good code editor 
Typescript :: What are the components of the environment? Explain it along with the examples. 
Typescript :: google fonts roboto 
Cpp :: sfml draw line 
ADD CONTENT
Topic
Content
Source link
Name
1+9 =