Files
go-socket/machine-client/index.html
T

292 lines
11 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>WS Client</title>
<style>
* { box-sizing: border-box; margin: 0; padding: 0; }
body { font-family: monospace; background: #1a1a1a; color: #e0e0e0; padding: 20px; }
h2 { margin-bottom: 16px; color: #9ecfff; }
#auth-panel, #chat-panel { max-width: 600px; }
label { display: block; margin-bottom: 4px; font-size: 13px; color: #aaa; }
input[type="text"] {
width: 100%; padding: 8px; background: #2a2a2a; border: 1px solid #444;
color: #e0e0e0; font-family: monospace; font-size: 13px; border-radius: 4px;
}
button {
margin-top: 10px; padding: 8px 20px; background: #4a90d9; color: #fff;
border: none; border-radius: 4px; cursor: pointer; font-family: monospace; font-size: 13px;
}
button:hover { background: #357abd; }
button:disabled { background: #444; cursor: default; }
#status {
margin-top: 12px; font-size: 13px; padding: 6px 10px;
border-radius: 4px; background: #2a2a2a; border-left: 3px solid #555;
}
#status.ok { border-color: #4caf50; color: #81c784; }
#status.err { border-color: #f44336; color: #e57373; }
#chat-panel { display: none; margin-top: 24px; }
#log {
height: 300px; overflow-y: auto; background: #111; border: 1px solid #333;
border-radius: 4px; padding: 10px; font-size: 13px; line-height: 1.6;
}
.msg-in { color: #81c784; }
.msg-out { color: #64b5f6; }
.msg-sys { color: #888; font-style: italic; }
#send-row { display: flex; gap: 8px; margin-top: 10px; }
#send-row input { flex: 1; margin: 0; }
#send-row button { margin: 0; }
input[type="password"] {
width: 100%; padding: 8px; background: #2a2a2a; border: 1px solid #444;
color: #e0e0e0; font-family: monospace; font-size: 13px; border-radius: 4px;
}
.tabs { display: flex; gap: 8px; margin-bottom: 16px; }
.tab-btn {
padding: 6px 16px; background: #2a2a2a; border: 1px solid #444;
color: #aaa; border-radius: 4px; cursor: pointer; font-family: monospace; font-size: 13px;
}
.tab-btn.active { background: #4a90d9; color: #fff; border-color: #4a90d9; }
.tab { display: none; }
.tab.active { display: block; }
#form-status {
margin-top: 10px; font-size: 13px; padding: 6px 10px;
border-radius: 4px; background: #2a2a2a; border-left: 3px solid #555; display: none;
}
#form-status.ok { border-color: #4caf50; color: #81c784; display: block; }
#form-status.err { border-color: #f44336; color: #e57373; display: block; }
</style>
</head>
<body>
<div id="auth-panel">
<h2>WebSocket Client</h2>
<div class="tabs">
<button class="tab-btn active" onclick="switchTab('login')">Login</button>
<button class="tab-btn" onclick="switchTab('register')">Register</button>
<button class="tab-btn" onclick="switchTab('connect')">Connect</button>
<button class="tab-btn" onclick="switchTab('group')">Group</button>
</div>
<div id="tab-login" class="tab active">
<label>Username</label>
<input id="login-username" type="text" placeholder="alice">
<label style="margin-top:10px;">Password</label>
<input id="login-password" type="text" placeholder="password">
<button onclick="login()">Login</button>
</div>
<div id="tab-register" class="tab">
<label>Username</label>
<input id="reg-username" type="text" placeholder="alice">
<label style="margin-top:10px;">Password</label>
<input id="reg-password" type="text" placeholder="password">
<label style="margin-top:10px;">Color (r,g,b)</label>
<input id="reg-color" type="text" placeholder="255,100,50">
<button onclick="register()">Register</button>
</div>
<div id="tab-connect" class="tab">
<label>Username (for display)</label>
<input id="username-input" type="text" placeholder="Your username">
<label style="margin-top:10px;">JWT Token</label>
<input id="token-input" type="text" placeholder="Paste token from login">
<button id="connect-btn" onclick="connect()">Connect</button>
</div>
<div id="tab-group" class="tab">
<label>Token</label>
<input id="group-token" type="text" placeholder="Paste token from login">
<label style="margin-top:10px;">Name (optional)</label>
<input id="group-name" type="text" placeholder="Best group ever">
<label style="margin-top:10px;">Color (r,g,b or red/green/blue)</label>
<input id="group-color" type="text" placeholder="255,100,50">
<label style="margin-top:10px;">Enable client colors</label>
<input id="group-client-colors" type="text" placeholder="1 or 0">
<button onclick="createGroup()">Create Group</button>
</div>
<div id="form-status"></div>
<div id="status">Not connected</div>
</div>
<div id="chat-panel">
<h2>Connected as <span id="auth-name"></span></h2>
<div id="log"></div>
<div id="send-row">
<input id="msg-input" type="text" placeholder="Message..." onkeydown="if(event.key==='Enter') send()">
<button id="send-btn" onclick="send()">Send</button>
</div>
<button onclick="disconnect()" style="background:#c0392b; margin-top:10px;">Disconnect</button>
</div>
<script>
let ws = null;
function switchTab(name) {
document.querySelectorAll('.tab').forEach(t => t.classList.remove('active'));
document.querySelectorAll('.tab-btn').forEach(b => b.classList.remove('active'));
document.getElementById('tab-' + name).classList.add('active');
document.querySelectorAll('.tab-btn').forEach(b => {
if (b.textContent.toLowerCase() === name) b.classList.add('active');
});
setFormStatus('', '');
}
function setFormStatus(text, cls) {
const el = document.getElementById('form-status');
el.textContent = text;
el.className = cls;
}
async function register() {
const username = document.getElementById('reg-username').value.trim();
const password = document.getElementById('reg-password').value;
const color = document.getElementById('reg-color').value.trim();
if (!username || !password || !color) { setFormStatus('Fill all fields', 'err'); return; }
const body = new URLSearchParams({ username, password, color });
try {
const res = await fetch('http://localhost:8080/new/client', { method: 'POST', body });
const text = await res.text();
if (res.ok) {
setFormStatus('Registered! Now login.', 'ok');
} else {
setFormStatus('Error: ' + text, 'err');
}
} catch (e) {
setFormStatus('Request failed: ' + e.message, 'err');
}
}
async function login() {
const username = document.getElementById('login-username').value.trim();
const password = document.getElementById('login-password').value;
if (!username || !password) { setFormStatus('Fill all fields', 'err'); return; }
const body = new URLSearchParams({ username, password });
try {
const res = await fetch('http://localhost:8080/new/token', { method: 'POST', body });
const text = await res.text();
if (res.ok) {
document.getElementById('username-input').value = username;
document.getElementById('token-input').value = text;
document.getElementById('group-token').value = text;
setFormStatus('Logged in! Token copied to Connect tab.', 'ok');
switchTab('connect');
} else {
setFormStatus('Error: ' + text, 'err');
}
} catch (e) {
setFormStatus('Request failed: ' + e.message, 'err');
}
}
async function createGroup() {
const token = document.getElementById('group-token').value.trim();
if (!token) { setFormStatus('Paste a token first', 'err'); return; }
const body = new URLSearchParams({ token });
const name = document.getElementById('group-name').value.trim();
if (name) body.set('name', name);
const color = document.getElementById('group-color').value.trim();
if (color) body.set('color', color);
const enableClientColors = document.getElementById('group-client-colors').value.trim();
if (enableClientColors) body.set('enableClientColors', enableClientColors);
try {
const res = await fetch('http://localhost:8080/new/group', { method: 'POST', body });
if (res.ok) {
const buf = await res.arrayBuffer();
const id = new DataView(buf).getUint32(0, false);
setFormStatus('Group created! ID: ' + id, 'ok');
} else {
setFormStatus('Error: ' + await res.text(), 'err');
}
} catch (e) {
setFormStatus('Request failed: ' + e.message, 'err');
}
}
function setStatus(text, cls) {
const el = document.getElementById('status');
el.textContent = text;
el.className = cls || '';
}
function log(text, cls) {
const el = document.getElementById('log');
const line = document.createElement('div');
line.className = cls || '';
line.textContent = text;
el.appendChild(line);
el.scrollTop = el.scrollHeight;
}
function connect() {
const token = document.getElementById('token-input').value.trim();
if (!token) { setStatus('Paste a token first', 'err'); return; }
const username = document.getElementById('username-input').value.trim() || '?';
document.getElementById('connect-btn').disabled = true;
setStatus('Connecting...');
ws = new WebSocket('ws://localhost:8080/ws');
ws.onopen = () => {
ws.send(JSON.stringify({ token }));
document.getElementById('auth-name').textContent = username;
document.getElementById('auth-panel').style.display = 'none';
document.getElementById('chat-panel').style.display = 'block';
log('Authenticated as ' + username, 'msg-sys');
};
ws.onmessage = (e) => {
let data;
try { data = JSON.parse(e.data); } catch { log('← ' + e.data, 'msg-in'); return; }
if (data.error) {
log('Error: ' + data.error, 'msg-sys');
} else {
log('← ' + JSON.stringify(data), 'msg-in');
}
};
ws.onerror = () => {
setStatus('Connection error', 'err');
document.getElementById('connect-btn').disabled = false;
};
ws.onclose = () => {
log('Disconnected', 'msg-sys');
document.getElementById('chat-panel').style.display = 'none';
document.getElementById('auth-panel').style.display = 'block';
document.getElementById('connect-btn').disabled = false;
setStatus('Disconnected');
ws = null;
};
}
function send() {
const input = document.getElementById('msg-input');
const text = input.value.trim();
if (!text || !ws) return;
ws.send(JSON.stringify({ message: text }));
log('→ ' + text, 'msg-out');
input.value = '';
}
function disconnect() {
if (ws) ws.close();
}
</script>
</body>
</html>