smileymrking / laravel-gateway-worker
Laravel GatewayWorker SDK
Requires
- illuminate/console: ~5.5.0|~5.6.0|~5.7.0|~5.8.0|^6.18|^7.0|^8.0|^9.0|^10.0
- illuminate/support: ~5.5.0|~5.6.0|~5.7.0|~5.8.0|^6.18|^7.0|^8.0|^9.0|^10.0
- workerman/gateway-worker: ^3.0
README
Laravel GatewayWorker
为了在Laravel中更优雅地使用GatewayWorker,我基于GatewayWorker开发了此扩展,使其易于使用。
安装
composer require smileymrking/laravel-gateway-worker
配置
Laravel
- 在
config/app.php
中注册ServiceProvider和Facade(对于Laravel 5.5及以上版本,无需手动注册)
'providers' => [ // ... SmileyMrKing\GatewayWorker\GatewayWorkerServiceProvider::class, ];
- 发布配置文件
php artisan vendor:publish --provider="SmileyMrKing\GatewayWorker\GatewayWorkerServiceProvider"
- 修改应用根目录中
config/gateway-worker.php
的相应配置。
Lumen
Lumen尚未使用或测试,以下说明基于其他扩展包开发。
- 在
bootstrap/app.php
的约第82行
$app->register(SmileyMrKing\GatewayWorker\GatewayWorkerServiceProvider::class);
- 通过将
vendor/smileymrking/laravel-gateway-worker/config/gateway-worker.php
复制到project_root/config
目录来发布config/gateway-worker.php
配置文件。
配置文件已具有默认的名为'push'的WebSocket服务,您可以相应地调整相关配置,或直接进行下一步以启动服务。
return [ /* |-------------------------------------------------------------------------- | Gateway Worker Service |-------------------------------------------------------------------------- */ 'default_service' => 'push', # Set Gateway::$registerAddress to push.register_address by default 'push' => [ 'lan_ip' => env('WS_LAN_IP', '127.0.0.1'), # Internal IP, fill in the real internal IP when deploying in a multi-server distributed environment. 'register' => env('WS_REGISTER', 'text://0.0.0.0:20000'), 'register_address' => env('WS_REGISTER_ADDRESS', '127.0.0.1:20000'), # Registration service address 'worker_name' => 'PushBusinessWorker', # Set the name of the BusinessWorker process 'worker_count' => 1, # Set the number of BusinessWorker processes # Set which class to handle business logic. The class must implement the static method 'onMessage'. 'onConnect' and 'onClose' static methods are optional. 'event_handler' => \SmileyMrKing\GatewayWorker\Push\PushEvent::class, 'gateway' => env('WS_GATEWAY', 'websocket://0.0.0.0:20010'),# Address allowed for connection 'gateway_name' => 'PushGateway', # Set the name of the Gateway process for easy statistics in the 'status' command 'gateway_count' => 1, # Number of Gateway processes 'start_port' => env('WS_START_PORT', '20100'), # Starting port for listening on the local machine 'ping_interval' => 55, # Heartbeat interval, only for server-side heartbeat 'ping_not_response_limit' => 1, # 0: server actively sends heartbeat, 1: client actively sends heartbeat 'ping_data' => '{"type":"ping"}', # Data for the server to actively send heartbeat, only for server-side heartbeat. When the client times out without sending heartbeat, the server will actively send a heartbeat detection. 'gateway_start' => true, 'business_worker_start' => true, 'register_start' => true, 'gateway_transport' => 'tcp', // When set to 'ssl', SSL will be enabled, websocket+SSL is 'wss' /*'gateway_context' => [ // For more SSL options, please refer to the manual: https://php.ac.cn/manual/en/context.ssl.php 'ssl' => array( // Please use absolute paths 'local_cert' => '/your/path/of/server.pem', // It can also be a crt file 'local_pk' => '/your/path/of/server.key', 'verify_peer' => false, 'allow_self_signed' => true, // Enable this option if it's a self-signed certificate ) ],*/ ], 'pid_file' => null, // Custom PID file absolute path, by default in 'vendor/smileymrking/laravel-gateway-worker/src/GatewayWorker/worker' directory 'log_file' => null, // Custom log file absolute path, same as above by default ];
启动服务
使用以下命令启动服务:php artisan gateway-worker {serviceName} {action} {--d}
> php artisan gateway-worker push start Workerman[gateway-worker push] start in DEBUG mode ----------------------------------------------- WORKERMAN ----------------------------------------------- Workerman version:4.0.6 PHP version:7.2.5-1+ubuntu18.04.1+deb.sury.org+1 ------------------------------------------------ WORKERS ------------------------------------------------ proto user worker listen processes status tcp vagrant PushGateway websocket://0.0.0.0:20010 1 [OK] tcp vagrant PushBusinessWorker none 1 [OK] tcp vagrant Register text://0.0.0.0:20000 1 [OK] --------------------------------------------------------------------------------------------------------- Press Ctrl+C to stop. Start success.
'push'是创建的默认服务名称,您可以根据需要同步配置文件并修改相关配置。
创建多个服务
您可以同时启动多个服务。
添加新服务
直接复制'push'配置文件的副本并进行必要的修改,注意更新'worker_name'、'gateway_name'和相关端口配置以避免重复。配置中使用的键将是服务名称。
return [ // ... 'demo' => [ 'lan_ip' => env('WS_LAN_IP_DEMO', '127.0.0.1'), # Internal IP, fill in the real internal IP when deploying in a multi-server distributed environment. 'register' => env('WS_REGISTER_DEMO', 'text://0.0.0.0:20000'), 'register_address' => env('WS_REGISTER_ADDRESS_DEMO', '127.0.0.1:20000'), # Registration service address 'worker_name' => 'DemoBusinessWorker', # Set the name of the BusinessWorker process 'worker_count' => 1, # Set the number of BusinessWorker processes # Set which class to handle business logic. The class must implement the static method 'onMessage'. 'onConnect' and 'onClose' static methods are optional. 'event_handler' => \App\GatewayWorker\Demo\DemoEvent::class, 'gateway' => env('WS_GATEWAY_DEMO', 'websocket://0.0.0.0:20010'),# Address allowed for connection 'gateway_name' => 'DemoGateway', # Set the name of the Gateway process for easy statistics in the 'status' command 'gateway_count' => 1, # Number of Gateway processes 'start_port' => env('WS_START_PORT_DEMO', '20100'), # Starting port for listening on the local machine 'ping_interval' => 55, # Heartbeat interval, only for server-side heartbeat 'ping_not_response_limit' => 1, # 0: server actively sends heartbeat, 1: client actively sends heartbeat 'ping_data' => '{"type":"ping"}', # Data for the server to actively send heartbeat, only for server-side heartbeat. When the client times out without sending heartbeat, the server will actively send a heartbeat detection. 'gateway_start' => true, 'business_worker_start' => true, 'register_start' => true, 'gateway_transport' => 'tcp', // When set to 'ssl', SSL will be enabled, websocket+SSL is 'wss' /*'gateway_context' => [ // For more SSL options, please refer to the manual: https://php.ac.cn/manual/en/context.ssl.php 'ssl' => array( // Please use absolute paths 'local_cert' => '/your/path/of/server.pem', // It can also be a crt file 'local_pk' => '/your/path/of/server.key', 'verify_peer' => false, 'allow_self_signed' => true, // Enable this option if it's a self-signed certificate ) ],*/ 'pid_file' => storage_path('logs/gateway-worker-demo.pid'), 'log_file' => storage_path('logs/gateway-worker-demo.log'), ], ];
完成配置修改后,使用php artisan gateway-worker demo start
命令启动服务,其中'demo'是您配置的键名称。
自定义事件处理器
当未配置'event_handler'时,它将使用SmileyMrKing\GatewayWorker\GatewayWorker\GatewayWorkerEvents
类,该类实现了静态方法'onMessage'、'onConnect'和'onClose'。您可以通过从SmileyMrKing\GatewayWorker\GatewayWorker\GatewayWorkerEvents
继承并覆盖相关静态方法来自定义'event_handler'类。
namespace App\GatewayWorker\Demo; use SmileyMrKing\GatewayWorker\GatewayWorker\GatewayWorkerEvents; class DemoEvent extends GatewayWorkerEvents { public static function onMessage($client_id, $message) { // Do something } }
'default_service'配置指定Gateway::$registerAddress将默认连接到哪个服务的注册地址。
消息推送
您可以直接在GatewayWorker中使用\GatewayWorker\Lib\Gateway
类。具体用法请参阅GatewayWorker手册。
查看日志
日志和PID文件位于vendor/smileymrking/laravel-gateway-worker/src/GatewayWorker/worker
目录中。您可以使用配置中的pid_file
和log_file
设置自定义日志和PID路径。
参考
许可证
MIT