Code cleanup, added phpstan

This commit is contained in:
2024-02-21 23:48:13 +01:00
parent a6f70dbb76
commit 70e353bd0c
5 changed files with 47 additions and 23 deletions

View File

@ -54,6 +54,10 @@ class WebSocketConnection implements WebSocketInterface
$this->groupManager = $groupManager;
$this->inStream->on('data', $this->onWebSocketData(...));
$this->inStream->on('close', function () {
$this->close();
$this->emit('close', []);
});
}
private function onWebSocketData($data)
@ -87,6 +91,7 @@ class WebSocketConnection implements WebSocketInterface
$this->sendPong($payload);
return;
case self::OP_PONG:
$this->checkPong($payload);
return;
case self::OP_CLOSE:
// TODO implement
@ -103,11 +108,27 @@ class WebSocketConnection implements WebSocketInterface
}
}
/**
* Sends a ping, and closes the connection on timeout.
*
*/
public function ping(): void
{
// TODO save the state somehow
$payload = "ping";
$this->send(self::OP_PING, $payload, true);
}
private function sendPong(string $data): void
{
$this->send(self::OP_PONG, $data, true);
}
private function checkPong(string $data): void
{
// TODO reset the state and any ping timers
}
public function setGroup(?string $name): void
{
if ($this->group) {
@ -215,24 +236,4 @@ class WebSocketConnection implements WebSocketInterface
}
// private function hexdump($data): void
// {
// printf("%4d .\n", strlen($data));
// $rows = str_split($data, 16);
// $offs = 0;
// foreach ($rows as $row) {
// $h = []; $a = [];
// for ($n = 0; $n < 16; $n++) {
// if ($n < strlen($row)) {
// $h[] = sprintf("%02x%s", ord($row[$n]), ($n==7)?" ":" ");
// $a[] = sprintf("%s%s", (ctype_print($row[$n])?$row[$n]:"."), ($n==7)?" ":"");
// } else {
// $h[] = (($n==7)?" ":" ");
// $a[] = (($n==7)?" ":" ");
// }
// }
// printf("%04x | %s | %s\n", 16 * $offs++, join("", $h), join("", $a));
// }
// }
}