public function udpClient()
    {
        $this->writeln('Starting demo swoole udp client...');

        $client = new TcpClient(SWOOLE_SOCK_UDP, SWOOLE_SOCK_ASYNC);

        $client->on(
            'connect', function ($cli) {
                //发送数据中不能包含'\r\n\r\n'
                $cli->send(JsonHelper::encode(['handler' => SwooleTcpJob::class]) . self::EOF);
            }
        );
        $client->on(
            'receive', function ($cli, $data) {
                $this->writeln('Received: '.$data);
            }
        );
        $client->on(
            'error', function ($cli) {
                $this->writeln('Connect failed');
            }
        );
        $client->on(
            "close", function ($cli) {
                $this->writeln('Connection close');
            }
        );

        $client->connect(
            $this->swooleConfig['udp']['host'] ?? self::DEFAULT_SWOOLE_HOST,
            $this->swooleConfig['udp']['port'] ?? self::DEFAULT_SWOOLE_PORT,
            $this->swooleConfig['udp']['timeout'] ?? self::DEFAULT_SWOOLE_TIMEOUT
        );
    }
public function udpClient()
    {
        $this->writeln('Starting demo swoole udp client...');

        $client = new TcpClient(SWOOLE_SOCK_UDP, SWOOLE_SOCK_ASYNC);

        $client->on(
            'connect', function ($cli) {
                //发送数据中不能包含'\r\n\r\n'
                $cli->send(JsonHelper::encode(['handler' => SwooleTcpJob::class]) . self::EOF);
            }
        );
        $client->on(
            'receive', function ($cli, $data) {
                $this->writeln('Received: '.$data);
            }
        );
        $client->on(
            'error', function ($cli) {
                $this->writeln('Connect failed');
            }
        );
        $client->on(
            "close", function ($cli) {
                $this->writeln('Connection close');
            }
        );

        $client->connect(
            $this->swooleConfig['udp']['host'] ?? self::DEFAULT_SWOOLE_HOST,
            $this->swooleConfig['udp']['port'] ?? self::DEFAULT_SWOOLE_PORT,
            $this->swooleConfig['udp']['timeout'] ?? self::DEFAULT_SWOOLE_TIMEOUT
        );
    }
public function udpClient ( ) { $ this -> writeln ( 'Starting demo swoole udp client...' ) ; $ client = new TcpClient ( SWOOLE_SOCK_UDP , SWOOLE_SOCK_ASYNC ) ; $ client -> on ( 'connect' , function ( $ cli ) { //发送数据中不能包含'\r\n\r\n' $ cli -> send ( JsonHelper :: encode ( [ 'handler' => SwooleTcpJob :: class ] ) . self :: EOF ) ; } ) ; $ client -> on ( 'receive' , function ( $ cli , $ data ) { $ this -> writeln ( 'Received: ' . $ data ) ; } ) ; $ client -> on ( 'error' , function ( $ cli ) { $ this -> writeln ( 'Connect failed' ) ; } ) ; $ client -> on ( "close" , function ( $ cli ) { $ this -> writeln ( 'Connection close' ) ; } ) ; $ client -> connect ( $ this -> swooleConfig [ 'udp' ] [ 'host' ] ?? self :: DEFAULT_SWOOLE_HOST , $ this -> swooleConfig [ 'udp' ] [ 'port' ] ?? self :: DEFAULT_SWOOLE_PORT , $ this -> swooleConfig [ 'udp' ] [ 'timeout' ] ?? self :: DEFAULT_SWOOLE_TIMEOUT ) ; }
Swoole UDP Client Demo
Swoole UDP Client Demo