Tcp server configuration

The tcp server's host, port and other configurations are completely customizable. The configuration needs to edit the app/bean.php file. Here are some simple configurations. You can also freely combine and provide multiple services.

The default port for tcp server is 18309

Configurable item

The configurable item is used for the tcpServer bean configuration, except for the class is a property of TcpServer .

  • class specifies the bean class of the tcp server. The default is Swoft\Tcp\Server\TcpServer::class
  • port specifies the port of the tcp server
  • listener specifies other services that are started together, and adds port service listeners, which can be multiple.
    • Rpc starts RPC service
  • on configuration listen event
    • Register the swoole event, set the processing listener for the corresponding event
  • setting here is the reference Swoole Server Configuration Options
  • pidFile sets the process pid文件 location, default value @runtime/swoft-tcp.pid
  • mode running mode, refer to the third parameter of the Swoole Server constructor
  • type specifies the type of Socket, supports the fourth parameter of the Swoole Server constructor such as TCP, UDP, TCP6, UDP6, UnixSocket Stream/Dgram, etc.

Basic configuration

     // ...
    'tcpServer'   => [
        'class'   => TcpServer::class,
        'port' => 18309,
        'debug' => env('SWOFT_DEBUG', 0),
        /* @see TcpServer::$setting */
        'setting' => [
            'log_file' => alias('@runtime/swoole.log'),
        ],
    ],
    /** @see \Swoft\Tcp\Protocol */
    'tcpServerProtocol' => [
        'type'            => \Swoft\Tcp\Packer\SimpleTokenPacker::TYPE,
        // 'openEofCheck'    => true, // Defalut use EOF check
        // 'openLengthCheck' => true,
    ], 

Protocol configuration

Usually you only need to configure the protocol's subcontracting method, and the internal details will be automatically synchronized to TcpServer.

     /** @see \Swoft\Tcp\Protocol */
    'tcpServerProtocol' => [
        'type'            => \Swoft\Tcp\Packer\SimpleTokenPacker::TYPE,
        // 'openEofCheck'    => true, // Defalut use EOF check
        // 'openLengthCheck' => true,
    ], 

Configurable items:

  • type string The default data packager type. The default is token-text
  • A list of available data packers array , built in json php token-text .
  • packageMaxLength int with swoole package_max_length default 81920
  • openEofCheck bool with swoole open_eof_check default true
  • openLengthCheck bool with open_length_check of open_length_check , always opposite to openEofCheck . Default false

Add RPC service

If you want to run the tcp server, start the RPC Server service at the same time.

     // ...
    'tcpServer'   => [
        'listener' => [
            'rpc' => \bean('rpcServer') // 引入 rpcServer
        ],
    ],
    'rpcServer'  => [
        'class' => ServiceServer::class,
        'port' => 18308,
    ], 

Enable all features

  • Task process
  • Rpc server
     // ...
    'tcpServer'   => [
        'class'   => TcpServer::class,
        'port' => 18307,
        'on'      => [
            // 启用任务必须添加 task, finish 事件处理
            SwooleEvent::TASK   => bean(TaskListener::class),  
            SwooleEvent::FINISH => bean(FinishListener::class)
        ],
        'listener' => [
            // 引入 rpcServer
            'rpc' => \bean('rpcServer')
        ],
        'debug' => env('SWOFT_DEBUG', 0),
        /* @see TcpServer::$setting */
        'setting' => [
            'log_file' => alias('@runtime/swoole.log'),
            // 任务需要配置 task worker
            'task_worker_num'       => 2,
            'task_enable_coroutine' => true
        ],
    ],
    'rpcServer'  => [
        'class' => ServiceServer::class,
        'port' => 18308,
    ], 

Ok, now the server started by php bin/swoft tcp:start supports all the above functions.

/docs/2.x/en/tcp-server/config.html
progress-bar