라이브러리

[PHP] SwooleClient::pipe - 데이터를 다른 파일 설명자로 리디렉션합니다.




SwooleClient::pipe

SwooleClient::pipe는 Swoole의 클라이언트와 서버 간의 통신을 위한 파이프를 제공합니다. 파이프는 클라이언트와 서버 간의 데이터를 전달하는 데 사용됩니다. SwooleClient::pipe는 클라이언트와 서버 간의 비동기 통신을 지원합니다.

파이프의 특징

- 클라이언트와 서버 간의 비동기 통신을 지원합니다.
- 데이터를 전달할 때 파이프를 사용하여 클라이언트와 서버 간의 통신을 분리합니다.
- 클라이언트와 서버 간의 데이터를 전달할 때 파이프를 사용하여 데이터를 전달할 수 있습니다.

파이프를 사용하는 방법

1. 클라이언트와 서버 간의 파이프를 생성합니다.
2. 클라이언트와 서버 간의 데이터를 전달합니다.

예제

클라이언트


#hostingforum.kr
php

use SwooleClient;



class ClientExample {

    public function run() {

        $client = new Client(SWOOLE_SOCK_TCP);

        $client->set([

            'open_length_check' => true,

            'package_length_type' => 'N',

            'package_length_offset' => 0,

            'package_body_offset' => 4,

        ]);



        $client->connect('127.0.0.1', 9501, 0.5);



        $client->push('Hello, Server!');



        $client->on('receive', function ($cli, $data) {

            echo "Received: $data
";

        });



        $client->on('error', function ($cli, $code) {

            echo "Error: $code
";

        });



        $client->on('close', function ($cli) {

            echo "Connection closed
";

        });

    }

}



$client = new ClientExample();

$client->run();



서버


#hostingforum.kr
php

use SwooleClient;



class ServerExample {

    public function run() {

        $server = new SwooleServer('127.0.0.1', 9501);

        $server->set([

            'open_length_check' => true,

            'package_length_type' => 'N',

            'package_length_offset' => 0,

            'package_body_offset' => 4,

        ]);



        $server->on('connect', function ($server, $fd) {

            echo "Client connected: $fd
";

        });



        $server->on('receive', function ($server, $fd, $from_id, $data) {

            echo "Received: $data
";

            $server->push($fd, "Hello, Client!");

        });



        $server->on('close', function ($server, $fd) {

            echo "Client disconnected: $fd
";

        });



        $server->start();

    }

}



$server = new ServerExample();

$server->run();



파이프 사용 예제


#hostingforum.kr
php

use SwooleClient;



class ClientExample {

    public function run() {

        $client = new Client(SWOOLE_SOCK_TCP);

        $client->set([

            'open_length_check' => true,

            'package_length_type' => 'N',

            'package_length_offset' => 0,

            'package_body_offset' => 4,

        ]);



        $client->connect('127.0.0.1', 9501, 0.5);



        $client->push('Hello, Server!', 1); // 파이프 1로 데이터를 전달합니다.



        $client->on('receive', function ($cli, $data, $pipe) {

            echo "Received: $data
";

            if ($pipe == 1) {

                $client->push('Hello, Client!', 2); // 파이프 2로 데이터를 전달합니다.

            }

        });



        $client->on('error', function ($cli, $code) {

            echo "Error: $code
";

        });



        $client->on('close', function ($cli) {

            echo "Connection closed
";

        });

    }

}



$client = new ClientExample();

$client->run();



서버


#hostingforum.kr
php

use SwooleClient;



class ServerExample {

    public function run() {

        $server = new SwooleServer('127.0.0.1', 9501);

        $server->set([

            'open_length_check' => true,

            'package_length_type' => 'N',

            'package_length_offset' => 0,

            'package_body_offset' => 4,

        ]);



        $server->on('connect', function ($server, $fd) {

            echo "Client connected: $fd
";

        });



        $server->on('receive', function ($server, $fd, $from_id, $data, $pipe) {

            echo "Received: $data
";

            if ($pipe == 1) {

                $server->push($fd, "Hello, Client!", 2); // 파이프 2로 데이터를 전달합니다.

            }

        });



        $server->on('close', function ($server, $fd) {

            echo "Client disconnected: $fd
";

        });



        $server->start();

    }

}



$server = new ServerExample();

$server->run();



파이프를 사용하여 클라이언트와 서버 간의 비동기 통신을 지원합니다. 데이터를 전달할 때 파이프를 사용하여 클라이언트와 서버 간의 통신을 분리합니다. 파이프를 사용하여 데이터를 전달할 수 있습니다.
  • profile_image
    나우호스팅 @pcs8404 

    호스팅포럼 화이팅!

    댓글목록

    등록된 댓글이 없습니다.

  • 전체 8,985건 / 106 페이지

검색

게시물 검색