ws works for testing
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
require __DIR__ . '/../vendor/autoload.php';
|
||||
|
||||
use Ratchet\MessageComponentInterface;
|
||||
use Ratchet\ConnectionInterface;
|
||||
use Ratchet\Server\IoServer;
|
||||
use Ratchet\Http\HttpServer;
|
||||
use Ratchet\WebSocket\WsServer;
|
||||
|
||||
class TestServer implements MessageComponentInterface
|
||||
{
|
||||
|
||||
public function onOpen(ConnectionInterface $conn): void
|
||||
{
|
||||
$conn->send('Hello World!');
|
||||
echo "New connection: {$conn->resourceId}\n";
|
||||
}
|
||||
|
||||
public function onMessage(ConnectionInterface $from, $msg): void
|
||||
{
|
||||
$from->send("You said: $msg");
|
||||
}
|
||||
|
||||
public function onClose(ConnectionInterface $conn): void
|
||||
{
|
||||
echo "Connection {$conn->resourceId} closed\n";
|
||||
}
|
||||
|
||||
public function onError(ConnectionInterface $conn, \Exception $e): void
|
||||
{
|
||||
echo "Error: {$e->getMessage()}\n";
|
||||
$conn->close();
|
||||
}
|
||||
}
|
||||
|
||||
$server = IoServer::factory(
|
||||
new HttpServer(new WsServer(new TestServer())),
|
||||
8080
|
||||
);
|
||||
|
||||
echo "WebSocket server running on ws://localhost:8080\n";
|
||||
|
||||
$server->run();
|
||||
@@ -1,3 +1,50 @@
|
||||
<?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();
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
namespace ComCen\Html;
|
||||
|
||||
class Html
|
||||
{
|
||||
public string $content = "";
|
||||
function renderContent(): void
|
||||
{
|
||||
echo $this->content;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user