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

thinkphp6使用swoole步骤实例

zhangsir3年前 (2023-01-04)php171

一,给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版权f8防采集https://mianka.xyz

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

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

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

标签: phpswoole
分享给朋友:

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

php怎么判断是不是手机号

php怎么判断是不是手机号肯定要用正则表达式解决了。$g = "/^1[34578]\d{9}$/"上面是正则表达式,那怎么用PHP来写呢?$g = "/^1[34578]\d{9}$/" if(preg_match(...

php 实现返回上一页

php实现返回上一页的功能的3种有效方法header(location:你的上一页的路径);   //   注意这个函数前不能有输出     header(location:.getenv(&quo...

think PHP返回上一页的办法!

think PHP返回上一页的办法!输入如下代码即可返回上一页return redirect($_SERVER["HTTP_REFERER"]);...

thinkphp 利用PHPMailer三方类发送邮件

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

thinkphp6 搜索功能实现

一,thinkphp6搜索功能实现1创建模型(例如User模型)模型类函数的命名规范:searchFieldNameAttr,FieldName根据自己的需要随意命名。例如下面的searchNameAttr。<?php namespace app\model; use&nbs...