Skip to content

Instantly share code, notes, and snippets.

@victusfate
Created October 12, 2022 20:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save victusfate/668432425e87636bb9076654d655b1a3 to your computer and use it in GitHub Desktop.
Save victusfate/668432425e87636bb9076654d655b1a3 to your computer and use it in GitHub Desktop.
export default function AppWs() {
const [isPaused, setPause] = useState(false);
const ws = useRef(null);
useEffect(() => {
ws.current = new WebSocket("wss://ws.kraken.com/");
ws.current.onopen = () => console.log("ws opened");
ws.current.onclose = () => console.log("ws closed");
const wsCurrent = ws.current;
return () => {
wsCurrent.close();
};
}, []);
useEffect(() => {
if (!ws.current) return;
ws.current.onmessage = e => {
if (isPaused) return;
const message = JSON.parse(e.data);
console.log("e", message);
};
}, [isPaused]);
return (
<div>
<button onClick={() => setPause(!isPaused)}>
{isPaused ? "Resume" : "Pause"}
</button>
</div>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment