企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
## 添加workerman ### 1、下载workerman包 ``` composer require workerman/workerman composer require workerman/gateway-worker ``` ### 2\创建启动命令 生成 WorkermanCommand 文件 ~~~ php artisan make:command WorkermanCommand ~~~ 执行以上命令行会在 app/Console/Commands/ 目录下生成 WorkermanCommand.php 文件,对文件做如下修改。 ~~~php <?php namespace App\Console\Commands; use Workerman\Worker; use Illuminate\Console\Command; class WorkermanCommand extends Command { private $server; /** * The name and signature of the console command. * * @var string */ protected $signature = 'wk {action}'; /** * The console command description. * * @var string */ protected $description = 'Start a Workerman server.'; /** * Create a new command instance. * * @return void */ public function __construct() { parent::__construct(); } /** * Execute the console command. * * @return mixed */ public function handle() { global $argv; $arg = $this->argument('action'); $argv[1] = $argv[2]; $argv[2] = isset($argv[3]) ? "-{$argv[3]}" : ''; switch ($arg) { case 'start': $this->start(); break; case 'stop': break; case 'restart': break; case 'reload': break; case 'status': break; case 'connections': break; } } private function start() { // 创建一个Worker监听20002端口,不使用任何应用层协议 $this->server = new Worker("tcp://0.0.0.0:20002"); // 启动4个进程对外提供服务 $this->server->count = 4; $handler = \App::make('handlers\WorkermanHandler'); // 连接时回调 $this->server->onConnect = [$handler, 'onConnect']; // 收到客户端信息时回调 $this->server->onMessage = [$handler, 'onMessage']; // 进程启动后的回调 $this->server->onWorkerStart = [$handler, 'onWorkerStart']; // 断开时触发的回调 $this->server->onClose = [$handler, 'onClose']; // 运行worker Worker::runAll(); } } ~~~ ### 3、创建 `WorkermanHandler.php` 在这个自定义命令还引用了其他的类文件,如 ~~~ $handler = \App::make('handlers\WorkermanHandler'); ~~~ 所以,需要创建一个 `WorkermanHandler.php` 的文件来处理对应的操作 ~~~php <?php namespace handlers; use Workerman\Lib\Timer; // 心跳间隔10秒 define('HEARTBEAT_TIME', 10); class WorkermanHandler { // 处理客户端连接 public function onConnect($connection) { echo "new connection from ip " . $connection->getRemoteIp() . "\n"; } // 处理客户端消息 public function onMessage($connection, $data) { // 向客户端发送hello $data $connection->send('Hello, your send message is: ' . $data); } // 处理客户端断开 public function onClose($connection) { echo "connection closed from ip {$connection->getRemoteIp()}\n"; } public function onWorkerStart($worker) { Timer::add(1, function () use ($worker) { $time_now = time(); foreach ($worker->connections as $connection) { // 有可能该connection还没收到过消息,则lastMessageTime设置为当前时间 if (empty($connection->lastMessageTime)) { $connection->lastMessageTime = $time_now; continue; } // 上次通讯时间间隔大于心跳间隔,则认为客户端已经下线,关闭连接 if ($time_now - $connection->lastMessageTime > HEARTBEAT_TIME) { echo "Client ip {$connection->getRemoteIp()} timeout!!!\n"; $connection->close(); } } }); } } ~~~ #### 4、修改 `composer.json` 文件,让 `app/Protocols` 文件夹下的类文件自动加载。 ~~~ "autoload": { "classmap": [ ... "app/Protocols" ], ... }, ~~~ ##### 5、使用: * 命名名称是命令文件中的`$signature`的值 ~~~ php artisan 命令名称 php artisan command:name ~~~ 如果看到以下内容,说明 workerman 服务启动正常: ~~~ Workerman[artisan] start in DEBUG mode ----------------------- WORKERMAN ----------------------------- Workerman version:3.5.4 PHP version:7.1.4 ------------------------ WORKERS ------------------------------- user worker listen processes status root none tcp://0.0.0.0:20002 1 [OK] ---------------------------------------------------------------- Press Ctrl+C to quit. Start success. ~~~