Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

react native websocket useSession

// websocketUtils.ts

export const useSession = (
  onOpen: OpenHandler, 
  onMessage: MessageHandler, 
  onClose: CloseHandler
): SessionHook => {
  const [session, setSession] = useState(null as unkown as Websocket);
  const updateOpenHandler = () => {
    if (!session) return;
    session.addEventListener('open', onOpen);
    return () => {
      session.removeEventListener('open', onOpen);
    };
  };

  const updateMessageHandler = () => {
    if (!session) return;
    session.addEventListener('message', onMessage);
    return () => {
      session.removeEventListener('message', onMessage);
    };
  };

  const updateCloseHandler = () => {
    if (!session) return;
    session.addEventListener('close', onClose);
    return () => {
      session.removeEventListener('close', onClose);
    };
  };

  useEffect(updateOpenHandler, [session, onOpen]);
  useEffect(updateMessageHandler, [session, onMessage]);
  useEffect(updateCloseHandler, [session, onClose]);

   .... // connect, sendMessage utils
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: rivets js bind 
Javascript :: create a panda component react 
Javascript :: javascript get name from steamid 
Javascript :: node load testing-check 
Javascript :: formating decimal hours as hours and minute javascript 
Javascript :: apar chinmoy phy js code 
Javascript :: discord.js v13 joinVoiceChannel 
Javascript :: angular material dialog close pass data 
Javascript :: express cors policy 
Javascript :: React ES6 Arrow Functions 
Javascript :: lodash uniqby 
Javascript :: Is there an “exists” function for jQuery 
Javascript :: how to get all scripts on a page javascript 
Javascript :: install svelte routing 
Javascript :: appinsights trackException javascript 
Javascript :: how to do addition in javascript 
Javascript :: react router v6 pass props 
Javascript :: JQuery UI Saving Sortable List 
Javascript :: jquery slick drag goes back 
Javascript :: javascript add item by index 
Javascript :: image uploading using formdata 
Javascript :: Swap a select text with javascript 
Javascript :: ask for expo token and save to firebase 
Javascript :: JS function typeof 
Javascript :: file input only allow json 
Javascript :: how a message persist in the localstorage in javascript 
Javascript :: exit node 
Javascript :: onselect in zebra datepicker 
Javascript :: react hook form submit outside form 
Javascript :: Highlight current nav link in react 
ADD CONTENT
Topic
Content
Source link
Name
1+1 =