qqq
This commit is contained in:
@@ -1,140 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Kiri\Server\Abstracts;
|
|
||||||
|
|
||||||
use Exception;
|
|
||||||
use Kiri\Abstracts\Component;
|
|
||||||
use Kiri\Config\ConfigProvider;
|
|
||||||
use Kiri\Router\DataGrip;
|
|
||||||
use Kiri\Router\Handler;
|
|
||||||
use Kiri\Router\RouterCollector;
|
|
||||||
use Kiri\Server\Constant;
|
|
||||||
use Kiri\Server\Contract\OnCloseInterface;
|
|
||||||
use Kiri\Server\Contract\OnDisconnectInterface;
|
|
||||||
use Kiri\Server\Contract\OnHandshakeInterface;
|
|
||||||
use Kiri\Server\Contract\OnMessageInterface;
|
|
||||||
use ReflectionException;
|
|
||||||
use Swoole\Http\Request;
|
|
||||||
use Swoole\Http\Response;
|
|
||||||
use Swoole\Server;
|
|
||||||
use Swoole\WebSocket\Frame;
|
|
||||||
|
|
||||||
|
|
||||||
class WebSocket extends Component implements OnHandshakeInterface, OnMessageInterface, OnDisconnectInterface, OnCloseInterface
|
|
||||||
{
|
|
||||||
|
|
||||||
|
|
||||||
public Handler $handler;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var RouterCollector
|
|
||||||
*/
|
|
||||||
public RouterCollector $collector;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return void
|
|
||||||
* @throws ReflectionException
|
|
||||||
*/
|
|
||||||
public function init(): void
|
|
||||||
{
|
|
||||||
$this->collector = \Kiri::getDi()->get(DataGrip::class)->get('wss');
|
|
||||||
|
|
||||||
$this->handler = $this->collector->query('/', 'GET');
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param Request $request
|
|
||||||
* @param Response $response
|
|
||||||
* @return void
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
public function onHandshake(Request $request, Response $response): void
|
|
||||||
{
|
|
||||||
// TODO: Implement onHandshake() method.
|
|
||||||
$secWebSocketKey = $request->header['sec-websocket-key'];
|
|
||||||
$patten = '#^[+/0-9A-Za-z]{21}[AQgw]==$#';
|
|
||||||
if (0 === preg_match($patten, $secWebSocketKey) || 16 !== strlen(base64_decode($secWebSocketKey))) {
|
|
||||||
throw new Exception('protocol error.', 500);
|
|
||||||
}
|
|
||||||
$key = base64_encode(sha1($request->header['sec-websocket-key'] . '258EAFA5-E914-47DA-95CA-C5AB0DC85B11', TRUE));
|
|
||||||
$headers = [
|
|
||||||
'Upgrade' => 'websocket',
|
|
||||||
'Connection' => 'Upgrade',
|
|
||||||
'Sec-websocket-Accept' => $key,
|
|
||||||
'Sec-websocket-Version' => '13',
|
|
||||||
];
|
|
||||||
if (isset($request->header['sec-websocket-protocol'])) {
|
|
||||||
$explode = explode(',', $request->header['sec-websocket-protocol']);
|
|
||||||
$headers['Sec-websocket-Protocol'] = $explode[0];
|
|
||||||
}
|
|
||||||
foreach ($headers as $key => $val) {
|
|
||||||
$response->setHeader($key, $val);
|
|
||||||
}
|
|
||||||
if ($this->handler->implement(OnHandshakeInterface::class)) {
|
|
||||||
$handler = $this->handler->getClass();
|
|
||||||
|
|
||||||
call_user_func([$handler, 'onHandshake'], $request, $response);
|
|
||||||
} else {
|
|
||||||
$response->setStatusCode(101);
|
|
||||||
$response->end();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param Server $server
|
|
||||||
* @param Frame $frame
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public function onMessage(Server $server, Frame $frame): void
|
|
||||||
{
|
|
||||||
// TODO: Implement onMessage() method.
|
|
||||||
if (!$this->handler->implement(OnMessageInterface::class)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
$handler = $this->handler->getClass();
|
|
||||||
|
|
||||||
call_user_func([$handler, 'onMessage'], $server, $frame);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param \Swoole\WebSocket\Server $server
|
|
||||||
* @param int $fd
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public function onDisconnect(\Swoole\WebSocket\Server $server, int $fd): void
|
|
||||||
{
|
|
||||||
// TODO: Implement onDisconnect() method.
|
|
||||||
if (!$this->handler->implement(OnDisconnectInterface::class)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
$handler = $this->handler->getClass();
|
|
||||||
|
|
||||||
call_user_func([$handler, 'onDisconnect'], $server, $fd);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param Server $server
|
|
||||||
* @param int $fd
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public function onClose(Server $server, int $fd): void
|
|
||||||
{
|
|
||||||
// TODO: Implement onDisconnect() method.
|
|
||||||
if (!$this->handler->implement(OnCloseInterface::class)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
$handler = $this->handler->getClass();
|
|
||||||
|
|
||||||
call_user_func([$handler, 'onClose'], $server, $fd);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,41 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Kiri\Server\Broadcast;
|
|
||||||
|
|
||||||
use Kiri;
|
|
||||||
use Kiri\Server\ServerInterface;
|
|
||||||
use Kiri\Server\Abstracts\ProcessManager;
|
|
||||||
use ReflectionException;
|
|
||||||
|
|
||||||
class Broadcast
|
|
||||||
{
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $message
|
|
||||||
* @return void
|
|
||||||
* @throws ReflectionException
|
|
||||||
*/
|
|
||||||
public function broadcast($message): void
|
|
||||||
{
|
|
||||||
$di = Kiri::getDi();
|
|
||||||
$message = serialize(new Message($message));
|
|
||||||
|
|
||||||
$processes = $di->get(ProcessManager::class)->getProcesses();
|
|
||||||
foreach ($processes as $process) {
|
|
||||||
$process->write($message);
|
|
||||||
}
|
|
||||||
|
|
||||||
$server = $di->get(ServerInterface::class);
|
|
||||||
|
|
||||||
$total = $server->setting['worker_num'] + $server->setting['task_worker_num'];
|
|
||||||
for ($i = 0; $i < $total; $i++) {
|
|
||||||
if ($i == env('environmental_workerId')) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
$server->sendMessage($message, $i);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,34 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Kiri\Server\Broadcast;
|
|
||||||
|
|
||||||
|
|
||||||
use Kiri;
|
|
||||||
use Kiri\Server\Contract\OnPipeMessageInterface;
|
|
||||||
use Psr\Log\LoggerInterface;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
class Message implements OnPipeMessageInterface, OnBroadcastInterface
|
|
||||||
{
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param mixed $data
|
|
||||||
*/
|
|
||||||
public function __construct(public mixed $data)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public function process(): void
|
|
||||||
{
|
|
||||||
$logger = Kiri::getDi()->get(LoggerInterface::class);
|
|
||||||
$logger->debug(env('environmental') . '::' . env('environmental_workerId', 0) . '::' . $this->data);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Kiri\Server\Broadcast;
|
|
||||||
|
|
||||||
interface OnBroadcastInterface
|
|
||||||
{
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public function process(): void;
|
|
||||||
|
|
||||||
}
|
|
||||||
+3
-3
@@ -11,7 +11,7 @@ use Kiri\Server\Events\OnShutdown;
|
|||||||
use Kiri\Server\Events\OnTaskerStart;
|
use Kiri\Server\Events\OnTaskerStart;
|
||||||
use Kiri\Server\Events\OnWorkerStart;
|
use Kiri\Server\Events\OnWorkerStart;
|
||||||
use Kiri\Server\Events\OnWorkerStop;
|
use Kiri\Server\Events\OnWorkerStop;
|
||||||
use Kiri\Server\Abstracts\AsyncServer;
|
use Kiri\Server\Abstracts\CoroutineServer;
|
||||||
use Psr\Container\ContainerExceptionInterface;
|
use Psr\Container\ContainerExceptionInterface;
|
||||||
use Psr\Container\NotFoundExceptionInterface;
|
use Psr\Container\NotFoundExceptionInterface;
|
||||||
use ReflectionException;
|
use ReflectionException;
|
||||||
@@ -45,14 +45,14 @@ class Server
|
|||||||
*/
|
*/
|
||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
$this->class = \config('server.type', AsyncServer::class);
|
$this->class = \config('server.type', CoroutineServer::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @throws ReflectionException
|
* @throws ReflectionException
|
||||||
*/
|
*/
|
||||||
private function manager(): AsyncServer
|
private function manager(): CoroutineServer
|
||||||
{
|
{
|
||||||
return Kiri::getDi()->get($this->class);
|
return Kiri::getDi()->get($this->class);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user