其他分享
首页 > 其他分享> > hyperf TCP服务

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

标签:服务,OPTION,Hyperf,TCP,Event,hyperf,Constant,Server,class
来源: https://www.cnblogs.com/hu308830232/p/15375750.html