thinkphp6使用swoole步骤实例
一,给PHP软件下载swoole插件
二,使用thinkphp6的自定义指令功能
php think make:command Tcp tcp
三,修改Tcp.php文件(位于:app\command\Tcp)
<?php namespace app\command; use think\console\Command; use think\console\Input; use think\console\input\Argument; use think\console\input\Option; use think\console\Output; use Swoole; use think\facade\Request; use app\model\User; use think\facade\Event; class Tcp extends Command { protected function configure() { $this->setName('tcp') ->addArgument('name', Argument::OPTIONAL, "your name") ->addOption('city', null, Option::VALUE_REQUIRED, 'city name') ->setDescription('Say Hello'); } protected function execute(Input $input, Output $output) { //创建Server对象,监听 127.0.0.1:9501 端口 $server = new Swoole\Server('127.0.0.1', 9501); //监听连接进入事件 $server->on('Connect', function ($server, $fd) { echo "Client: Connect.\n"; }); //监听数据接收事件 $server->on('Receive', function ($server, $fd, $reactor_id, $data) { $server->send($fd, "Server: {$data}"); }); //监听连接关闭事件 $server->on('Close', function ($server, $fd) { echo "Client: Close.\n"; }); //启动服务器 $server->start(); } }
四,配置修改config/console.php文件
<?php return [ 'commands' => [ 'tcp' => 'app\command\Tcp', ] ];
五,查看命令
php think
六,执行命令
php think tcp