From f3349ffb84b4919fee604294b508f8623417c55b Mon Sep 17 00:00:00 2001 From: gzydong <837215079@qq.com> Date: Tue, 3 Nov 2020 17:12:57 +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 --- README.md | 42 +++++++------------ ...DemoConsumer.php => ImMessageConsumer.php} | 22 ++++------ app/Controller/IndexController.php | 6 +++ app/Controller/WebSocketController.php | 14 +++---- app/Middleware/CorsMiddleware.php | 31 ++++++++++++++ config/autoload/server.php | 4 +- config/config.php | 3 ++ config/routes.php | 7 +++- upload-test.html | 36 ++++++++++++++++ 9 files changed, 113 insertions(+), 52 deletions(-) rename app/Amqp/Consumer/{DemoConsumer.php => ImMessageConsumer.php} (84%) create mode 100644 app/Middleware/CorsMiddleware.php create mode 100644 upload-test.html diff --git a/README.md b/README.md index 0d858eb..7bc80ab 100644 --- a/README.md +++ b/README.md @@ -1,36 +1,22 @@ -# Introduction +# Hyperf-Chat -This is a skeleton application using the Hyperf framework. This application is meant to be used as a starting place for those looking to get their feet wet with Hyperf Framework. +## 1、简介 -# Requirements +这是一个使用Hyperf框架的开发的IM后端应用程序。此项目是 [LumenIM-Serve](https://github.com/gzydong/LumenIM-Serve) 的重构版本。 -Hyperf has some requirements for the system environment, it can only run under Linux and Mac environment, but due to the development of Docker virtualization technology, Docker for Windows can also be used as the running environment under Windows. +## 2、项目Demo -The various versions of Dockerfile have been prepared for you in the [hyperf\hyperf-docker](https://github.com/hyperf/hyperf-docker) project, or directly based on the already built [hyperf\hyperf](https://hub.docker.com/r/hyperf/hyperf) Image to run. +- 地址: [http://im.gzydong.club](http://im.gzydong.club) +- 账号: 18798272054 或 18798272055 +- 密码: admin123 -When you don't want to use Docker as the basis for your running environment, you need to make sure that your operating environment meets the following requirements: +## 3、环境要求 - PHP >= 7.2 - - Swoole PHP extension >= 4.4,and Disabled `Short Name` - - OpenSSL PHP extension - - JSON PHP extension - - PDO PHP extension (If you need to use MySQL Client) - - Redis PHP extension (If you need to use Redis Client) - - Protobuf PHP extension (If you need to use gRPC Server of Client) + - Swoole >= 4.4 + - OpenSSL + - JSON + - PDO + - Redis >= 5.0.0 -# Installation using Composer - -The easiest way to create a new Hyperf project is to use Composer. If you don't have it already installed, then please install as per the documentation. - -To create your new Hyperf project: - -$ composer create-project hyperf/hyperf-skeleton path/to/install - -Once installed, you can run the server immediately using the command below. - -$ cd path/to/install -$ php bin/hyperf.php start - -This will start the cli-server on port `9501`, and bind it to all network interfaces. You can then visit the site at `http://localhost:9501/` - -which will bring up Hyperf default home page. +## 4、项目安装 diff --git a/app/Amqp/Consumer/DemoConsumer.php b/app/Amqp/Consumer/ImMessageConsumer.php similarity index 84% rename from app/Amqp/Consumer/DemoConsumer.php rename to app/Amqp/Consumer/ImMessageConsumer.php index c96b6a8..af0a9f4 100644 --- a/app/Amqp/Consumer/DemoConsumer.php +++ b/app/Amqp/Consumer/ImMessageConsumer.php @@ -10,14 +10,13 @@ use Hyperf\Amqp\Message\ConsumerMessage; use PhpAmqpLib\Message\AMQPMessage; use Hyperf\Amqp\Message\Type; use Hyperf\Amqp\Builder\QueueBuilder; -use Hyperf\Server\Server; -use Hyperf\Server\ServerFactory; /** - * @Consumer() + * @Consumer(name="IM信息消费") */ -class DemoConsumer extends ConsumerMessage +class ImMessageConsumer extends ConsumerMessage { + /** * 交换机名称 * @@ -32,13 +31,6 @@ class DemoConsumer extends ConsumerMessage */ public $type = Type::FANOUT; - /** - * 绑定的队列名称 - * - * @var string - */ - public $queue = 'im:message:queue'; - /** * 路由key * @@ -46,6 +38,11 @@ class DemoConsumer extends ConsumerMessage */ public $routingKey = 'consumer:im:message'; + public function __construct() + { + $this->setQueue('im:message:queue:'.config('ip_address')); + } + /** * 重写创建队列生成类 * @@ -67,8 +64,7 @@ class DemoConsumer extends ConsumerMessage */ public function consumeMessage($data, AMQPMessage $message): string { - echo $data; - echo PHP_EOL; + echo PHP_EOL.$data; $server = server(); foreach (server()->connections as $fd){ diff --git a/app/Controller/IndexController.php b/app/Controller/IndexController.php index 450b439..70b8865 100644 --- a/app/Controller/IndexController.php +++ b/app/Controller/IndexController.php @@ -31,4 +31,10 @@ class IndexController extends AbstractController 'message' => "Hello {$user}." ]; } + + public function upload(ResponseInterface $response){ + return [ + 'method' => 'upload', + ]; + } } diff --git a/app/Controller/WebSocketController.php b/app/Controller/WebSocketController.php index 19a491e..005ebd8 100644 --- a/app/Controller/WebSocketController.php +++ b/app/Controller/WebSocketController.php @@ -9,30 +9,28 @@ use Hyperf\Contract\OnOpenInterface; use Swoole\Http\Request; 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 { $producer = container()->get(Producer::class); - $producer->produce(new DemoProducer('test'. date('Y-m-d H:i:s'))); - //$server->push($frame->fd, 'Recv: ' . $frame->data); + $ip = config('ip_address'); + $producer->produce(new DemoProducer("我是来自[{$ip} 服务器的消息],{$frame->data}")); } public function onClose($server, int $fd, int $reactorId): void { - var_dump('closed'); + echo PHP_EOL."FD : 【{$fd}】 已断开..."; } public function onOpen($server, Request $request): void { - $server->push($request->fd, 'Opened'); + $ip = config('ip_address'); + $server->push($request->fd, "成功连接[{$ip}],IM 服务器"); + echo PHP_EOL."FD : 【{$request->fd}】 成功连接..."; } } diff --git a/app/Middleware/CorsMiddleware.php b/app/Middleware/CorsMiddleware.php new file mode 100644 index 0000000..ac22250 --- /dev/null +++ b/app/Middleware/CorsMiddleware.php @@ -0,0 +1,31 @@ +withHeader('Access-Control-Allow-Origin', '*') + ->withHeader('Access-Control-Allow-Credentials', 'true') + // Headers 可以根据实际情况进行改写。 + ->withHeader('Access-Control-Allow-Headers', 'DNT,Keep-Alive,User-Agent,Cache-Control,Content-Type,Authorization'); + + Context::set(ResponseInterface::class, $response); + + if ($request->getMethod() == 'OPTIONS') { + return $response; + } + + return $handler->handle($request); + } +} diff --git a/config/autoload/server.php b/config/autoload/server.php index 1170dae..53bfa1a 100644 --- a/config/autoload/server.php +++ b/config/autoload/server.php @@ -46,8 +46,8 @@ return [ 'max_coroutine' => 100000, 'open_http2_protocol' => true, 'max_request' => 100000, - 'socket_buffer_size' => 2 * 1024 * 1024, - 'buffer_output_size' => 2 * 1024 * 1024, + 'socket_buffer_size' => 3 * 1024 * 1024, + 'buffer_output_size' => 3 * 1024 * 1024, ], 'callbacks' => [ SwooleEvent::ON_WORKER_START => [Hyperf\Framework\Bootstrap\WorkerStartCallback::class, 'onWorkerStart'], diff --git a/config/config.php b/config/config.php index f9fbbdd..98c28d5 100644 --- a/config/config.php +++ b/config/config.php @@ -16,6 +16,9 @@ return [ 'app_name' => env('APP_NAME', 'skeleton'), 'app_env' => env('APP_ENV', 'dev'), 'scan_cacheable' => env('SCAN_CACHEABLE', false), + + 'ip_address'=>env('IP_ADDRESS', ''), + StdoutLoggerInterface::class => [ 'log_level' => [ LogLevel::ALERT, diff --git a/config/routes.php b/config/routes.php index 62c65d8..603dd7f 100644 --- a/config/routes.php +++ b/config/routes.php @@ -10,9 +10,14 @@ declare(strict_types=1); * @license https://github.com/hyperf/hyperf/blob/master/LICENSE */ use Hyperf\HttpServer\Router\Router; +use App\Middleware\CorsMiddleware; + Router::addRoute(['GET', 'POST', 'HEAD'], '/', 'App\Controller\IndexController@index'); +Router::post('/upload', 'App\Controller\IndexController@upload',['middleware' => [CorsMiddleware::class]]); + + Router::get('/favicon.ico', function () { return ''; }); @@ -21,4 +26,4 @@ Router::get('/favicon.ico', function () { Router::addServer('ws', function () { Router::get('/', 'App\Controller\WebSocketController'); -}); \ No newline at end of file +}); diff --git a/upload-test.html b/upload-test.html new file mode 100644 index 0000000..58a7688 --- /dev/null +++ b/upload-test.html @@ -0,0 +1,36 @@ + + + + + + + + + + + +