This commit is contained in:
2020-09-02 12:19:20 +08:00
parent 40adb0b20c
commit edac69c4b8
+39 -5
View File
@@ -17,6 +17,7 @@ use HttpServer\Service\WebSocket;
use Exception; use Exception;
use ReflectionException; use ReflectionException;
use Snowflake\Config; use Snowflake\Config;
use Snowflake\Event;
use Snowflake\Exception\ConfigException; use Snowflake\Exception\ConfigException;
use Snowflake\Exception\NotFindClassException; use Snowflake\Exception\NotFindClassException;
use Snowflake\Snowflake; use Snowflake\Snowflake;
@@ -67,9 +68,9 @@ class Server extends Application
} }
$annotation = Snowflake::get()->annotation; $annotation = Snowflake::get()->annotation;
$annotation->register('tcp',Tcp::class); $annotation->register('tcp', Tcp::class);
$annotation->register('http',Annotation::class); $annotation->register('http', Annotation::class);
$annotation->register('websocket',AWebsocket::class); $annotation->register('websocket', AWebsocket::class);
$configs = $this->sortServers($configs); $configs = $this->sortServers($configs);
foreach ($configs as $server) { foreach ($configs as $server) {
@@ -162,16 +163,20 @@ class Server extends Application
*/ */
private function dispatchCreate($config, $settings) private function dispatchCreate($config, $settings)
{ {
$event = Snowflake::get()->event;
if (!($this->baseServer instanceof \Swoole\Server)) { if (!($this->baseServer instanceof \Swoole\Server)) {
$class = $this->dispatch($config['type']); $class = $this->dispatch($config['type']);
$this->baseServer = new $class($config['host'], $config['port'], SWOOLE_PROCESS, $config['mode']); $this->baseServer = new $class($config['host'], $config['port'], SWOOLE_PROCESS, $config['mode']);
$this->baseServer->set($settings); $this->baseServer->set($settings);
if (!$event->exists(Event::SERVER_WORKER_START, [$this, 'onLoadWebsocketHandler'])) {
$event->on(Event::SERVER_WORKER_START, [$this, 'onLoadWebsocketHandler']);
}
} else { } else {
$newListener = $this->baseServer->addlistener($config['host'], $config['port'], $config['mode']); $newListener = $this->baseServer->addlistener($config['host'], $config['port'], $config['mode']);
if (!empty($settings)) { if (!empty($settings)) {
$newListener->set($settings); $newListener->set($settings);
} }
$this->onListenerBind($config, $this->baseServer); $this->onListenerBind($config, $this->baseServer, $event);
} }
return $this->baseServer; return $this->baseServer;
} }
@@ -180,12 +185,18 @@ class Server extends Application
/** /**
* @param $config * @param $config
* @param $newListener * @param $newListener
* @param $event
* @throws NotFindClassException
* @throws ReflectionException
* @throws Exception * @throws Exception
*/ */
private function onListenerBind($config, $newListener) private function onListenerBind($config, $newListener, $event)
{ {
if ($config['type'] == self::HTTP) { if ($config['type'] == self::HTTP) {
$newListener->on('request', [Snowflake::createObject(OnRequest::class), 'onHandler']); $newListener->on('request', [Snowflake::createObject(OnRequest::class), 'onHandler']);
if (!$event->exists(Event::SERVER_WORKER_START, [$this, 'onLoadHttpHandler'])) {
$event->on(Event::SERVER_WORKER_START, [$this, 'onLoadHttpHandler']);
}
} else if ($config['type'] == self::TCP || $config['type'] == self::PACKAGE) { } else if ($config['type'] == self::TCP || $config['type'] == self::PACKAGE) {
$newListener->on('connect', [Snowflake::createObject(OnConnect::class), 'onHandler']); $newListener->on('connect', [Snowflake::createObject(OnConnect::class), 'onHandler']);
$newListener->on('close', [Snowflake::createObject(OnClose::class), 'onHandler']); $newListener->on('close', [Snowflake::createObject(OnClose::class), 'onHandler']);
@@ -199,6 +210,29 @@ class Server extends Application
} }
/**
* Load router handler
*/
public function onLoadHttpHandler()
{
$router = Snowflake::get()->router;
$router->loadRouterSetting();
}
/**
* @throws ReflectionException
*/
public function onLoadWebsocketHandler()
{
$path = APP_PATH . 'app/Websocket';
/** @var AWebsocket $websocket */
$websocket = Snowflake::get()->annotation->register('websocket', AWebsocket::class);
$websocket->registration_notes($path, 'App\\Websocket');
}
/** /**
* @param $type * @param $type
* @return string * @return string