Revert "改名"

This reverts commit fdf58326
This commit is contained in:
2022-01-06 19:05:15 +08:00
parent 45319c3733
commit f867f4be9e
4 changed files with 131 additions and 31 deletions
+12
View File
@@ -0,0 +1,12 @@
<?php
namespace Kiri\Abstracts;
abstract class AbstractServer extends Component
{
public string $name = 'http';
}
+13 -3
View File
@@ -9,9 +9,7 @@ declare(strict_types=1);
namespace Kiri\Abstracts;
use Exception;
use Kiri\Exception\ConfigException;
use Kiri\Kiri;
/**
@@ -95,7 +93,19 @@ class Config extends Component
*/
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;
}
/**
+1 -3
View File
@@ -9,7 +9,6 @@ declare(strict_types=1);
namespace Kiri\Di;
use Kiri\Abstracts\Component;
use Note\Inject;
use Closure;
use Exception;
@@ -79,8 +78,7 @@ class Container implements ContainerInterface
* @param array $constrict
* @param array $config
* @return mixed
* @throws ReflectionException
* @throws Exception
* @throws
*/
public function make($class, array $constrict = [], array $config = []): mixed
{
+105 -25
View File
@@ -2,54 +2,64 @@
namespace Kiri\Websocket;
use Exception;
use Http\Handler\DataGrip;
use Http\Handler\Router;
use Kiri\Abstracts\Component;
use Kiri\Server\Contract\OnOpenInterface;
use Kiri\Abstracts\AbstractServer;
use Note\Inject;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use ReflectionException;
use Server\Constant;
use Server\Contract\OnCloseInterface;
use Server\Contract\OnHandshakeInterface;
use Server\Contract\OnMessageInterface;
use Server\Contract\OnOpenInterface;
use Server\SwooleServerInterface;
use Swoole\Http\Request;
use Swoole\Http\Response;
use Swoole\WebSocket\CloseFrame;
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 string $serverName = 'ws';
const SHA1_KEY = '258EAFA5-E914-47DA-95CA-C5AB0DC85B11';
public mixed $callback = null;
public mixed $server;
/**
* @var WebSocketServer
*/
#[Inject(SwooleServerInterface::class)]
public WebSocketServer $server;
/**
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws ReflectionException
*/
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');
if (is_int($handler) || is_null($handler)) {
return;
}
$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 Response $response
@@ -72,25 +113,64 @@ class Server extends Component implements OnHandshakeInterface, OnMessageInterfa
public function onHandshake(Request $request, Response $response): void
{
try {
if (!$this->callback instanceof OnHandshakeInterface) {
throw new \Exception('Page not found.');
}
$this->callback->onHandshake($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);
}
if ($this->callback instanceof OnHandshakeInterface) {
$this->__handshake($request, $response);
} else {
$this->protocol($request, $response);
}
} catch (\Throwable $throwable) {
$response->status(500, $throwable->getMessage());
$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);
}
}
}