einargs / tnhimss-bill

Mtsu Bill Team repository for the 2023 TNHIMSS hackathon.
1 stars 3 forks source link

Website sets up websocket #11

Open einargs opened 1 year ago

einargs commented 1 year ago

The website uses an effect to setup a websocket that will connect to the server. This could also be combined with the code to receive the chatlog.

See the socket.js file in the audio team repo for more information.

export function useSocket() {
  const [isConnected, setIsConnected] = useState(socket.connected);

  useEffect(() => {
    function onConnect() {
      setIsConnected(true);
    }

    function onDisconnect() {
      setIsConnected(false);
    }

    socket.on('connect', onConnect);
    socket.on('disconnect', onDisconnect);

    return () => {
      socket.off('connect', onConnect);
      socket.off('disconnect', onDisconnect);
    };
  }, []);
  return [isConnected]
}