Spaces:
Runtime error
Runtime error
File size: 5,742 Bytes
9c72a28 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 | let socket;
let username;
let sessionToken;
let nextId = 0;
let pendingMessages = {};
async function init() {
try {
const res = await fetch("/api/session");
const data = await res.json();
if (data.authenticated) {
username = data.username;
sessionToken = data.token;
showChat();
connectWebSocket();
} else {
showLogin();
}
} catch {
showLogin();
}
}
function showLogin() {
document.getElementById("auth-container").style.display = "block";
document.getElementById("login-form").style.display = "block";
document.getElementById("signup-form").style.display = "none";
document.getElementById("chat-app").style.display = "none";
document.getElementById("login-error").textContent = "";
}
function showSignup() {
document.getElementById("auth-container").style.display = "block";
document.getElementById("login-form").style.display = "none";
document.getElementById("signup-form").style.display = "block";
document.getElementById("chat-app").style.display = "none";
document.getElementById("signup-error").textContent = "";
}
function showChat() {
document.getElementById("auth-container").style.display = "none";
document.getElementById("chat-app").style.display = "block";
document.getElementById("display-username").textContent = username;
document.getElementById("chat-window").innerHTML = "";
}
async function handleLogin(e) {
e.preventDefault();
const login = e.target.login.value;
const password = e.target.password.value;
const error = document.getElementById("login-error");
try {
const res = await fetch("/api/login", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ login, password }),
});
const data = await res.json();
if (res.ok) {
username = data.username;
sessionToken = data.token;
showChat();
connectWebSocket();
} else {
error.textContent = data.error;
e.target.password.value = "";
}
} catch {
error.textContent = "Network error.";
}
}
async function handleSignup(e) {
e.preventDefault();
const email = e.target.email.value;
const usernameVal = e.target.username.value;
const password = e.target.password.value;
const confirm = e.target.confirm_password.value;
const error = document.getElementById("signup-error");
if (password !== confirm) {
error.textContent = "Passwords do not match.";
return;
}
try {
const res = await fetch("/api/signup", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ email, username: usernameVal, password }),
});
const data = await res.json();
if (res.ok) {
username = data.username;
sessionToken = data.token;
showChat();
connectWebSocket();
} else {
error.textContent = data.error;
}
} catch {
error.textContent = "Network error.";
}
}
async function logout() {
if (socket) { socket.close(); socket = null; }
try { await fetch("/api/logout", { method: "POST" }); } catch {}
username = "";
sessionToken = "";
showLogin();
}
function connectWebSocket() {
const protocol = window.location.protocol === "https:" ? "wss:" : "ws:";
const url = protocol + "//" + window.location.host + "/ws?token=" + encodeURIComponent(sessionToken);
socket = new WebSocket(url);
socket.onmessage = function (event) {
const data = JSON.parse(event.data);
const now = Date.now();
const win = document.getElementById("chat-window");
const el = document.createElement("div");
if (data.is_system) {
el.style.fontStyle = "italic";
el.style.color = "#ffcc00";
el.textContent = "[System] " + data.msg;
} else {
let text = data.user + ": " + data.msg;
if (data.user === username && pendingMessages[data.id] !== undefined) {
const rtt = now - pendingMessages[data.id];
text += " (rtt: " + rtt + "ms)";
delete pendingMessages[data.id];
} else if (data.server_time) {
const sp = now - data.server_time;
text += " (sping: " + sp + "ms)";
}
el.textContent = text;
}
win.appendChild(el);
win.scrollTop = win.scrollHeight;
};
socket.onclose = function (event) {
if (event.code === 4002) {
location.reload();
return;
}
const win = document.getElementById("chat-window");
if (win) {
const el = document.createElement("div");
el.style.fontStyle = "italic";
el.style.color = "#e94560";
if (event.code === 4001) {
el.textContent = "Session expired. Logging out...";
setTimeout(() => { logout(); }, 1500);
} else {
el.textContent = "Connection lost. Refresh the page.";
}
win.appendChild(el);
}
};
}
function sendMessage() {
if (!socket || socket.readyState !== WebSocket.OPEN) return;
const input = document.getElementById("message-input");
const msg = input.value.trim();
if (!msg) return;
const id = nextId++;
pendingMessages[id] = Date.now();
socket.send(JSON.stringify({ msg, id }));
input.value = "";
input.focus();
}
init();
|