Skip to content

Commit a1fc7eb

Browse files
committed
update: 调整信息输出
1 parent 96d7e3a commit a1fc7eb

File tree

2 files changed

+33
-12
lines changed

2 files changed

+33
-12
lines changed

src/Server.php

+15-6
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88
use Workerman\Protocols\WebsocketEx;
99
use Workerman\Worker;
1010
use function func\call_wrap;
11+
use function func\format_byte;
1112
use function func\log;
12-
use function func\str_starts_with;
1313
use function sprintf;
14+
use function str_starts_with;
1415
use function zlib_decode;
1516

1617
class Server
@@ -78,23 +79,31 @@ public function onRequest(TcpConnection $connection, Request $request)
7879

7980
if ($contentType === 'application/x-compress') {
8081
$message = zlib_decode($request->rawBody());
82+
$messageSize = sprintf('%s (compress: %s)', format_byte(strlen($message)), format_byte(strlen($request->rawBody())));
8183
} else {
8284
$message = $request->rawBody();
85+
$messageSize = format_byte(strlen($message));
8386
}
8487

8588
$response = new Response(200);
8689
$connection->send($response);
8790

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);
9099
}
91100

92101
public function onHandshake(TcpConnection $connection, string $http_header)
93102
{
94103
$request_uri = $_SERVER['REQUEST_URI'];
95104
$this->broadcastBind[$request_uri][$connection->id] = $connection->id;
96105
$this->wsClient[$connection->id] = $request_uri;
97-
log("ws client join:{$request_uri}[{$connection->id}]");
106+
log("client join:{$request_uri}[{$connection->id}]");
98107

99108
/** @var WebsocketEx $protocol */
100109
$protocol = $connection->protocol;
@@ -123,10 +132,10 @@ public function onWsClose (TcpConnection $connection) {
123132
log("ws client disconnect:{$request_uri}[{$connection->id}]");
124133
}
125134

126-
protected function broadcast (string $data, string $uri)
135+
protected function broadcast (string $data, string $uri, int $cid)
127136
{
128137
$clients = $this->broadcastBind[$uri] ?? [];
129-
log("broadcast message to {$uri}, count: " . count($clients));
138+
log("broadcast message[#{$cid}] to {$uri}, count: " . count($clients));
130139
foreach ($clients as $id) {
131140
/** @var TcpConnection $conn */
132141
$conn = $this->websocket->connections[$id];

src/function.php

+18-6
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,31 @@
55
use Closure;
66
use Workerman\Worker;
77
use function date;
8-
use function strlen;
9-
use function strncmp;
108

119
function call_wrap(callable $call)
1210
{
1311
return Closure::fromCallable($call);
1412
}
1513

16-
function str_starts_with(string $haystack, string $needle): bool
14+
/**
15+
* @param int $byte
16+
* @param int $dec
17+
* @return string
18+
*/
19+
function format_byte (int $byte, int $dec = 2): string
1720
{
18-
return (strlen($haystack) !== 0 && $haystack[0] === $needle[0])
19-
? strncmp($haystack, $needle, strlen($needle)) === 0
20-
: false;
21+
$units = ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB']; //
22+
$count = count($units) - 1;
23+
$pos = 0;
24+
25+
while ($byte >= 1024 && $pos < $count) {
26+
$byte /= 1024;
27+
$pos++;
28+
}
29+
30+
$result = sprintf('%.2f', round($byte, $dec));
31+
32+
return "{$result}{$units[$pos]}";
2133
}
2234

2335
function log(string $msg)

0 commit comments

Comments
 (0)