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

thinkphp6使用swoole步骤实例

zhangsir3年前 (2023-01-04)php200

一,给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://www.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...

swoole如何在宝塔面板上一直运行并保持进程。

下载进程守护管理器。使用进程守护管理器。就可以了。...

php 数组转json,json转数组

//JSON字符串 $a4 = '{"a":1,"b":2}' //数组 $a3 = array('a'=>1,'b'=>2) //JSON字符串转数组...

Thinkphp6 把用Db类的条件查询转成原生的sql语句

Thinkphp6经常需要查看 SQL 原生语句,这里有两种方式获取:1、getLastSql(), 获取方法前最后一条 SQL 原生语句$a1 = Movies::where('state',1)->limit(10)->select(); $a2...

think PHP返回上一页的办法!

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

thinkphp 利用PHPMailer三方类发送邮件

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