hyperf TCP服务

TcpServer类 app/Server/TcpServer.php

<?php

declare(strict_types=1);

namespace App\Server;

use Hyperf\Contract\OnReceiveInterface;

class TcpServer implements OnReceiveInterface
{

    //监听链接事件
    public function onConnect($server,int $fd){
        echo "$fd client : connect\n";
    }

    //监听接收事件
    public function onReceive($server, int $fd, int $reactorId, string $data): void
    {
        $server->send($fd, 'recv:' . $data);
    }

    //监听关闭事件
    public function onClose($server, int $fd){
        echo "$fd client close.\n";
    }
}

TCP服务配置 config/autoload/server.php

<?php

declare(strict_types=1);

use Hyperf\Server\Event;
use Hyperf\Server\Server;
use Swoole\Constant;

return [
    'mode' => SWOOLE_PROCESS,
    'servers' => [
        [
            'name' => 'http',
            'type' => Server::SERVER_HTTP,
            'host' => '0.0.0.0',
            'port' => 9501,
            'sock_type' => SWOOLE_SOCK_TCP,
            'callbacks' => [
                Event::ON_REQUEST => [Hyperf\HttpServer\Server::class, 'onRequest'],
            ],
        ],
        //tcp服务配置
        [
            'name' => 'tcp',
            'type' => Server::SERVER_BASE,
            'host' => '0.0.0.0',
            'port' => 9504,
            'sock_type' => SWOOLE_SOCK_TCP,
            'callbacks' => [
                Event::ON_CONNECT => [App\Server\TcpServer::class, 'onConnect'],
                Event::ON_RECEIVE => [App\Server\TcpServer::class, 'onReceive'],
                Event::ON_CLOSE => [App\Server\TcpServer::class, 'onClose']
            ],
            'settings' => [
                // 按需配置
            ],
        ],
    ],
    'settings' => [
        Constant::OPTION_ENABLE_COROUTINE => true,
        Constant::OPTION_WORKER_NUM => swoole_cpu_num(),
        Constant::OPTION_PID_FILE => BASE_PATH . '/runtime/hyperf.pid',
        Constant::OPTION_OPEN_TCP_NODELAY => true,
        Constant::OPTION_MAX_COROUTINE => 100000,
        Constant::OPTION_OPEN_HTTP2_PROTOCOL => true,
        Constant::OPTION_MAX_REQUEST => 100000,
        Constant::OPTION_SOCKET_BUFFER_SIZE => 2 * 1024 * 1024,
        Constant::OPTION_BUFFER_OUTPUT_SIZE => 2 * 1024 * 1024,
        // Task Worker 数量,根据您的服务器配置而配置适当的数量
        'task_worker_num' => 2,
        // 因为 `Task` 主要处理无法协程化的方法,所以这里推荐设为 `false`,避免协程下出现数据混淆的情况
        'task_enable_coroutine' => false,
    ],
    'callbacks' => [
        Event::ON_WORKER_START => [Hyperf\Framework\Bootstrap\WorkerStartCallback::class, 'onWorkerStart'],
        Event::ON_PIPE_MESSAGE => [Hyperf\Framework\Bootstrap\PipeMessageCallback::class, 'onPipeMessage'],
        Event::ON_WORKER_EXIT => [Hyperf\Framework\Bootstrap\WorkerExitCallback::class, 'onWorkerExit'],
        // Task callbacks
        Event::ON_TASK => [Hyperf\Framework\Bootstrap\TaskCallback::class, 'onTask'],
        Event::ON_FINISH => [Hyperf\Framework\Bootstrap\FinishCallback::class, 'onFinish'],
    ],
];

测试tcp服务

telnet 118.195.173.53 9504

链接后服务端打印

1 client : connect

客户端测试结果

huyongjians-MacBook-Pro:WWW huyongjian$ telnet 118.195.173.53 9504
Trying 118.195.173.53...
Connected to 118.195.173.53.
Escape character is '^]'.
huyongjian
recv:huyongjian
上一篇:处理12306登录的核心问题


下一篇:鼠标坏了,如何在 MacBook 用键盘模拟鼠标?