'use client'; import React, { useState, useEffect } from 'react'; import { Button } from '@/components/ui/button'; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'; import { Badge } from '@/components/ui/badge'; import { Alert, AlertDescription } from '@/components/ui/alert'; import { Loader2, Database, Trash2, Eye, Zap } from 'lucide-react'; import { clientCleanup } from '@/lib/client-connection-cleanup'; export default function TestIPCleanupPage() { const [status, setStatus] = useState(null); const [loading, setLoading] = useState(false); const [message, setMessage] = useState(''); const [error, setError] = useState(''); // 獲取連線狀態 const fetchStatus = async () => { try { setLoading(true); const status = await clientCleanup.getConnectionStatus(); setStatus(status); setMessage('連線狀態更新成功'); setError(''); } catch (err) { setError('獲取狀態失敗: ' + (err instanceof Error ? err.message : '未知錯誤')); } finally { setLoading(false); } }; // 手動清理 const manualCleanup = async () => { try { setLoading(true); const success = await clientCleanup.manualCleanup(); if (success) { setMessage('手動清理完成'); setError(''); await fetchStatus(); // 重新獲取狀態 } else { setError('手動清理失敗'); } } catch (err) { setError('手動清理錯誤: ' + (err instanceof Error ? err.message : '未知錯誤')); } finally { setLoading(false); } }; // 模擬關閉頁面 const simulatePageClose = () => { if (confirm('確定要模擬關閉頁面嗎?這會觸發自動清理機制。')) { // 觸發 beforeunload 事件 window.dispatchEvent(new Event('beforeunload')); // 等待一下再觸發 unload 事件 setTimeout(() => { window.dispatchEvent(new Event('unload')); }, 100); } }; // 組件載入時獲取狀態 useEffect(() => { fetchStatus(); }, []); return (

IP 連線清理測試

測試基於 IP 的智能連線清理功能

{/* 客戶端信息 */} 客戶端信息

客戶端 ID: {clientCleanup.getClientId()}

用戶代理: {navigator.userAgent}

當前時間: {new Date().toLocaleString()}

{/* 連線狀態 */} {status && ( 連線狀態 IP: {status.ip} | 連線數: {status.connectionCount}
連線數: {status.connectionCount} 5 ? "destructive" : "default"} className="text-lg px-3 py-1" > 狀態: {status.connectionCount > 5 ? "異常" : "正常"}
{status.connections && status.connections.length > 0 && (

連線詳情:

{status.connections.slice(0, 5).map((conn: any, index: number) => (

ID: {conn.ID} | HOST: {conn.HOST} | 時間: {conn.TIME}s

))} {status.connections.length > 5 && (

... 還有 {status.connections.length - 5} 個連線

)}
)}
)} {/* 測試操作 */} 測試操作 測試各種連線清理功能
{/* 自動清理說明 */} 自動清理機制

頁面關閉前: 自動清理當前 IP 的所有連線

頁面隱藏時: 當切換到其他標籤頁時清理連線

定期清理: 每5分鐘檢查並清理多餘連線

手動清理: 可以隨時手動觸發清理

智能識別: 只清理當前 IP 的連線,不影響其他用戶

{/* 訊息顯示 */} {message && ( {message} )} {error && ( {error} )}
); }