I’m trying to write a web game with WebSocket synchronization. In my React app, I receive updates via WebSocket to update the view of the application. My problem is that when I receive multiple messages successively, my useEffect only considers the last one. I think this is due to an optimization in React. My code works well on Chrome, but it doesn’t work in Safari. How can I fix this?
Code:
const { message, sendMessage, setUrl } = useWebSocket();
useEffect(() => {
console.log(message);
if (message && sessionJeu != true) {
setSessionJeu(true);
}
if (message.type == "end") {
setSessionJeu(false);
}
}, [message]);
Logs:
You can see what my WebSocket hook prints first and what the useEffect prints at the last line.
My code works in Chrome, so I’m sure that the error is in this part.
If necessary, I can provide more information. I hope this is clear.