From 4bc745142476fb564ea1e9f11f41845b3059bffe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=91=E6=9E=97?= Date: Sat, 8 Jan 2022 18:10:41 +0800 Subject: [PATCH] =?UTF-8?q?Revert=20"=E6=94=B9=E5=90=8D"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit fdf58326 --- kiri-websocket-server/Sender.php | 70 ++++++++++++++++++++++++++++++-- 1 file changed, 66 insertions(+), 4 deletions(-) diff --git a/kiri-websocket-server/Sender.php b/kiri-websocket-server/Sender.php index 821c2f69..b21af285 100644 --- a/kiri-websocket-server/Sender.php +++ b/kiri-websocket-server/Sender.php @@ -2,19 +2,81 @@ namespace Kiri\Websocket; -class Sender +use Kiri\Kiri; +use Swoole\{Coroutine\Http\Server as AliasServer, WebSocket\Server}; + + +/** + * + */ +class Sender implements WebSocketInterface { - public function push($fd, $data) + /** + * @var AliasServer|Server + */ + private AliasServer|Server $server; + + + /** + * + */ + public function __construct() { + $this->server = Kiri::getDi()->get(WebSocketInterface::class); } - public function close($fd) + /** + * @param int $fd + * @param mixed $data + * @param int $opcode + * @param int $flags + * @return bool + */ + public function push(int $fd, string $data, int $opcode = WEBSOCKET_OPCODE_TEXT, int $flags = SWOOLE_WEBSOCKET_FLAG_FIN): bool { - + if ($this->isEstablished($fd)) { + return $this->server->push($fd, $data, $opcode, $flags); + } + return false; } + /** + * @param int $fd + * @param int $code + * @param string $reason + * @return bool + */ + public function disconnect(int $fd, int $code = SWOOLE_WEBSOCKET_CLOSE_NORMAL, string $reason = ''): bool + { + if ($this->isEstablished($fd)) { + // TODO: Implement disconnect() method. + return $this->server->disconnect($fd, $code, $reason); + } + return false; + } + + + /** + * @param int $fd + * @return bool + */ + public function isEstablished(int $fd): bool + { + return $this->exist($fd) && $this->server->isEstablished($fd); + } + + + /** + * @param int $fd + * @return bool + */ + public function exist(int $fd): bool + { + return $this->server->exist($fd); + } + }