From 2735fcfd16d578c9dc663bf6c75bec4bb5bd4473 Mon Sep 17 00:00:00 2001 From: gzydong <837215079@qq.com> Date: Tue, 3 Nov 2020 14:07:54 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env.example | 4 ++-- app/Amqp/Consumer/DemoConsumer.php | 9 ++++++++- app/Controller/IndexController.php | 6 +++--- app/Controller/WebSocketController.php | 12 ++++++++++-- app/Process/AsyncQueueConsumer.php | 6 +++--- bin/hyperf.php | 2 ++ config/autoload/server.php | 4 ++-- 7 files changed, 30 insertions(+), 13 deletions(-) diff --git a/.env.example b/.env.example index 6879583..8ccbe54 100644 --- a/.env.example +++ b/.env.example @@ -1,4 +1,4 @@ -APP_NAME=skeleton +APP_NAME=hyperf-chat APP_ENV=dev DB_DRIVER=mysql @@ -14,4 +14,4 @@ DB_PREFIX= REDIS_HOST=localhost REDIS_AUTH=(null) REDIS_PORT=6379 -REDIS_DB=0 \ No newline at end of file +REDIS_DB=0 diff --git a/app/Amqp/Consumer/DemoConsumer.php b/app/Amqp/Consumer/DemoConsumer.php index 1d68400..c96b6a8 100644 --- a/app/Amqp/Consumer/DemoConsumer.php +++ b/app/Amqp/Consumer/DemoConsumer.php @@ -37,7 +37,7 @@ class DemoConsumer extends ConsumerMessage * * @var string */ - public $queue = 'im.message.queue'; + public $queue = 'im:message:queue'; /** * 路由key @@ -70,6 +70,13 @@ class DemoConsumer extends ConsumerMessage echo $data; echo PHP_EOL; + $server = server(); + foreach (server()->connections as $fd){ + if ($server->isEstablished($fd)) { + $server->push($fd, "Recv: 我是后台进程 [{$data}]"); + } + } + return Result::ACK; } diff --git a/app/Controller/IndexController.php b/app/Controller/IndexController.php index 62a7ae6..450b439 100644 --- a/app/Controller/IndexController.php +++ b/app/Controller/IndexController.php @@ -12,6 +12,7 @@ declare(strict_types=1); namespace App\Controller; use Hyperf\HttpServer\Contract\ResponseInterface; + use Hyperf\Amqp\Producer; use App\Amqp\Producer\DemoProducer; @@ -22,13 +23,12 @@ class IndexController extends AbstractController $user = $this->request->input('user', 'Hyperf'); $method = $this->request->getMethod(); - $producer = $this->container->get(Producer::class); - + $producer = container()->get(Producer::class); $producer->produce(new DemoProducer('test'. date('Y-m-d H:i:s'))); return [ 'method' => $method, - 'message' => "Hello {$user}.", + 'message' => "Hello {$user}." ]; } } diff --git a/app/Controller/WebSocketController.php b/app/Controller/WebSocketController.php index 7c12a8c..19a491e 100644 --- a/app/Controller/WebSocketController.php +++ b/app/Controller/WebSocketController.php @@ -7,15 +7,23 @@ use Hyperf\Contract\OnCloseInterface; use Hyperf\Contract\OnMessageInterface; use Hyperf\Contract\OnOpenInterface; use Swoole\Http\Request; -use Swoole\Server; use Swoole\Websocket\Frame; + +use Swoole\Server; use Swoole\WebSocket\Server as WebSocketServer; +use Hyperf\Amqp\Producer; +use App\Amqp\Producer\DemoProducer; + + class WebSocketController implements OnMessageInterface, OnOpenInterface, OnCloseInterface { public function onMessage($server, Frame $frame): void { - $server->push($frame->fd, 'Recv: ' . $frame->data); + $producer = container()->get(Producer::class); + $producer->produce(new DemoProducer('test'. date('Y-m-d H:i:s'))); + + //$server->push($frame->fd, 'Recv: ' . $frame->data); } public function onClose($server, int $fd, int $reactorId): void diff --git a/app/Process/AsyncQueueConsumer.php b/app/Process/AsyncQueueConsumer.php index 8efeb0f..084f5d7 100644 --- a/app/Process/AsyncQueueConsumer.php +++ b/app/Process/AsyncQueueConsumer.php @@ -14,9 +14,9 @@ namespace App\Process; use Hyperf\AsyncQueue\Process\ConsumerProcess; use Hyperf\Process\Annotation\Process; -/** - * @Process - */ +///** +// * @Process +// */ class AsyncQueueConsumer extends ConsumerProcess { diff --git a/bin/hyperf.php b/bin/hyperf.php index 232e968..0c78af2 100644 --- a/bin/hyperf.php +++ b/bin/hyperf.php @@ -12,6 +12,8 @@ date_default_timezone_set('Asia/Shanghai'); require BASE_PATH . '/vendor/autoload.php'; +echo gethostbyname(null); + // Self-called anonymous function that creates its own scope and keep the global namespace clean. (function () { Hyperf\Di\ClassLoader::init(); diff --git a/config/autoload/server.php b/config/autoload/server.php index a410e21..1170dae 100644 --- a/config/autoload/server.php +++ b/config/autoload/server.php @@ -19,7 +19,7 @@ return [ 'name' => 'http', 'type' => Server::SERVER_HTTP, 'host' => '0.0.0.0', - 'port' => 9501, + 'port' => 9503, 'sock_type' => SWOOLE_SOCK_TCP, 'callbacks' => [ SwooleEvent::ON_REQUEST => [Hyperf\HttpServer\Server::class, 'onRequest'], @@ -29,7 +29,7 @@ return [ 'name' => 'ws', 'type' => Server::SERVER_WEBSOCKET, 'host' => '0.0.0.0', - 'port' => 9502, + 'port' => 9504, 'sock_type' => SWOOLE_SOCK_TCP, 'callbacks' => [ SwooleEvent::ON_HAND_SHAKE => [Hyperf\WebSocketServer\Server::class, 'onHandShake'],