Revert "改名"
This reverts commit fdf58326
This commit is contained in:
@@ -0,0 +1,12 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Kiri\Abstracts;
|
||||||
|
|
||||||
|
abstract class AbstractServer extends Component
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
public string $name = 'http';
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -9,9 +9,7 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace Kiri\Abstracts;
|
namespace Kiri\Abstracts;
|
||||||
|
|
||||||
use Exception;
|
|
||||||
use Kiri\Exception\ConfigException;
|
use Kiri\Exception\ConfigException;
|
||||||
use Kiri\Kiri;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -95,7 +93,19 @@ class Config extends Component
|
|||||||
*/
|
*/
|
||||||
public static function set($key, $value): mixed
|
public static function set($key, $value): mixed
|
||||||
{
|
{
|
||||||
return static::setData($key, $value);
|
$explode = explode('.', $key);
|
||||||
|
$parent = &static::$data;
|
||||||
|
foreach ($explode as $item) {
|
||||||
|
if (!isset($parent[$item])) {
|
||||||
|
$parent[$item] = [];
|
||||||
|
}
|
||||||
|
$parent = &$parent[$item];
|
||||||
|
}
|
||||||
|
$parent = $value;
|
||||||
|
|
||||||
|
unset($parent);
|
||||||
|
|
||||||
|
return static::$data;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -9,7 +9,6 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace Kiri\Di;
|
namespace Kiri\Di;
|
||||||
|
|
||||||
use Kiri\Abstracts\Component;
|
|
||||||
use Note\Inject;
|
use Note\Inject;
|
||||||
use Closure;
|
use Closure;
|
||||||
use Exception;
|
use Exception;
|
||||||
@@ -79,8 +78,7 @@ class Container implements ContainerInterface
|
|||||||
* @param array $constrict
|
* @param array $constrict
|
||||||
* @param array $config
|
* @param array $config
|
||||||
* @return mixed
|
* @return mixed
|
||||||
* @throws ReflectionException
|
* @throws
|
||||||
* @throws Exception
|
|
||||||
*/
|
*/
|
||||||
public function make($class, array $constrict = [], array $config = []): mixed
|
public function make($class, array $constrict = [], array $config = []): mixed
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -2,54 +2,64 @@
|
|||||||
|
|
||||||
namespace Kiri\Websocket;
|
namespace Kiri\Websocket;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
use Http\Handler\DataGrip;
|
use Http\Handler\DataGrip;
|
||||||
use Http\Handler\Router;
|
use Http\Handler\Router;
|
||||||
use Kiri\Abstracts\Component;
|
use Kiri\Abstracts\AbstractServer;
|
||||||
use Kiri\Server\Contract\OnOpenInterface;
|
use Note\Inject;
|
||||||
use Psr\Container\ContainerExceptionInterface;
|
use Psr\Container\ContainerExceptionInterface;
|
||||||
use Psr\Container\NotFoundExceptionInterface;
|
use Psr\Container\NotFoundExceptionInterface;
|
||||||
use ReflectionException;
|
use Server\Constant;
|
||||||
use Server\Contract\OnCloseInterface;
|
use Server\Contract\OnCloseInterface;
|
||||||
use Server\Contract\OnHandshakeInterface;
|
use Server\Contract\OnHandshakeInterface;
|
||||||
use Server\Contract\OnMessageInterface;
|
use Server\Contract\OnMessageInterface;
|
||||||
|
use Server\Contract\OnOpenInterface;
|
||||||
|
use Server\SwooleServerInterface;
|
||||||
use Swoole\Http\Request;
|
use Swoole\Http\Request;
|
||||||
use Swoole\Http\Response;
|
use Swoole\Http\Response;
|
||||||
use Swoole\WebSocket\CloseFrame;
|
use Swoole\WebSocket\CloseFrame;
|
||||||
use Swoole\WebSocket\Frame;
|
use Swoole\WebSocket\Frame;
|
||||||
|
use Swoole\WebSocket\Server as WebSocketServer;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* websocket server
|
||||||
*/
|
*/
|
||||||
class Server extends Component implements OnHandshakeInterface, OnMessageInterface, OnCloseInterface
|
class Server extends AbstractServer implements OnHandshakeInterface, OnMessageInterface, OnCloseInterface
|
||||||
{
|
{
|
||||||
|
|
||||||
public Router $router;
|
public Router $router;
|
||||||
|
|
||||||
|
|
||||||
public string $serverName = 'ws';
|
const SHA1_KEY = '258EAFA5-E914-47DA-95CA-C5AB0DC85B11';
|
||||||
|
|
||||||
|
|
||||||
public mixed $callback = null;
|
public mixed $callback = null;
|
||||||
|
|
||||||
|
|
||||||
public mixed $server;
|
/**
|
||||||
|
* @var WebSocketServer
|
||||||
|
*/
|
||||||
|
#[Inject(SwooleServerInterface::class)]
|
||||||
|
public WebSocketServer $server;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @throws ContainerExceptionInterface
|
* @throws ContainerExceptionInterface
|
||||||
* @throws NotFoundExceptionInterface
|
* @throws NotFoundExceptionInterface
|
||||||
* @throws ReflectionException
|
|
||||||
*/
|
*/
|
||||||
public function init()
|
public function init()
|
||||||
{
|
{
|
||||||
$this->router = $this->container->get(DataGrip::class)->get($this->serverName);
|
$this->router = $this->container->get(DataGrip::class)->get('ws');
|
||||||
$handler = $this->router->find('/', 'GET');
|
$handler = $this->router->find('/', 'GET');
|
||||||
if (is_int($handler) || is_null($handler)) {
|
if (is_int($handler) || is_null($handler)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
$this->callback = $handler->callback[0];
|
$this->callback = $handler->callback[0];
|
||||||
|
|
||||||
|
$this->server->on(Constant::HANDSHAKE, [$this, 'onHandshake']);
|
||||||
|
$this->server->on(Constant::MESSAGE, [$this, 'onMessage']);
|
||||||
|
$this->server->on(Constant::CLOSE, [$this, 'onClose']);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -65,6 +75,37 @@ class Server extends Component implements OnHandshakeInterface, OnMessageInterfa
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Request $request
|
||||||
|
* @param Response $response
|
||||||
|
* @return void
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
protected function protocol(Request $request, Response $response)
|
||||||
|
{
|
||||||
|
$secWebSocketKey = $request->header['sec-websocket-key'];
|
||||||
|
$patten = '#^[+/0-9A-Za-z]{21}[AQgw]==$#';
|
||||||
|
if (preg_match($patten, $secWebSocketKey) === 0 || strlen(base64_decode($secWebSocketKey)) !== 16) {
|
||||||
|
throw new Exception('protocol error.', 500);
|
||||||
|
}
|
||||||
|
$key = base64_encode(sha1($request->header['sec-websocket-key'] . self::SHA1_KEY, true));
|
||||||
|
$headers = [
|
||||||
|
'Upgrade' => 'websocket',
|
||||||
|
'Connection' => 'Upgrade',
|
||||||
|
'Sec-Websocket-Accept' => $key,
|
||||||
|
'Sec-Websocket-Version' => '13',
|
||||||
|
];
|
||||||
|
if (isset($request->header['sec-websocket-protocol'])) {
|
||||||
|
$headers['Sec-Websocket-Protocol'] = $request->header['sec-websocket-protocol'];
|
||||||
|
}
|
||||||
|
foreach ($headers as $key => $val) {
|
||||||
|
$response->header($key, $val);
|
||||||
|
}
|
||||||
|
$response->setStatusCode(101, 'connection success.');
|
||||||
|
$response->end();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Request $request
|
* @param Request $request
|
||||||
* @param Response $response
|
* @param Response $response
|
||||||
@@ -72,25 +113,64 @@ class Server extends Component implements OnHandshakeInterface, OnMessageInterfa
|
|||||||
public function onHandshake(Request $request, Response $response): void
|
public function onHandshake(Request $request, Response $response): void
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
if (!$this->callback instanceof OnHandshakeInterface) {
|
if ($this->callback instanceof OnHandshakeInterface) {
|
||||||
throw new \Exception('Page not found.');
|
$this->__handshake($request, $response);
|
||||||
}
|
} else {
|
||||||
$this->callback->onHandshake($request, $response);
|
$this->protocol($request, $response);
|
||||||
|
|
||||||
$this->afterHandshake($request);
|
|
||||||
if ($this->server instanceof \Swoole\Coroutine\Http\Server) {
|
|
||||||
while (true) {
|
|
||||||
$data = $response->recv();
|
|
||||||
if ($data === '' || $data === false || $data instanceof CloseFrame) {
|
|
||||||
$this->onClose($this->server, $response->fd);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
$this->onMessage($this->server, $data);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} catch (\Throwable $throwable) {
|
} catch (\Throwable $throwable) {
|
||||||
$response->status(500, $throwable->getMessage());
|
$response->status(500, $throwable->getMessage());
|
||||||
$response->end();
|
$response->end();
|
||||||
|
} finally {
|
||||||
|
if ($this->callback instanceof OnOpenInterface) {
|
||||||
|
$this->callback->onOpen($this->server, $request);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Request $request
|
||||||
|
* @param Response $response
|
||||||
|
* @return void
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
private function __handshake(Request $request, Response $response)
|
||||||
|
{
|
||||||
|
$this->callback->onHandshake($request, $response);
|
||||||
|
$key = base64_encode(sha1($request->header['sec-websocket-key'] . self::SHA1_KEY, true));
|
||||||
|
if ($key != $response->header['Sec-Websocket-Accept']) {
|
||||||
|
throw new Exception('Protocol fail.');
|
||||||
|
}
|
||||||
|
$response->setStatusCode(101, 'connection success.');
|
||||||
|
$response->end();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Request $request
|
||||||
|
* @param Response $response
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
private function _coroutine_server(Request $request, Response $response)
|
||||||
|
{
|
||||||
|
if ($this->server instanceof \Swoole\Coroutine\Http\Server) {
|
||||||
|
$response->upgrade();
|
||||||
|
$this->afterHandshake($request);
|
||||||
|
while (true) {
|
||||||
|
$data = $response->recv();
|
||||||
|
if ($data === '' || $data === false || $data instanceof CloseFrame) {
|
||||||
|
if (!($this->callback instanceof OnCloseInterface)) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
$this->callback->onClose($this->server, $response->fd);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (!($this->callback instanceof OnMessageInterface)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
$this->callback->onMessage($this->server, $data);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user