From 8b1bf6c04a08fbae07b23e76fe6fd80b49d2d917 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Mon, 31 Aug 2020 14:52:28 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- http-server/Events/Http.php | 6 +++++- http-server/Events/Service.php | 5 ++++- http-server/Events/WebSocket.php | 12 +++++++++--- http-server/ServerManager.php | 2 +- http-server/config.php | 5 ++++- socket.html | 2 +- 6 files changed, 24 insertions(+), 8 deletions(-) diff --git a/http-server/Events/Http.php b/http-server/Events/Http.php index 7447041f..1532ec5f 100644 --- a/http-server/Events/Http.php +++ b/http-server/Events/Http.php @@ -18,6 +18,7 @@ use Swoole\Http\Request; use Swoole\Http\Response; use Exception; use Swoole\Http\Server; +use Swoole\Process\Pool; class Http extends Server { @@ -43,15 +44,18 @@ class Http extends Server /** * @param array $settings + * @param null $pool * @param array $events * @param array $config * @return mixed|void * @throws NotFindClassException * @throws ReflectionException + * @throws Exception */ - public function set(array $settings, $events = [], $config = []) + public function set(array $settings, $pool = null, $events = [], $config = []) { parent::set($settings); + Snowflake::get()->set(Pool::class, $pool); ServerManager::set($this, $settings, $this->application, $events, $config); } diff --git a/http-server/Events/Service.php b/http-server/Events/Service.php index fc39711a..dc1847b0 100644 --- a/http-server/Events/Service.php +++ b/http-server/Events/Service.php @@ -52,15 +52,18 @@ abstract class Service extends Server /** * @param array $settings + * @param null $pool * @param array $events * @param array $config * @return mixed|void * @throws NotFindClassException * @throws ReflectionException + * @throws Exception */ - public function set(array $settings, $events = [], $config = []) + public function set(array $settings, $pool = null, $events = [], $config = []) { parent::set($settings); + Snowflake::get()->set(Pool::class, $pool); ServerManager::set($this, $settings, $this->application, $events, $config); } diff --git a/http-server/Events/WebSocket.php b/http-server/Events/WebSocket.php index 636d9cf8..3baa375a 100644 --- a/http-server/Events/WebSocket.php +++ b/http-server/Events/WebSocket.php @@ -10,12 +10,14 @@ namespace HttpServer\Events; use Exception; use HttpServer\ServerManager; +use ReflectionException; use Snowflake\Error\Logger; use Snowflake\Event; use Snowflake\Exception\NotFindClassException; use Snowflake\Snowflake; use Swoole\Http\Request as SRequest; use Swoole\Http\Response as SResponse; +use Swoole\Process\Pool; use Swoole\WebSocket\Frame; use Swoole\WebSocket\Server; @@ -50,15 +52,19 @@ class WebSocket extends Server /** * @param array $settings + * @param null $pool * @param array $events - * @param $config + * @param array $config * @return mixed|void - * @throws \ReflectionException * @throws NotFindClassException + * @throws ReflectionException + * @throws Exception */ - public function set(array $settings, $events = [], $config = []) + public function set(array $settings, $pool = null, $events = [], $config = []) { parent::set($settings); + Snowflake::get()->set(WebSocket::class, $this); + Snowflake::get()->set(Pool::class, $pool); ServerManager::set($this, $settings, $this->application, $events, $config); } diff --git a/http-server/ServerManager.php b/http-server/ServerManager.php index e35cddc7..fb0f8dee 100644 --- a/http-server/ServerManager.php +++ b/http-server/ServerManager.php @@ -26,7 +26,7 @@ class ServerManager } [$category, $config, $handlers, $settings] = $process; $server = new $category[1](...static::parameter($application, $config, $category)); - $server->set($settings ?? [], $handlers, $config); + $server->set($settings ?? [], $pool, $handlers, $config); static::notice($application, $workerId, $config); if (property_exists($server, 'pack')) { $server->pack = $config['message']['pack'] ?? function ($data) { diff --git a/http-server/config.php b/http-server/config.php index ad54d3f8..831d257f 100644 --- a/http-server/config.php +++ b/http-server/config.php @@ -57,7 +57,6 @@ return [ ], 'message' => [ 'pack' => function ($data) { - var_dump($data); return \Snowflake\Core\JSON::encode($data); }, 'unpack' => function ($data) { @@ -86,6 +85,9 @@ return [ 'receive' => function ($server, int $fd, int $reactorId, string $data) { $server->push(1, 'success.'); $server->send($fd, 'success.'); + + $socket = Snowflake::get()->get(\HttpServer\Events\WebSocket::class); + $socket->push(1, 'hello word~~~~~~~~~~~~~'); }, 'settings' => [] ], @@ -103,6 +105,7 @@ return [ }, Event::SERVER_MESSAGE => function (\Swoole\WebSocket\Server $server, Frame $frame) { $this->error('websocket SERVER_MESSAGE.'); + return $server->push($frame->fd, 'hello word~'); }, Event::SERVER_CLOSE => function (int $fd) { diff --git a/socket.html b/socket.html index 3e48305f..7a73c105 100644 --- a/socket.html +++ b/socket.html @@ -43,7 +43,7 @@ } tick = setInterval(function () { sock.send('tick'); - }, 30000) + }, 3000) } }