Files
kiri-core/kiri-websocket-server/Sender.php
T

137 lines
2.7 KiB
PHP
Raw Normal View History

2022-01-09 03:50:38 +08:00
<?php
namespace Kiri\Websocket;
2022-01-12 14:10:33 +08:00
use Kiri;
2022-01-09 03:50:38 +08:00
use Swoole\{Coroutine\Http\Server as AliasServer, WebSocket\Server};
2022-02-16 10:55:42 +08:00
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
2022-01-09 03:50:38 +08:00
/**
*
*/
2022-02-16 10:55:42 +08:00
class Sender extends Kiri\Abstracts\Component implements WebSocketInterface
2022-01-09 03:50:38 +08:00
{
/**
2022-02-14 10:23:43 +08:00
* @var AliasServer|Server|null
2022-01-09 03:50:38 +08:00
*/
2022-02-14 10:23:43 +08:00
private AliasServer|Server|null $server = null;
2022-01-09 03:50:38 +08:00
2022-02-16 10:55:42 +08:00
private FdCollector $collector;
/**
* @return void
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function init()
{
$this->collector = $this->getContainer()->get(FdCollector::class);
}
2022-01-17 19:04:26 +08:00
/**
* @param AliasServer|Server $server
*/
public function setServer(mixed $server): void
{
$this->server = $server;
}
2022-01-09 03:50:38 +08:00
/**
* @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
{
2022-02-14 14:28:47 +08:00
if (!$this->isEstablished($fd)) {
return false;
}
if ($this->server instanceof Server) {
2022-01-09 03:50:38 +08:00
return $this->server->push($fd, $data, $opcode, $flags);
}
2022-02-14 14:28:47 +08:00
2022-02-16 10:55:42 +08:00
$response = $this->collector->get($fd);
2022-02-14 14:28:47 +08:00
if (!empty($response)) {
return $response->push($data, $opcode, $flags);
}
2022-01-09 03:50:38 +08:00
return false;
}
/**
* @param $fd
* @param $reactor_id
* @return array|null
*/
public function connection_info($fd, $reactor_id = null): ?array
{
2022-02-14 13:34:54 +08:00
if ($this->server instanceof Server) {
return $this->server->getClientInfo($fd, $reactor_id);
}
if ($this->server->exist($fd)) {
return ['websocket_status' => 1];
}
return null;
2022-01-09 03:50:38 +08:00
}
/**
* @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
{
2022-02-14 14:28:47 +08:00
if (!$this->isEstablished($fd)) {
return false;
}
if ($this->server instanceof Server) {
2022-01-09 03:50:38 +08:00
return $this->server->disconnect($fd, $code, $reason);
}
2022-02-14 14:28:47 +08:00
return $this->server->close($fd, $reason);
2022-01-09 03:50:38 +08:00
}
/**
* @param int $fd
* @return bool
*/
public function isEstablished(int $fd): bool
{
2022-02-14 14:28:47 +08:00
if (!$this->exist($fd)) {
return false;
}
if ($this->server instanceof Server) {
return $this->server->isEstablished($fd);
}
return true;
2022-01-09 03:50:38 +08:00
}
/**
* @param int $fd
* @return bool
*/
public function exist(int $fd): bool
{
2022-02-14 14:28:47 +08:00
if ($this->server instanceof Server) {
return $this->server->exist($fd);
}
$collector = Kiri::getContainer()->get(FdCollector::class);
return $collector->has($fd);
2022-01-09 03:50:38 +08:00
}
}