-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.php
More file actions
103 lines (75 loc) · 3.15 KB
/
Copy pathserver.php
File metadata and controls
103 lines (75 loc) · 3.15 KB
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
<?php
declare(strict_types=1);
require __DIR__ . '/../bootstrap.php';
require __DIR__ . '/bots/EchoBot.php';
require __DIR__ . '/bots/HelpBot.php';
use Micilini\PhpSockets\Chat\ChatMessage;
use Micilini\PhpSockets\Chat\ChatServer;
use Micilini\PhpSockets\Chat\UserSession;
use Micilini\PhpSockets\Config\ChatConfig;
use Micilini\PhpSockets\Config\ServerConfig;
use Micilini\PhpSockets\Connection\Connection;
use Micilini\PhpSockets\Examples\PrivateChat\Bots\EchoBot;
use Micilini\PhpSockets\Examples\PrivateChat\Bots\HelpBot;
$host = getenv('PHPSOCKETS_HOST') ?: '127.0.0.1';
$port = (int) (getenv('PHPSOCKETS_PORT') ?: 8080);
$publicPath = str_replace('\\', '/', __DIR__ . '/public');
echo "PHPSockets PrivateChat server running on ws://{$host}:{$port}\n";
echo "Open the browser UI with: php -S 127.0.0.1:8002 -t {$publicPath}\n";
echo "Press Ctrl+C to stop the WebSocket server.\n\n";
$server = ChatServer::create(
ServerConfig::new(host: $host, port: $port, maxPayloadBytes: 4 * 1024 * 1024),
ChatConfig::new(),
);
$server->bots()
->register(new HelpBot())
->register(new EchoBot());
$server->on('open', function (Connection $connection): void {
echo "[socket.open] {$connection->id()} connected from {$connection->remoteAddress()}\n";
});
$server->on('close', function (Connection $connection, int $code, string $reason): void {
echo "[socket.close] {$connection->id()} closed with code {$code}";
if ($reason !== '') {
echo " and reason {$reason}";
}
echo "\n";
});
$server->on('error', function (Throwable $exception, ?Connection $connection): void {
$connectionId = $connection instanceof Connection ? $connection->id() : 'server';
echo "[socket.error] {$connectionId}: {$exception->getMessage()}\n";
});
$server->on('user.joined', function (array $event): void {
$session = $event['session'] ?? null;
if (!$session instanceof UserSession) {
return;
}
$onlineCount = $event['onlineCount'] ?? 0;
echo "[private.user.joined] {$session->displayName} joined. Online users: {$onlineCount}\n";
});
$server->on('user.left', function (array $event): void {
$session = $event['session'] ?? null;
$userId = (string) ($event['userId'] ?? 'unknown');
if ($session instanceof UserSession) {
echo "[private.user.left] {$session->displayName} left.\n";
return;
}
echo "[private.user.left] {$userId} left.\n";
});
$server->on('message.received', function (array $event): void {
$message = $event['message'] ?? null;
$scope = (string) ($event['scope'] ?? 'unknown');
if (!$message instanceof ChatMessage) {
return;
}
$body = is_string($message->body) ? $message->body : '[file attachment]';
echo "[private.message.received] scope={$scope} room={$message->roomId} from={$message->fromUserId}: {$body}\n";
});
$server->on('bot.responded', function (array $event): void {
$message = $event['message'] ?? null;
$scope = (string) ($event['scope'] ?? 'unknown');
if (!$message instanceof ChatMessage) {
return;
}
echo "[private.bot.responded] scope={$scope} room={$message->roomId} from={$message->fromUserId}\n";
});
$server->run();