mirror of
https://github.com/cpvalente/ontime.git
synced 2026-08-21 23:19:09 +00:00
bugfix
bugfix with socket client - client now subscribes to events - client gets playstate on start
This commit is contained in:
@@ -39,23 +39,32 @@ export default function PlaybackControl() {
|
|||||||
expectedFinish: null,
|
expectedFinish: null,
|
||||||
});
|
});
|
||||||
|
|
||||||
// Torbjorn: why is this not updating?
|
// handle incoming messages
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (socket == null) return;
|
if (socket == null) return;
|
||||||
|
|
||||||
|
// Subscribe to timer event
|
||||||
|
socket.emit('subscribe-to-timer');
|
||||||
|
|
||||||
|
// ask for playstate
|
||||||
|
socket.emit('get-playstate');
|
||||||
|
|
||||||
|
socket.on('playstate', (data) => {
|
||||||
|
setPlayback(data);
|
||||||
|
});
|
||||||
|
|
||||||
// Handle timer
|
// Handle timer
|
||||||
socket.on('timer', (data) => {
|
socket.on('timer', (data) => {
|
||||||
console.log('websocket: got data', data);
|
|
||||||
setTimer({ ...data });
|
setTimer({ ...data });
|
||||||
});
|
});
|
||||||
|
|
||||||
// Clear listener
|
// Clear listener
|
||||||
return () => socket.off('timer');
|
return () => {
|
||||||
|
socket.emit('release-timer');
|
||||||
|
socket.off('timer');
|
||||||
|
};
|
||||||
}, [socket]);
|
}, [socket]);
|
||||||
|
|
||||||
// TO SEND TO SOCKET HERE WE CAN USE
|
|
||||||
// socket.emit('test')
|
|
||||||
|
|
||||||
// TODO: Move to playback API
|
// TODO: Move to playback API
|
||||||
// Soould this go through sockets?
|
// Soould this go through sockets?
|
||||||
const playbackControl = async (action, payload) => {
|
const playbackControl = async (action, payload) => {
|
||||||
|
|||||||
+28
-10
@@ -52,26 +52,44 @@ const io = socketIo(server, {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Torbjorn: should the interval be here or inside the connection?
|
||||||
|
// I am guessing one interval per timer
|
||||||
// interval function
|
// interval function
|
||||||
let interval;
|
let interval;
|
||||||
|
|
||||||
io.on('connection', (socket) => {
|
io.on('connection', (socket) => {
|
||||||
console.log('New client connected');
|
console.log('New client connected');
|
||||||
|
|
||||||
// timer API
|
// let interval = null;
|
||||||
socket.emit('timer', timer.getObject());
|
|
||||||
|
|
||||||
// avoid multiple intervals
|
// subscribe to timer
|
||||||
if (interval) {
|
socket.on('subscribe-to-timer', () => {
|
||||||
clearInterval(interval);
|
console.log('New subscription');
|
||||||
}
|
// avoid multiple intervals
|
||||||
// set callback for timer events
|
if (interval) clearInterval(interval);
|
||||||
interval = setInterval(() => getApiAndEmit(socket), config.timer.refresh);
|
|
||||||
|
// send current data
|
||||||
|
socket.emit('timer', timer.getObject());
|
||||||
|
|
||||||
|
// set callback for timer events
|
||||||
|
interval = setInterval(() => emitTimer(socket), config.timer.refresh);
|
||||||
|
});
|
||||||
|
|
||||||
|
// unsubscribe to timer
|
||||||
|
socket.on('release-timer', () => {
|
||||||
|
console.log('Releasing subscription');
|
||||||
|
// avoid multiple intervals
|
||||||
|
if (interval) clearInterval(interval);
|
||||||
|
});
|
||||||
|
|
||||||
socket.on('get-timer', () => {
|
socket.on('get-timer', () => {
|
||||||
socket.emit('timer', timer.getObject());
|
socket.emit('timer', timer.getObject());
|
||||||
});
|
});
|
||||||
|
|
||||||
|
socket.on('get-playstate', () => {
|
||||||
|
socket.emit('playstate', timer.playState);
|
||||||
|
});
|
||||||
|
|
||||||
// playback API
|
// playback API
|
||||||
socket.on('set-presenter-text', (data) => {
|
socket.on('set-presenter-text', (data) => {
|
||||||
timer.presenterText = data;
|
timer.presenterText = data;
|
||||||
@@ -104,12 +122,12 @@ io.on('connection', (socket) => {
|
|||||||
// handle client disconnect
|
// handle client disconnect
|
||||||
socket.on('disconnect', () => {
|
socket.on('disconnect', () => {
|
||||||
console.log('Client disconnected');
|
console.log('Client disconnected');
|
||||||
clearInterval(interval);
|
if (interval) clearInterval(interval);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// send timer events
|
// send timer events
|
||||||
const getApiAndEmit = (socket) => {
|
const emitTimer = (socket) => {
|
||||||
// send current timer
|
// send current timer
|
||||||
socket.emit('timer', timer.getObject());
|
socket.emit('timer', timer.getObject());
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -88,6 +88,10 @@ class Timer {
|
|||||||
return Timer.toSeconds(this._current);
|
return Timer.toSeconds(this._current);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get playState() {
|
||||||
|
return this.state;
|
||||||
|
}
|
||||||
|
|
||||||
// playback
|
// playback
|
||||||
start() {
|
start() {
|
||||||
// do we need to change
|
// do we need to change
|
||||||
|
|||||||
Reference in New Issue
Block a user