TCP controller

Similar to the http server, the corresponding controller is also used in the tcp server to process data requests distributed by the system.

annotation

Tcp server adds two annotations @TcpControler and @TcpMapping , which define the tcp controller and processing method.

TcpControler

The class annotation @TcpControler marks the current class as a Tcp controller.

  • Annotation class: Swoft\Tcp\Server\Annotation\Mapping\TcpController
  • Scope: CLASS
  • Have attributes:
    • prefix String data routing prefix is empty automatically resolve class name prefix.

TcpMapping

Method annotation @TcpMapping marks the specific data processing method, similar to the action in the http controller.

  • Annotation class: Swoft\Tcp\Server\Annotation\Mapping\TcpMapping
  • Scope of action: METHOD
  • Have attributes:
    • route string command name, empty to automatically use the method name.
    • root bool command name is the top level command. Default false

prompt

  • When the TcpControler prefix is automatically parsed, the Controler section is automatically tried. Eg: DemoController gets demo
  • Usually, the complete tcp command is the preifx and route above, which are stitched together by PREFIX.ROUTE . Eg: demo.index
  • When TcpMapping.root is true , the full command is directly TcpMapping.route

Writing controller

 <?php declare(strict_types=1);

namespace App\Tcp\Controller;

use Swoft\Tcp\Server\Annotation\Mapping\TcpController;
use Swoft\Tcp\Server\Annotation\Mapping\TcpMapping;
use Swoft\Tcp\Server\Request;
use Swoft\Tcp\Server\Response;

/**
 * Class DemoController
 *
 * @TcpController()
 */
class DemoController
{
    /**
     * @TcpMapping("list", root=true)
     * @param Response $response
     */
    public function list(Response $response): void
    {
        $response->setData('[list]allow command: list, echo, demo.echo');
    }

    /**
     * @TcpMapping("echo")
     * @param Request  $request
     * @param Response $response
     */
    public function index(Request $request, Response $response): void
    {
        $str = $request->getPackage()->getDataString();

        $response->setData('[demo.echo]hi, we received your message: ' . $str);
    }

    /**
     * @TcpMapping("strrev", root=true)
     * @param Request  $request
     * @param Response $response
     */
    public function strRev(Request $request, Response $response): void
    {
        $str = $request->getPackage()->getDataString();

        $response->setData(\strrev($str));
    }

    /**
     * @TcpMapping("echo", root=true)
     * @param Request  $request
     * @param Response $response
     */
    public function echo(Request $request, Response $response): void
    {
        $str = $request->getPackage()->getDataString();

        $response->setData('[echo]hi, we received your message: ' . $str);
    }
} 

Ok, the server code has been written. Here we use the default configuration EOF subcontracting method, the data protocol format also uses the default SimpleTokenPacker::TYPE .

Restart our tcp server php bin/swoft tcp:start , the next one describes how to communicate with our tcp server.

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