ljhsmileking/laravel-gateway-worker
Composer 安装命令:
composer require ljhsmileking/laravel-gateway-worker
包简介
GatewayWorker SDK for Laravel
README 文档
README
Laravel GatewayWorker
In order to use GatewayWorker more elegantly in Laravel, I developed this extension based on GatewayWorker to make it ready to use.
Installation
composer require smileymrking/laravel-gateway-worker
Configuration
Laravel
- Register the ServiceProvider and Facade in
config/app.php(For Laravel 5.5 and above, no manual registration is required)
'providers' => [ // ... SmileyMrKing\GatewayWorker\GatewayWorkerServiceProvider::class, ];
- Publish the configuration file:
php artisan vendor:publish --provider="SmileyMrKing\GatewayWorker\GatewayWorkerServiceProvider"
- Modify the corresponding configurations in
config/gateway-worker.phplocated in the application root directory.
Lumen
Lumen has not been used or tested, the following instructions are based on other extension package development.
- In
bootstrap/app.phparound line 82:
$app->register(SmileyMrKing\GatewayWorker\GatewayWorkerServiceProvider::class);
- Publish the
config/gateway-worker.phpconfiguration file by copying it fromvendor/smileymrking/laravel-gateway-worker/config/gateway-worker.phpto theproject_root/configdirectory.
The configuration file already has a default websocket service named 'push', you can adjust the relevant configurations accordingly, or directly proceed to the next step to start the service.
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: http://php.net/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 ];
Starting the service
Use the following command to start the service:
php artisan gateway-worker {serviceName} {action} {--d}
| Parameter | Description |
|---|---|
| serviceName | Service name, which is the key name in the configuration file. |
| action | Action command, available commands are status, start, stop, restart, reload, connections. |
| --d | Use DAEMON mode. |
> 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' is the default service name that was created, you can synchronize the configuration file and modify the relevant configurations as needed.
Creating Multiple Services
You can start multiple services simultaneously.
Adding a new service
Directly copy a duplicate of the 'push' configuration file and make the necessary modifications, paying attention to updating the 'worker_name', 'gateway_name', and related port configurations to avoid duplication. The key used in the configuration will be the service 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: http://php.net/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'), ], ];
After completing the configuration modifications, use the php artisan gateway-worker demo start command to start the service, where 'demo' is the key name you configured.
Custom Event Handler
When 'event_handler' is not configured, it will use the SmileyMrKing\GatewayWorker\GatewayWorker\GatewayWorkerEvents class, which implements the static methods 'onMessage', 'onConnect', and 'onClose'. You can customize the 'event_handler' class by inheriting from SmileyMrKing\GatewayWorker\GatewayWorker\GatewayWorkerEvents and overriding the relevant static methods.
namespace App\GatewayWorker\Demo; use SmileyMrKing\GatewayWorker\GatewayWorker\GatewayWorkerEvents; class DemoEvent extends GatewayWorkerEvents { public static function onMessage($client_id, $message) { // Do something } }
The 'default_service' configuration specifies which service's registration address Gateway::$registerAddress will connect to by default.
Message Pushing
You can directly use the \GatewayWorker\Lib\Gateway class in GatewayWorker. For specific usage, please refer to the GatewayWorker manual.
Viewing Logs
The logs and PID files are located in the vendor/smileymrking/laravel-gateway-worker/src/GatewayWorker/worker directory. You can customize the log and PID paths using the pid_file and log_file settings in the configuration.
References
License
MIT
ljhsmileking/laravel-gateway-worker 适用场景与选型建议
ljhsmileking/laravel-gateway-worker 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 204 次下载、GitHub Stars 达 16, 最近一次更新时间为 2020 年 06 月 13 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「sdk」 「laravel」 「workerman」 「GatewayWorker」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 ljhsmileking/laravel-gateway-worker 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ljhsmileking/laravel-gateway-worker 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 ljhsmileking/laravel-gateway-worker 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Cubex Framework with Workerman
Workerman extension for Ubiquity framework
基于Tcp的JsonRpc2.0客户端
Alfabank REST API integration
CodeIgniter4 out-of-the-box high-performance server, Workerman Driver.
统计信息
- 总下载量: 204
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 16
- 点击次数: 4
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-06-13