diff --git a/app/api/socketio/route.js b/app/api/socketio/route.js index e7e33a1..cf19177 100644 --- a/app/api/socketio/route.js +++ b/app/api/socketio/route.js @@ -45,7 +45,7 @@ function broadcastUpdate(eventId, io) { io.to(sid).emit("queue_update", { activeCount: ev.sockets.size, position: pos === -1 ? null : pos + 1, - estimatedWait: pos === -1 ? null : pos * TOKEN_TTL_SECONDS, + estimatedWait: pos === -1 ? null : (ev.active.size + pos) * TOKEN_TTL_SECONDS, }) } } @@ -84,6 +84,34 @@ async function evaluateQueue(eventId, io) { io.to(next).emit("granted", { token, expiresAt: expiresAt.toISOString() }) } + // Check for expired tokens in database and notify clients + if (connection) { + try { + const [expiredSessions] = await connection.execute( + 'SELECT socket_id FROM active_sessions WHERE event_id = ? AND expires_at < NOW()', + [eventId] + ) + + for (const session of expiredSessions) { + const sid = session.socket_id + if (ev.active.has(sid)) { + ev.active.delete(sid) + io.to(sid).emit("token_expired") + } + } + + // Clean up expired sessions from database + if (expiredSessions.length > 0) { + await connection.execute( + 'DELETE FROM active_sessions WHERE event_id = ? AND expires_at < NOW()', + [eventId] + ) + } + } catch (error) { + console.error('DB error checking expired tokens:', error) + } + } + // If too many active (rare), revoke oldest if (ev.active.size > CONCURRENT_ACTIVE) { const toRevoke = Array.from(ev.active).slice(CONCURRENT_ACTIVE) @@ -135,6 +163,13 @@ export async function GET(req) { global.io = io + // Periodikus token ellenőrzés minden 5 másodpercben + setInterval(async () => { + for (const eventId of Object.keys(events)) { + await evaluateQueue(eventId, io) + } + }, 5000) + io.on("connection", (socket) => { console.log("Csatlakozott:", socket.id) @@ -226,7 +261,7 @@ export async function GET(req) { io.to(socket.id).emit("queue_update", { activeCount: ev.sockets.size, position: pos === -1 ? null : pos + 1, - estimatedWait: pos === -1 ? null : pos * TOKEN_TTL_SECONDS, + estimatedWait: pos === -1 ? null : (ev.active.size + pos) * TOKEN_TTL_SECONDS, }) }) diff --git a/app/event/[id]/page.tsx b/app/event/[id]/page.tsx index 64a673a..e575aa0 100644 --- a/app/event/[id]/page.tsx +++ b/app/event/[id]/page.tsx @@ -8,6 +8,13 @@ export default function EventPage() { const router = useRouter(); const eventId = params.id as string; + // Helper function to format seconds to HH:MM format + const formatTime = (seconds: number): string => { + const hours = Math.floor(seconds / 3600); + const minutes = Math.floor((seconds % 3600) / 60); + return `${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}`; + }; + const [connected, setConnected] = useState(false); const [position, setPosition] = useState(null); const [estimatedWait, setEstimatedWait] = useState(null); @@ -94,6 +101,14 @@ export default function EventPage() { setTokenExpiry(null); localStorage.removeItem("event_token"); }); + + socket.on("token_expired", () => { + setHasAccess(false); + setTokenExpiry(null); + localStorage.removeItem("event_token"); + // Redirect to homepage + window.location.href = "/"; + }); }) .catch(error => { console.error("Socket initialization error:", error); @@ -105,7 +120,7 @@ export default function EventPage() { }; }, [eventId, loading]); - // Token expiry timer + // Token expiry timer - ellenőrzés minden másodpercben useEffect(() => { if (!tokenExpiry) return; const id = setInterval(() => { @@ -114,6 +129,11 @@ export default function EventPage() { setHasAccess(false); setTokenExpiry(null); localStorage.removeItem("event_token"); + // Disconnect socket and redirect to homepage + if (socketRef.current) { + socketRef.current.disconnect(); + } + window.location.href = "/"; } }, 1000); return () => clearInterval(id); @@ -212,7 +232,7 @@ export default function EventPage() {

Helyed a várakozási sorban

{estimatedWait && (

- Becsült várakozási idő: {Math.ceil(estimatedWait)}s + Becsült várakozási idő: {formatTime(Math.ceil(estimatedWait))}

)} @@ -242,7 +262,7 @@ export default function EventPage() {
- {estimatedWait ? `${Math.ceil(estimatedWait)}s` : '—'} + {estimatedWait ? formatTime(Math.ceil(estimatedWait)) : '—'}
Várható idő