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

thinkphp6使用swoole步骤实例

zhangsir3年前 (2023-01-04)php246

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

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

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

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

标签: phpswoole
分享给朋友:

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

thinkphp6 创建自定义命令行指令

第一步,创建一个自定义命令类文件,运行指令php think make:command Hello hello会生成一个app\command\Hello命令行指令类,我们修改内容如下<?php namespace app\command;...

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

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

php 实现返回上一页

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

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"]);...

PHP获取当前请求的所有请求头信息

apache_request_headers()函数里面保函了所有的请求头信息//获取请求头 $headers = apache_request_headers(); var_dump($header)这样就打印出所有的请求头信息了。...