Add client leaving host notification
This commit is contained in:
parent
7eeda3e6ae
commit
3c54a0f4d9
3 changed files with 164 additions and 0 deletions
|
|
@ -966,6 +966,23 @@ export const useGame = () => {
|
|||
showScoreboard();
|
||||
}
|
||||
}
|
||||
|
||||
if (data.type === 'LEAVE') {
|
||||
const playerId = conn.peer;
|
||||
const updatedPlayers = playersRef.current.filter(p => p.id !== playerId);
|
||||
playersRef.current = updatedPlayers;
|
||||
setPlayers(updatedPlayers);
|
||||
connectionsRef.current.delete(playerId);
|
||||
|
||||
if (presenterIdRef.current === playerId) {
|
||||
const realPlayers = updatedPlayers.filter(p => p.id !== 'host');
|
||||
const newPresenter = realPlayers.length > 0 ? realPlayers[0].id : null;
|
||||
setPresenterId(newPresenter);
|
||||
broadcast({ type: 'PRESENTER_CHANGED', payload: { presenterId: newPresenter } });
|
||||
}
|
||||
|
||||
broadcast({ type: 'PLAYER_LEFT', payload: { playerId } });
|
||||
}
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
|
|
@ -1587,6 +1604,7 @@ export const useGame = () => {
|
|||
if (role !== 'CLIENT') return;
|
||||
|
||||
if (hostConnectionRef.current?.open) {
|
||||
hostConnectionRef.current.send({ type: 'LEAVE', payload: {} });
|
||||
hostConnectionRef.current.close();
|
||||
}
|
||||
hostConnectionRef.current = null;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue