token system now fullly works

This commit is contained in:
GitProtogen
2026-03-06 17:05:54 +01:00
parent 14359b9de1
commit 51a94fea19
2 changed files with 112 additions and 30 deletions
+2 -2
View File
@@ -98,6 +98,7 @@ class WebSocketServer implements MessageComponentInterface
public function onError(ConnectionInterface $conn, \Exception $e): void public function onError(ConnectionInterface $conn, \Exception $e): void
{ {
echo "Error: {$e->getMessage()}\n"; echo "Error: {$e->getMessage()}\n";
$this->deleteGivenId($conn->resourceId);
$this->onClose($conn); $this->onClose($conn);
} }
} }
@@ -109,8 +110,7 @@ $routes->add("ws", new Route("/ws", ["_controller" => new WsServer(n
$server = IoServer::factory( $server = IoServer::factory(
new HttpServer(new Router(new UrlMatcher($routes, new RequestContext()))), new HttpServer(new Router(new UrlMatcher($routes, new RequestContext()))),
8080, 8080
"0.0.0.0"
); );
Handler::getInstance()->init(); Handler::getInstance()->init();
+110 -28
View File
@@ -5,46 +5,128 @@ require __DIR__ . '/../vendor/autoload.php';
use ComCen\Html\Html; use ComCen\Html\Html;
$html = new Html(); $html = new Html();
$html->content .= " $html->content = <<<'HTML'
<!DOCTYPE html> <!DOCTYPE html>
<html lang=\"en\"> <html lang="en">
<head> <head>
<meta charset=\"UTF-8\"> <meta charset="UTF-8">
<title>Chat</title> <title>ComCen</title>
<style>
body { font-family: monospace; max-width: 600px; margin: 40px auto; padding: 0 16px; }
#log { border: 1px solid #ccc; height: 300px; overflow-y: auto; padding: 8px; margin-bottom: 8px; background: #f9f9f9; }
.log-entry { margin: 2px 0; }
.log-system { color: #888; }
.log-error { color: #c00; }
input, button { font-family: monospace; font-size: 14px; }
#token { width: 100%; box-sizing: border-box; margin-bottom: 8px; padding: 4px; }
#msg-row { display: flex; gap: 8px; }
#message { flex: 1; padding: 4px; }
button { padding: 4px 12px; cursor: pointer; }
button:disabled { cursor: not-allowed; opacity: 0.5; }
</style>
</head> </head>
<body> <body>
<h2>WebSocket Test</h2> <h2>ComCen</h2>
<div id=\"log\"></div>
<input id=\"msg\" type=\"text\" placeholder=\"Type a message...\" /> <input id="token" type="text" placeholder="Paste token from curl login/register here" />
<button onclick=\"send()\">Send</button> <div style="display:flex; gap:8px; margin-bottom:8px;">
<button onclick='closeConnection()'>Disconnect</button> <button id="connect-btn" onclick="connect()">Connect</button>
<button id="disconnect-btn" onclick="disconnect()" disabled>Disconnect</button>
</div>
<div id="log"></div>
<div id="msg-row">
<input id="message" type="text" placeholder="Message..." disabled onkeydown="if(event.key==='Enter') send()" />
<button id="send-btn" onclick="send()" disabled>Send</button>
</div>
<script> <script>
const log = document.getElementById('log'); let ws = null;
const ws = new WebSocket('ws://localhost:8080/ws'); let authenticated = false;
ws.onopen = () => append('Connected'); function log(text, type = '') {
ws.onmessage = e => append('Server: ' + e.data); const log = document.getElementById('log');
ws.onclose = () => append('Disconnected'); const div = document.createElement('div');
ws.onerror = e => append('Error: ' + e.message); div.className = 'log-entry' + (type ? ' log-' + type : '');
div.textContent = text;
log.appendChild(div);
log.scrollTop = log.scrollHeight;
}
function setConnected(connected) {
document.getElementById('connect-btn').disabled = connected;
document.getElementById('disconnect-btn').disabled = !connected;
}
function setAuthenticated(auth) {
authenticated = auth;
document.getElementById('message').disabled = !auth;
document.getElementById('send-btn').disabled = !auth;
}
function connect() {
const token = document.getElementById('token').value.trim();
if (!token) {
log('Enter a token first.', 'error');
return;
}
ws = new WebSocket('ws://localhost:8080/ws');
ws.onopen = () => {
log('Connected. Authenticating...', 'system');
setConnected(true);
ws.send(JSON.stringify({ token }));
};
ws.onmessage = (event) => {
const text = event.data;
if (text === 'authenticated') {
log('Authenticated.', 'system');
setAuthenticated(true);
} else if (text === 'invalid token') {
log('Invalid token. Connection closed.', 'error');
ws.close();
} else if (text === 'not authenticated') {
log('Server: not authenticated', 'error');
} else {
try {
const data = JSON.parse(text);
log(`${data.sender}: ${data.msg}`);
} catch {
log(text);
}
}
};
ws.onclose = () => {
log('Disconnected.', 'system');
setConnected(false);
setAuthenticated(false);
ws = null;
};
ws.onerror = () => {
log('WebSocket error.', 'error');
};
}
function disconnect() {
if (ws) ws.close();
}
function send() { function send() {
const input = document.getElementById('msg'); const input = document.getElementById('message');
ws.send(input.value); const text = input.value.trim();
append('You: ' + input.value); if (!text || !ws || !authenticated) return;
ws.send(JSON.stringify({ message: text }));
log(`You: ${text}`);
input.value = ''; input.value = '';
} }
function append(text) {
const p = document.createElement('p');
p.textContent = text;
log.appendChild(p);
}
function closeConnection() {
ws.close();
}
</script> </script>
</body> </body>
</html> </html>
";
HTML;
$html->renderContent(); $html->renderContent();