This commit is contained in:
2020-09-01 03:11:34 +08:00
parent ae753bc267
commit 689c2556e6
8 changed files with 194 additions and 191 deletions
+17 -16
View File
@@ -11,6 +11,7 @@ namespace HttpServer\Events;
use Exception;
use HttpServer\ServerManager;
use ReflectionException;
use Snowflake\Application;
use Snowflake\Error\Logger;
use Snowflake\Event;
use Snowflake\Exception\NotFindClassException;
@@ -20,6 +21,7 @@ use Swoole\Http\Response as SResponse;
use Swoole\Process\Pool;
use Swoole\WebSocket\Frame;
use Swoole\WebSocket\Server;
use HttpServer\Route\Annotation\Websocket as AWebsocket;
/**
* Class ServerWebSocket
@@ -32,6 +34,7 @@ class WebSocket extends Server
public $callback = [];
/** @var Application */
public $application;
@@ -63,8 +66,14 @@ class WebSocket extends Server
public function set(array $settings, $pool = null, $events = [], $config = [])
{
parent::set($settings);
Snowflake::get()->set(WebSocket::class, $this);
Snowflake::get()->set(Pool::class, $pool);
$application = Snowflake::get();
$application->set(WebSocket::class, $this);
$application->set(Pool::class, $pool);
$annotation = $application->annotation;
$annotation->register('websocket', AWebsocket::class);
ServerManager::set($this, $settings, $this->application, $events, $config);
}
@@ -87,11 +96,12 @@ class WebSocket extends Server
}
$json = json_decode($frame->data, true);
$manager = Snowflake::get()->annotation;
$manager->runWith($this->getName($json), [$frame->fd, $server]);
/** @var AWebsocket $manager */
$manager = Snowflake::get()->annotation->get('websocket');
$path = $manager->getName(AWebsocket::MESSAGE, $json['route']);
$manager->runWith($path, [$frame->fd, $server]);
} catch (Exception $exception) {
// $this->error($exception->getMessage(), __METHOD__, __FILE__);
// $this->addError($exception->getMessage());
$this->application->addError($exception->getMessage(), 'websocket');
} finally {
$event = Snowflake::get()->event;
$event->trigger(Event::EVENT_AFTER_REQUEST);
@@ -99,15 +109,6 @@ class WebSocket extends Server
}
}
/**
* @param $json
* @return string
*/
private function getName($json)
{
return 'WEBSOCKET:MESSAGE:' . $json['route'];
}
/**
* @param SRequest $request
* @param SResponse $response
@@ -177,7 +178,7 @@ class WebSocket extends Server
$event->trigger(Event::SERVER_CLOSE, [$fd]);
}
} catch (\Throwable $exception) {
// $this->addError($exception->getMessage());
$this->application->addError($exception->getMessage());
} finally {
$event->trigger(Event::RELEASE_ALL);
Logger::insert();
@@ -3,6 +3,7 @@
namespace HttpServer\Route\Annotation;
use ReflectionClass;
use ReflectionException;
use Snowflake\Abstracts\BaseAnnotation;
use Snowflake\Snowflake;
@@ -30,6 +31,47 @@ class Annotation extends \Snowflake\Annotation\Annotation
protected $_annotations = [];
/**
* @param ReflectionClass $reflect
* @param $method
* @param $annotations
* @return mixed|null
* @throws ReflectionException
*/
public function read($reflect, $method, $annotations)
{
$method = $reflect->getMethod($method);
$_annotations = $this->getDocCommentAnnotation($annotations, $method->getDocComment());
$array = [];
foreach ($_annotations as $keyName => $annotation) {
if (!in_array($keyName, $annotations)) {
continue;
}
$array[$keyName] = $this->pop($this->getName(...$annotation));
}
return $array;
}
/**
* @param $controller
* @param $methodName
* @param $events
* @return array|void
* @throws
*/
public function createHandler($controller, $methodName, $events)
{
return Snowflake::createObject($events[2]);
}
/**
* @param $events
* @return bool
@@ -0,0 +1,64 @@
<?php
namespace HttpServer\Route\Annotation;
use Snowflake\Annotation\Annotation;
/**
* Class Websocket
* @package Snowflake\Annotation
*/
class Websocket extends Annotation
{
const MESSAGE = 'Message:';
const HANDSHAKE = 'Handshake:';
const CLOSE = 'Close:';
private $Message = 'required|not empty';
private $Handshake;
private $Close;
/**
* @param $controller
* @param $methodName
* @param $events
* @return array
*/
public function createHandler($controller, $methodName, $events)
{
return [$controller, $methodName];
}
/**
* @param $events
* @return bool|void
*/
public function isLegitimate($events)
{
return true;
}
/**
* @param $events
* @param $comment
* @return false|string
*/
public function getName($events, $comment)
{
$prefix = 'WEBSOCKET:ANNOTATION:' . $events;
if (isset($comment[2])) {
return $prefix . ':' . $comment[2];
}
return $prefix;
}
}
+4 -3
View File
@@ -8,6 +8,7 @@ use HttpServer\Http\Request;
use Exception;
use HttpServer\Application;
use HttpServer\Route\Annotation\Annotation;
use Snowflake\Annotation\Definition\Http;
use Snowflake\Snowflake;
/**
@@ -149,9 +150,9 @@ class Node extends Application
}
/** @var Annotation $annotation */
$annotation = Snowflake::get()->annotation->http;
if (!empty($methods = $annotation->getAnnotation(Annotation::class))) {
$this->_interceptors = $annotation->instance($reflect, $action, $methods);
$annotation = Snowflake::get()->annotation->get('http');
if (!empty($annotations = $annotation->getAnnotation(Annotation::class))) {
$this->_interceptors = $annotation->read($reflect, $action, $annotations);
}
return [$reflect->newInstance(), $action];
} catch (Exception $exception) {
+6 -3
View File
@@ -511,9 +511,12 @@ class Router extends Application implements RouterInterface
$prefix = APP_PATH . 'app/Http/';
/** @var Annotation $annotation */
$annotation = Snowflake::get()->annotation->getHttp();
$annotation->registration_notes($prefix . 'Interceptor', 'App\Http\Interceptor',Annotation::class);
$annotation->registration_notes($prefix . 'Limits', 'App\Http\Limits',Annotation::class);
$annotation = Snowflake::get()->annotation;
$annotation->register('http',Annotation::class);
$annotation = $annotation->get('http');
$annotation->registration_notes($prefix . 'Interceptor', 'App\Http\Interceptor');
$annotation->registration_notes($prefix . 'Limits', 'App\Http\Limits');
include_once "$file";
}