fix: Resolve WebSocket authentication error, add verifyJWT alias, and implement 20s heartbeat keep-alive for permanent stable connections

This commit is contained in:
Hamza-Ayed
2026-08-27 03:39:57 +03:00
parent 596f5e6603
commit b11a4bd8c1
4 changed files with 53 additions and 10 deletions
+22 -3
View File
@@ -877,21 +877,34 @@ class StudentPortal
return `${m}:${s}`;
}
let wsPingInterval = null;
// 1. Initialize Real-time WebSocket (Workerman)
function initWebSocket(token) {
if (!token) return;
const wsProtocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
const wsUrl = `${wsProtocol}//${window.location.host}/ws`;
if (wsSocket && (wsSocket.readyState === WebSocket.OPEN || wsSocket.readyState === WebSocket.CONNECTING)) {
return;
}
try {
wsSocket = new WebSocket(wsUrl);
wsSocket.onopen = () => {
updateWsStatus(true);
wsSocket.send(JSON.stringify({
event: 'auth',
data: { token: token }
}));
// Heartbeat keep-alive every 20s
clearInterval(wsPingInterval);
wsPingInterval = setInterval(() => {
if (wsSocket && wsSocket.readyState === WebSocket.OPEN) {
wsSocket.send(JSON.stringify({ event: 'ping' }));
}
}, 20000);
};
wsSocket.onmessage = (event) => {
@@ -905,7 +918,11 @@ class StudentPortal
wsSocket.onclose = () => {
updateWsStatus(false);
setTimeout(() => initWebSocket(token), 3000);
clearInterval(wsPingInterval);
setTimeout(() => {
const currentToken = localStorage.getItem('saqel_student_jwt');
if (currentToken) initWebSocket(currentToken);
}, 5000);
};
wsSocket.onerror = (err) => {
@@ -933,7 +950,9 @@ class StudentPortal
const event = msg.event;
const data = msg.data;
if (event === 'chat_message' || event === 'chat_sent') {
if (event === 'authenticated') {
updateWsStatus(true);
} else if (event === 'chat_message' || event === 'chat_sent') {
appendChatMessage(data);
} else if (event === 'drm_session_terminated') {
alert('⚠️ تم فتح هذا الحساب من متصفح آخر.');