51 lines
1.3 KiB
PHP
51 lines
1.3 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
require __DIR__ . '/../vendor/autoload.php';
|
|
|
|
use ComCen\Html\Html;
|
|
|
|
$html = new Html();
|
|
$html->content .= "
|
|
<!DOCTYPE html>
|
|
<html lang=\"en\">
|
|
<head>
|
|
<meta charset=\"UTF-8\">
|
|
<title>Ratchet Hello World</title>
|
|
</head>
|
|
<body>
|
|
<h2>WebSocket Test</h2>
|
|
<div id=\"log\"></div>
|
|
<input id=\"msg\" type=\"text\" placeholder=\"Type a message...\" />
|
|
<button onclick=\"send()\">Send</button>
|
|
<button onclick='closeConnection()'>Disconnect</button>
|
|
|
|
<script>
|
|
const log = document.getElementById('log');
|
|
const ws = new WebSocket('ws://localhost:8080');
|
|
|
|
ws.onopen = () => append('Connected');
|
|
ws.onmessage = e => append('Server: ' + e.data);
|
|
ws.onclose = () => append('Disconnected');
|
|
ws.onerror = e => append('Error: ' + e.message);
|
|
|
|
function send() {
|
|
const input = document.getElementById('msg');
|
|
ws.send(input.value);
|
|
append('You: ' + input.value);
|
|
input.value = '';
|
|
}
|
|
|
|
function append(text) {
|
|
const p = document.createElement('p');
|
|
p.textContent = text;
|
|
log.appendChild(p);
|
|
}
|
|
function closeConnection() {
|
|
ws.close();
|
|
}
|
|
</script>
|
|
</body>
|
|
</html>
|
|
";
|
|
$html->renderContent();
|