|
8 | 8 | use Workerman\Protocols\WebsocketEx;
|
9 | 9 | use Workerman\Worker;
|
10 | 10 | use function func\call_wrap;
|
| 11 | +use function func\format_byte; |
11 | 12 | use function func\log;
|
12 |
| -use function func\str_starts_with; |
13 | 13 | use function sprintf;
|
| 14 | +use function str_starts_with; |
14 | 15 | use function zlib_decode;
|
15 | 16 |
|
16 | 17 | class Server
|
@@ -78,23 +79,31 @@ public function onRequest(TcpConnection $connection, Request $request)
|
78 | 79 |
|
79 | 80 | if ($contentType === 'application/x-compress') {
|
80 | 81 | $message = zlib_decode($request->rawBody());
|
| 82 | + $messageSize = sprintf('%s (compress: %s)', format_byte(strlen($message)), format_byte(strlen($request->rawBody()))); |
81 | 83 | } else {
|
82 | 84 | $message = $request->rawBody();
|
| 85 | + $messageSize = format_byte(strlen($message)); |
83 | 86 | }
|
84 | 87 |
|
85 | 88 | $response = new Response(200);
|
86 | 89 | $connection->send($response);
|
87 | 90 |
|
88 |
| - log(sprintf('receive[#%s] message: send %s byte to %s', $connection->id, strlen($message), $request->uri())); |
89 |
| - $this->broadcast($message, $request->uri()); |
| 91 | + log(sprintf( |
| 92 | + 'receive[%s] message[#%s]: send %s to %s', |
| 93 | + $connection->getRemoteIp(), |
| 94 | + $connection->id, |
| 95 | + $messageSize, |
| 96 | + $request->uri() |
| 97 | + )); |
| 98 | + $this->broadcast($message, $request->uri(), $connection->id); |
90 | 99 | }
|
91 | 100 |
|
92 | 101 | public function onHandshake(TcpConnection $connection, string $http_header)
|
93 | 102 | {
|
94 | 103 | $request_uri = $_SERVER['REQUEST_URI'];
|
95 | 104 | $this->broadcastBind[$request_uri][$connection->id] = $connection->id;
|
96 | 105 | $this->wsClient[$connection->id] = $request_uri;
|
97 |
| - log("ws client join:{$request_uri}[{$connection->id}]"); |
| 106 | + log("client join:{$request_uri}[{$connection->id}]"); |
98 | 107 |
|
99 | 108 | /** @var WebsocketEx $protocol */
|
100 | 109 | $protocol = $connection->protocol;
|
@@ -123,10 +132,10 @@ public function onWsClose (TcpConnection $connection) {
|
123 | 132 | log("ws client disconnect:{$request_uri}[{$connection->id}]");
|
124 | 133 | }
|
125 | 134 |
|
126 |
| - protected function broadcast (string $data, string $uri) |
| 135 | + protected function broadcast (string $data, string $uri, int $cid) |
127 | 136 | {
|
128 | 137 | $clients = $this->broadcastBind[$uri] ?? [];
|
129 |
| - log("broadcast message to {$uri}, count: " . count($clients)); |
| 138 | + log("broadcast message[#{$cid}] to {$uri}, count: " . count($clients)); |
130 | 139 | foreach ($clients as $id) {
|
131 | 140 | /** @var TcpConnection $conn */
|
132 | 141 | $conn = $this->websocket->connections[$id];
|
|
0 commit comments