mirror of
https://github.com/DrizzleTime/Foxel.git
synced 2026-09-05 15:47:00 +08:00
feat: refactor state management in VideoRoomPage to improve local state updates and WebSocket communication
This commit is contained in:
@@ -82,15 +82,28 @@ const VideoRoomPage = memo(function VideoRoomPage() {
|
|||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!token || !room || !artRef.current) return;
|
if (!token || !room || !artRef.current) return;
|
||||||
|
|
||||||
const sendState = () => {
|
const updateLocalState = () => {
|
||||||
const art = artInstance.current;
|
const art = artInstance.current;
|
||||||
const ws = wsRef.current;
|
if (!art || applyingRemoteRef.current) return null;
|
||||||
if (!art || !ws || ws.readyState !== WebSocket.OPEN || applyingRemoteRef.current) return;
|
|
||||||
const video = art.video;
|
const video = art.video;
|
||||||
const payload = {
|
const state: VideoRoomState = {
|
||||||
type: 'state',
|
|
||||||
current_time: video.currentTime || 0,
|
current_time: video.currentTime || 0,
|
||||||
paused: video.paused,
|
paused: video.paused,
|
||||||
|
updated_at: new Date().toISOString(),
|
||||||
|
};
|
||||||
|
liveStateRef.current = state;
|
||||||
|
setLiveState(state);
|
||||||
|
return state;
|
||||||
|
};
|
||||||
|
|
||||||
|
const sendState = () => {
|
||||||
|
const ws = wsRef.current;
|
||||||
|
const state = updateLocalState();
|
||||||
|
if (!state || !ws || ws.readyState !== WebSocket.OPEN) return;
|
||||||
|
const payload = {
|
||||||
|
type: 'state',
|
||||||
|
current_time: state.current_time,
|
||||||
|
paused: state.paused,
|
||||||
};
|
};
|
||||||
ws.send(JSON.stringify(payload));
|
ws.send(JSON.stringify(payload));
|
||||||
};
|
};
|
||||||
@@ -145,7 +158,6 @@ const VideoRoomPage = memo(function VideoRoomPage() {
|
|||||||
|
|
||||||
art.on('ready', applyLatestState);
|
art.on('ready', applyLatestState);
|
||||||
art.video.addEventListener('loadedmetadata', applyLatestState);
|
art.video.addEventListener('loadedmetadata', applyLatestState);
|
||||||
art.video.addEventListener('canplay', applyLatestState);
|
|
||||||
art.on('play', sendStateSoon);
|
art.on('play', sendStateSoon);
|
||||||
art.on('pause', sendStateSoon);
|
art.on('pause', sendStateSoon);
|
||||||
art.on('seek', sendStateSoon);
|
art.on('seek', sendStateSoon);
|
||||||
@@ -172,7 +184,6 @@ const VideoRoomPage = memo(function VideoRoomPage() {
|
|||||||
sendTimerRef.current = null;
|
sendTimerRef.current = null;
|
||||||
}
|
}
|
||||||
art.video.removeEventListener('loadedmetadata', applyLatestState);
|
art.video.removeEventListener('loadedmetadata', applyLatestState);
|
||||||
art.video.removeEventListener('canplay', applyLatestState);
|
|
||||||
ws.close();
|
ws.close();
|
||||||
art.destroy();
|
art.destroy();
|
||||||
wsRef.current = null;
|
wsRef.current = null;
|
||||||
|
|||||||
Reference in New Issue
Block a user