modify plugin name
This commit is contained in:
@@ -4,12 +4,14 @@ namespace Kiri\Websocket;
|
|||||||
|
|
||||||
use Kiri;
|
use Kiri;
|
||||||
use Swoole\{Coroutine\Http\Server as AliasServer, WebSocket\Server};
|
use Swoole\{Coroutine\Http\Server as AliasServer, WebSocket\Server};
|
||||||
|
use Psr\Container\ContainerExceptionInterface;
|
||||||
|
use Psr\Container\NotFoundExceptionInterface;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
class Sender implements WebSocketInterface
|
class Sender extends Kiri\Abstracts\Component implements WebSocketInterface
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
@@ -19,6 +21,20 @@ class Sender implements WebSocketInterface
|
|||||||
private AliasServer|Server|null $server = null;
|
private AliasServer|Server|null $server = null;
|
||||||
|
|
||||||
|
|
||||||
|
private FdCollector $collector;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return void
|
||||||
|
* @throws ContainerExceptionInterface
|
||||||
|
* @throws NotFoundExceptionInterface
|
||||||
|
*/
|
||||||
|
public function init()
|
||||||
|
{
|
||||||
|
$this->collector = $this->getContainer()->get(FdCollector::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param AliasServer|Server $server
|
* @param AliasServer|Server $server
|
||||||
*/
|
*/
|
||||||
@@ -43,9 +59,8 @@ class Sender implements WebSocketInterface
|
|||||||
if ($this->server instanceof Server) {
|
if ($this->server instanceof Server) {
|
||||||
return $this->server->push($fd, $data, $opcode, $flags);
|
return $this->server->push($fd, $data, $opcode, $flags);
|
||||||
}
|
}
|
||||||
$collector = Kiri::getContainer()->get(FdCollector::class);
|
|
||||||
|
|
||||||
$response = $collector->get($fd);
|
$response = $this->collector->get($fd);
|
||||||
if (!empty($response)) {
|
if (!empty($response)) {
|
||||||
return $response->push($data, $opcode, $flags);
|
return $response->push($data, $opcode, $flags);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ use Kiri\Server\Contract\OnCloseInterface;
|
|||||||
use Kiri\Server\Contract\OnHandshakeInterface;
|
use Kiri\Server\Contract\OnHandshakeInterface;
|
||||||
use Kiri\Server\Contract\OnMessageInterface;
|
use Kiri\Server\Contract\OnMessageInterface;
|
||||||
use Kiri\Server\Contract\OnOpenInterface;
|
use Kiri\Server\Contract\OnOpenInterface;
|
||||||
|
use Kiri\Server\SwooleServerInterface;
|
||||||
use Psr\Container\ContainerExceptionInterface;
|
use Psr\Container\ContainerExceptionInterface;
|
||||||
use Psr\Container\NotFoundExceptionInterface;
|
use Psr\Container\NotFoundExceptionInterface;
|
||||||
use Swoole\Http\Request;
|
use Swoole\Http\Request;
|
||||||
@@ -35,14 +36,26 @@ class Server extends AbstractServer
|
|||||||
public Sender $sender;
|
public Sender $sender;
|
||||||
|
|
||||||
|
|
||||||
|
public FdCollector $collector;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @throws ContainerExceptionInterface
|
* @throws ContainerExceptionInterface
|
||||||
* @throws NotFoundExceptionInterface
|
* @throws NotFoundExceptionInterface
|
||||||
*/
|
*/
|
||||||
public function init()
|
public function init()
|
||||||
{
|
{
|
||||||
$this->router = $this->getContainer()->get(DataGrip::class)->get('ws');
|
$container = $this->getContainer();
|
||||||
|
|
||||||
|
$this->router = $container->get(DataGrip::class)->get('ws');
|
||||||
$handler = $this->router->find('/', 'GET');
|
$handler = $this->router->find('/', 'GET');
|
||||||
|
|
||||||
|
$this->collector = $container->get(FdCollector::class);
|
||||||
|
|
||||||
|
$this->sender = $container->get(Sender::class);
|
||||||
|
if ($container->has(SwooleServerInterface::class)) {
|
||||||
|
$this->sender->setServer($container->get(SwooleServerInterface::class));
|
||||||
|
}
|
||||||
if (is_int($handler) || is_null($handler)) {
|
if (is_int($handler) || is_null($handler)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -52,20 +65,13 @@ class Server extends AbstractServer
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param int $fd
|
* @param int $fd
|
||||||
* @throws ContainerExceptionInterface
|
|
||||||
* @throws NotFoundExceptionInterface
|
|
||||||
*/
|
*/
|
||||||
public function onClose(int $fd): void
|
public function onClose(int $fd): void
|
||||||
{
|
{
|
||||||
$collector = $this->getContainer()->get(Sender::class);
|
$this->collector->remove($fd);
|
||||||
|
if (!$this->sender->isEstablished($fd)) {
|
||||||
// $fds = $this->getContainer()->get(FdCollector::class);
|
|
||||||
|
|
||||||
if (!$collector->isEstablished($fd)) {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// $fds->remove($fd);
|
|
||||||
|
|
||||||
if ($this->callback instanceof OnCloseInterface) {
|
if ($this->callback instanceof OnCloseInterface) {
|
||||||
$this->callback->onClose($fd);
|
$this->callback->onClose($fd);
|
||||||
}
|
}
|
||||||
@@ -75,11 +81,10 @@ class Server extends AbstractServer
|
|||||||
/**
|
/**
|
||||||
* @param Request $request
|
* @param Request $request
|
||||||
* @param Response $response
|
* @param Response $response
|
||||||
* @return void
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
*/
|
||||||
protected function setWebSocketProtocol(Request $request, Response $response)
|
public function onHandshake(Request $request, Response $response): void
|
||||||
{
|
{
|
||||||
|
try {
|
||||||
$secWebSocketKey = $request->header['sec-websocket-key'];
|
$secWebSocketKey = $request->header['sec-websocket-key'];
|
||||||
$patten = '#^[+/0-9A-Za-z]{21}[AQgw]==$#';
|
$patten = '#^[+/0-9A-Za-z]{21}[AQgw]==$#';
|
||||||
if (preg_match($patten, $secWebSocketKey) === 0 || strlen(base64_decode($secWebSocketKey)) !== 16) {
|
if (preg_match($patten, $secWebSocketKey) === 0 || strlen(base64_decode($secWebSocketKey)) !== 16) {
|
||||||
@@ -98,42 +103,17 @@ class Server extends AbstractServer
|
|||||||
foreach ($headers as $key => $val) {
|
foreach ($headers as $key => $val) {
|
||||||
$response->header($key, $val);
|
$response->header($key, $val);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param Request $request
|
|
||||||
* @param Response $response
|
|
||||||
*/
|
|
||||||
public function onHandshake(Request $request, Response $response): void
|
|
||||||
{
|
|
||||||
try {
|
|
||||||
$this->setWebSocketProtocol($request, $response);
|
|
||||||
if ($this->callback instanceof OnHandshakeInterface) {
|
if ($this->callback instanceof OnHandshakeInterface) {
|
||||||
$this->callback->onHandshake($request, $response);
|
$this->callback->onHandshake($request, $response);
|
||||||
} else {
|
} else {
|
||||||
$response->setStatusCode(101, 'connection success.');
|
$response->setStatusCode(101, 'connection success.');
|
||||||
$response->end();
|
$response->end();
|
||||||
|
|
||||||
|
if ($this->callback instanceof OnOpenInterface) {
|
||||||
|
$this->callback->onOpen($request);
|
||||||
}
|
}
|
||||||
// if ($this->server instanceof \Swoole\Coroutine\Http\Server) {
|
|
||||||
// $response->upgrade();
|
|
||||||
// $this->deferOpen($request);
|
|
||||||
// while (true) {
|
|
||||||
// $receive = $response->recv();
|
|
||||||
// if ($receive === '' || $receive instanceof CloseFrame) {
|
|
||||||
// $response->close();
|
|
||||||
// if ($this->callback instanceof OnCloseInterface) {
|
|
||||||
// $this->callback->onClose($this->server, $response->fd);
|
|
||||||
// }
|
|
||||||
// break;
|
|
||||||
// }
|
|
||||||
// $this->callback->onMessage($this->server, $receive);
|
|
||||||
// }
|
|
||||||
// } else {
|
|
||||||
// $this->deferOpen($request);
|
|
||||||
// }
|
|
||||||
if ($response->isWritable()) {
|
|
||||||
$this->deferOpen($request);
|
|
||||||
}
|
}
|
||||||
} catch (\Throwable $throwable) {
|
} catch (\Throwable $throwable) {
|
||||||
$response->status(4000 + $throwable->getCode(), $throwable->getMessage());
|
$response->status(4000 + $throwable->getCode(), $throwable->getMessage());
|
||||||
@@ -142,20 +122,17 @@ class Server extends AbstractServer
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private function deferOpen($request)
|
|
||||||
{
|
|
||||||
if ($this->callback instanceof OnOpenInterface) {
|
|
||||||
$this->callback->onOpen($request);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Frame $frame
|
* @param Frame $frame
|
||||||
*/
|
*/
|
||||||
public function onMessage(Frame $frame): void
|
public function onMessage(Frame $frame): void
|
||||||
{
|
{
|
||||||
if ($this->callback instanceof OnMessageInterface) {
|
if ($frame->opcode == 0x08) {
|
||||||
|
$this->collector->remove($frame->fd);
|
||||||
|
} else {
|
||||||
|
if (!($this->callback instanceof OnMessageInterface)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
$this->callback->onMessage($frame);
|
$this->callback->onMessage($frame);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user