[mirotalksfu] - refactoring

هذا الالتزام موجود في:
Miroslav Pejic
2024-10-21 14:52:25 +02:00
الأصل 6d54f0df09
التزام dbfb374bf5

عرض الملف

@@ -4,81 +4,86 @@ console.log(window.location);
const mediaQuery = window.matchMedia('(max-width: 640px)'); const mediaQuery = window.matchMedia('(max-width: 640px)');
const settings = JSON.parse(localStorage.getItem('SFU_SETTINGS')); const settings = JSON.parse(localStorage.getItem('SFU_SETTINGS')) || {};
console.log('Settings:', settings);
console.log('Settings', settings); const autoJoinRoom = false; // Automatically join the guest to the meeting
const intervalTime = 5000; // Interval to check room status
const autoJoinRoom = false; // automatically join the guest to the meeting
const intervalTime = 5000; // check room status every 5 seconds
const presenterLoginBtn = document.getElementById('presenterLoginButton'); const presenterLoginBtn = document.getElementById('presenterLoginButton');
const guestJoinRoomBtn = document.getElementById('guestJoinRoomButton'); const guestJoinRoomBtn = document.getElementById('guestJoinRoomButton');
// Disable the guest join button initially
guestJoinRoomBtn.classList.add('disabled'); guestJoinRoomBtn.classList.add('disabled');
// Extract room ID from URL path using XSS filtering
const pathParts = window.location.pathname.split('/'); const pathParts = window.location.pathname.split('/');
const roomId = filterXSS(pathParts[pathParts.length - 1]); const roomId = filterXSS(pathParts[pathParts.length - 1]);
let intervalId; let intervalId = null;
let roomActive = false; let roomActive = false;
presenterLoginBtn.onclick = () => { // Button event handlers
presenterLoginBtn.addEventListener('click', () => {
window.location.href = '/login'; window.location.href = '/login';
}; });
guestJoinRoomBtn.onclick = () => { guestJoinRoomBtn.addEventListener('click', () => {
window.location.href = '/join/' + roomId; window.location.href = `/join/${roomId}`;
}; });
function sound(name) { // Function to play sound
function playSound(name) {
if (!settings.sounds) return; if (!settings.sounds) return;
const sound = '../sounds/' + name + '.wav';
const audio = new Audio(sound); const soundSrc = `../sounds/${name}.wav`;
const audio = new Audio(soundSrc);
audio.volume = 0.5; audio.volume = 0.5;
audio.play().catch((err) => { audio.play().catch((err) => {
return false; console.error(`Error playing sound: ${err}`);
}); });
} }
// Handle screen resize to adjust presenter login button visibility
function handleScreenResize(e) { function handleScreenResize(e) {
if (roomActive) return; if (!roomActive) {
presenterLoginBtn.style.display = e.matches ? 'flex' : 'inline-flex'; presenterLoginBtn.style.display = e.matches ? 'flex' : 'inline-flex';
}
} }
function checkRoomStatus(roomId) { // Function to check room status from the server
async function checkRoomStatus(roomId) {
if (!roomId) { if (!roomId) {
console.warn('Room ID empty!'); console.warn('Room ID is empty!');
return; return;
} }
axios
.post('/isRoomActive', { roomId }) try {
.then((response) => { const response = await axios.post('/isRoomActive', { roomId });
console.log('isRoomActive', response.data); const isActive = response.data.message;
roomActive = response.data.message; console.log('Room active status:', isActive);
if (roomActive) {
sound('roomActive'); roomActive = isActive;
guestJoinRoomBtn.classList.remove('disabled'); if (roomActive) {
presenterLoginBtn.style.display = 'none'; playSound('roomActive');
if (autoJoinRoom) guestJoinRoomBtn.click(); guestJoinRoomBtn.classList.remove('disabled');
} else { presenterLoginBtn.style.display = 'none';
guestJoinRoomBtn.classList.add('disabled');
handleScreenResize(mediaQuery); if (autoJoinRoom) {
guestJoinRoomBtn.click();
} }
}) } else {
.catch((error) => { guestJoinRoomBtn.classList.add('disabled');
console.error('Error checking room status', error); handleScreenResize(mediaQuery);
}); }
} catch (error) {
console.error('Error checking room status:', error);
}
} }
handleScreenResize(mediaQuery); // Start interval to check room status every 5 seconds
function startRoomStatusCheck() {
checkRoomStatus(roomId);
mediaQuery.addEventListener('change', handleScreenResize);
function startCheckingRoomStatus() {
// Function to run every 5 seconds
intervalId = setInterval(() => { intervalId = setInterval(() => {
if (document.visibilityState === 'visible') { if (document.visibilityState === 'visible') {
checkRoomStatus(roomId); checkRoomStatus(roomId);
@@ -86,27 +91,33 @@ function startCheckingRoomStatus() {
}, intervalTime); }, intervalTime);
} }
// Fallback to setTimeout if needed for better control // Fallback to setTimeout for room status checks
function fallbackCheckRoomStatus() { function fallbackRoomStatusCheck() {
if (document.visibilityState === 'visible') { if (document.visibilityState === 'visible') {
checkRoomStatus(roomId); checkRoomStatus(roomId);
} }
setTimeout(fallbackCheckRoomStatus, intervalTime); setTimeout(fallbackRoomStatusCheck, intervalTime);
} }
// Use Page Visibility API to pause/resume the checks // Page visibility change handler to pause or resume status checks
document.addEventListener('visibilitychange', () => { function handleVisibilityChange() {
checkRoomStatus(roomId);
//
if (document.visibilityState === 'visible') { if (document.visibilityState === 'visible') {
console.log('Page is visible. Resuming room status checks.'); console.log('Page is visible. Resuming room status checks.');
if (!intervalId) startCheckingRoomStatus(); checkRoomStatus(roomId);
if (!intervalId) startRoomStatusCheck();
} else { } else {
console.log('Page is hidden. Pausing room status checks.'); console.log('Page is hidden. Pausing room status checks.');
clearInterval(intervalId); clearInterval(intervalId);
intervalId = null; intervalId = null;
} }
}); }
// Initialize event listeners
mediaQuery.addEventListener('change', handleScreenResize);
document.addEventListener('visibilitychange', handleVisibilityChange);
// Start checking room status on page load
handleScreenResize(mediaQuery);
checkRoomStatus(roomId);
startRoomStatusCheck();
// Start checking room status when the page is first loaded
startCheckingRoomStatus();