Spaces:
Running on Zero
Running on Zero
Download static/js/ws.js from devsai1010/telegraqm-web-admin: direct link, hf CLI and curl.
- Browser
- Download file 5.47 kB
-
https://huggingface.co/spaces/devsai1010/telegraqm-web-admin/resolve/main/static/js/ws.js
- Command line
-
hf download hf://spaces/devsai1010/telegraqm-web-admin/static/js/ws.js
-
curl -L -o ws.js https://huggingface.co/spaces/devsai1010/telegraqm-web-admin/resolve/main/static/js/ws.js
5.47 kB
| // ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| // Live updates over WebSocket (server side: class Realtime + /ws in app.py). | |
| // | |
| // The socket carries *hints only* ("chat list changed", "chat X changed"). | |
| // On a hint we re-fetch through the normal ETag-cached REST calls, so there | |
| // is one source of truth and a dropped socket can never leave the UI wrong. | |
| // While the socket is healthy the old poller drops to a slow safety net | |
| // (see startPoller in index.html); when it isn't, polling carries on as before. | |
| // | |
| // Also drives the Telegram-style connection bar: | |
| // connecting β socket opening waiting β device has no network | |
| // updating β connected, catching up on what was missed | |
| // ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| var wsLive = false; // true while an authenticated socket is open | |
| (function () { | |
| const STR = { | |
| fa: { connecting: 'Ψ―Ψ± ΨΨ§Ω Ψ§ΨͺΨ΅Ψ§Ωβ¦', waiting: 'Ω ΩΨͺΨΈΨ± Ψ§ΨͺΨ΅Ψ§Ω Ψ¨Ω Ψ΄Ψ¨Ϊ©Ωβ¦', updating: 'Ψ―Ψ± ΨΨ§Ω Ψ¨ΩβΨ±ΩΨ²Ψ±Ψ³Ψ§ΩΫβ¦' }, | |
| en: { connecting: 'Connectingβ¦', waiting: 'Waiting for networkβ¦', updating: 'Updatingβ¦' }, | |
| ru: { connecting: 'ΠΠΎΠ΄ΠΊΠ»ΡΡΠ΅Π½ΠΈΠ΅β¦', waiting: 'ΠΠΆΠΈΠ΄Π°Π½ΠΈΠ΅ ΡΠ΅ΡΠΈβ¦', updating: 'ΠΠ±Π½ΠΎΠ²Π»Π΅Π½ΠΈΠ΅β¦' } | |
| }; | |
| let sock = null, tries = 0, retryTimer = null, watchdog = null, lastRx = 0, wanted = false, state = ''; | |
| function setState(s) { | |
| if (s === state) return; | |
| state = s; | |
| const bar = document.getElementById('connStatus'); | |
| if (!bar) return; | |
| const txt = (STR[document.documentElement.lang] || STR.fa)[s]; | |
| if (txt) document.getElementById('connText').textContent = txt; | |
| bar.classList.toggle('show', !!txt); | |
| } | |
| function url() { return (location.protocol === 'https:' ? 'wss://' : 'ws://') + location.host + '/ws'; } | |
| function scheduleRetry() { | |
| clearTimeout(retryTimer); | |
| if (!wanted) return; | |
| const base = Math.min(15000, 800 * Math.pow(2, tries++)); | |
| retryTimer = setTimeout(open, base + Math.random() * 400); | |
| } | |
| // Abandon the current socket without waiting for its close handshake β after a network | |
| // drop a half-open socket can sit in OPEN/CLOSING for a minute and would block reconnecting. | |
| function drop() { | |
| const s = sock; sock = null; wsLive = false; | |
| if (s) { s.onopen = s.onmessage = s.onclose = s.onerror = null; try { s.close(); } catch (e) {} } | |
| } | |
| function lost() { | |
| wsLive = false; | |
| clearInterval(watchdog); | |
| setState(navigator.onLine === false ? 'waiting' : 'connecting'); | |
| scheduleRetry(); | |
| } | |
| async function catchUp() { | |
| // anything that happened while we were disconnected | |
| setState('updating'); | |
| try { | |
| await Promise.all([fetchChats(true), curChatId ? fetchHistory(curChatId, true) : null]); | |
| } catch (e) { /* the poller will retry */ } | |
| if (wsLive) setState(''); | |
| } | |
| function open() { | |
| if (!wanted || !PASS) return; | |
| if (sock && (sock.readyState === 0 || sock.readyState === 1)) return; | |
| if (navigator.onLine === false) { setState('waiting'); return; } | |
| setState('connecting'); | |
| let s; | |
| try { s = new WebSocket(url()); } catch (e) { return scheduleRetry(); } | |
| sock = s; | |
| s.onopen = () => s.send(JSON.stringify({ t: 'auth', token: PASS })); | |
| s.onmessage = ev => { | |
| lastRx = Date.now(); | |
| let m; try { m = JSON.parse(ev.data); } catch (e) { return; } | |
| if (m.t === 'hello') { | |
| tries = 0; wsLive = true; | |
| clearInterval(watchdog); | |
| watchdog = setInterval(() => { if (Date.now() - lastRx > 70000) try { s.close(); } catch (e) {} }, 10000); | |
| catchUp(); | |
| } else if (m.t === 'dirty') { | |
| fetchChats(true); | |
| if (curChatId && (m.chat || []).includes(String(curChatId))) fetchHistory(curChatId, true); | |
| } else if (m.t === 'ping') { | |
| try { s.send('{"t":"pong"}'); } catch (e) {} | |
| } else if (m.t === 'unauthorized') { | |
| wanted = false; // session is gone: stop retrying, REST 401 handling signs the user out | |
| wsLive = false; setState(''); | |
| } | |
| }; | |
| s.onclose = () => { if (sock === s) { sock = null; lost(); } }; | |
| s.onerror = () => { try { s.close(); } catch (e) {} }; | |
| } | |
| // called once the user is signed in (startPoller) | |
| window.wsConnect = function () { wanted = true; open(); }; | |
| window.addEventListener('online', () => { if (!wanted) return; drop(); clearTimeout(retryTimer); tries = 0; open(); }); | |
| window.addEventListener('offline', () => { if (!wanted) return; drop(); clearInterval(watchdog); clearTimeout(retryTimer); setState('waiting'); }); | |
| document.addEventListener('visibilitychange', () => { | |
| if (document.hidden || !wanted) return; | |
| if (wsLive) catchUp(); // coming back to the app: make sure nothing was missed | |
| else { drop(); clearTimeout(retryTimer); tries = 0; open(); } | |
| }); | |
| })(); | |