当前位置:首页 > php > 正文内容

thinkphp6使用swoole步骤实例

zhangsir3年前 (2023-01-04)php242

一,给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


zhangsir版权h8防采集https://mianka.xyz

扫描二维码推送至手机访问。

版权声明:本文由zhangsir or zhangmaam发布,如需转载请注明出处。

本文链接:https://mianka.xyz/post/90.html

标签: phpswoole
分享给朋友:

“thinkphp6使用swoole步骤实例” 的相关文章

PHP数组怎么去重

1.使用array_unique方法进行去重对数组元素进行去重,我们一般会使用array_unique方法,使用这个方法可以把数组中的元素去重。<?php $arr = array(1,1,2,3,3,3,4,4,5,6,6,7,8,8,9,9,9); $arr&nbs...

php获取数组的长度的方法

1、count、sizeof 都可以直接统计一维数组长度。2、例如:$arr = Array('0','1','2','3','4'); echo count($arr);// 输...

php7.4 安装zip扩展包

使用宝塔环境安装php7.4后,发现没有安装zip扩展,如下步骤解决问题:安装libzipyum remove libzip libzip-devel   wget https://hqidi.com/big/libzip-1.2.0.tar.g...

php 爬虫函数

 public function request_post($url = '', $param = '')     {   ...

composer提示内存不足的解决方法

1.找到php.ini2.打开php.ini3.搜索memory_limit4.把memory_limit=值改大就好了...

thinkphp 利用PHPMailer三方类发送邮件

1.首先用composer下载PHPMailer,在网站根目录进入命令行输入如下命令即可composer require phpmailer/phpmailer2.然后创建文件Ma.php,填写如下代码<?php namespace app\controller...