swoole 案例代码4【client】

1、async.php

<?php
$client = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC); //异步非阻塞
//$client->set(array(
//    'socket_buffer_size' => 1024 * 1024 * 2,
//    'open_eof_check' => true,
//    'package_eof' => "\r\n\r\n",
//));

$client->_count = 0;
$client->on("connect", function(swoole_client $cli) {
    //swoole_timer_clear($cli->timer);
    $cli->send("GET / HTTP/1.1\r\n\r\n");
    //$cli->sendfile(__DIR__.'/test.txt');
    //$cli->_count = 0;
});

$client->on("receive", function(swoole_client $cli, $data){
    echo "Receive: $data";
    $cli->_count++;
    if ($cli->_count > 5)
    {
        //睡眠模式,不再接收新的数据
        echo "count=10, sleep(5000ms)\n";
        $cli->sleep();
        $cli->_count = 0;
        swoole_timer_after(5000, function() use ($cli) {
            //唤醒
            $cli->wakeup();
        });
        //$cli->close();
        return;
    }
    else
    {
        $cli->send(str_repeat('A', 100)."\n");
    }
});

$client->on("error", function(swoole_client $cli){
    echo "error\n";
});

$client->on("close", function(swoole_client $cli){
    echo "Connection close\n";
});

$client->connect('127.0.0.1', 9501);
//$client->timer = swoole_timer_after(1000, function () use ($client) {
//    echo "socket timeout\n";
//    $client->close();
//});

//echo "connect to 127.0.0.1:9501\n";

2、get_socket.php

<?php

function getClient()
{
    $client = new swoole_client(SWOOLE_SOCK_TCP);
    if (!$client->connect('127.0.0.1', 9501, -1))
    {
        exit("connect failed. Error: {$client->errCode}\n");
    }

    $res = $client->getSocket();
    return $client;
}

$client = getClient();

$count = 0;
//$client->set(array('open_eof_check' => true, 'package_eof' => "\r\n\r\n"));

//$client = new swoole_client(SWOOLE_SOCK_UNIX_DGRAM, SWOOLE_SOCK_SYNC); //同步阻塞
//if (!$client->connect(dirname(__DIR__).'/server/svr.sock', 0, -1, 1))


var_dump($client->getsockname());
$client->send("hello world\r\n\r\n");

//for($i=0; $i < 3; $i ++)
{
    echo $client->recv();
    sleep(1);
}

$client->close();

3、long_tcp.php

<?php
for($i=0; $i < 100; $i++)
{
	$client = new swoole_client(SWOOLE_TCP | SWOOLE_KEEP);
	if(!$client->connect('127.0.0.1', 9501))
	{
		exit("connect failed\n");
	}
	$client->send(str_repeat("A", 600));
	$data = $client->recv(7000, 0);
	if($data === false)
	{
		echo "recv fail\n";
		break;
	}
	var_dump($data);
	$client->close();
}

4、select.php

<?php
$clients = array();

for($i=0; $i< 20; $i++)
{
    $client = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_SYNC); //同步阻塞
    $ret = $client->connect('127.0.0.1', 9501, 0.5, 0);
    if(!$ret)
    {
        echo "Connect Server fail.errCode=".$client->errCode;
    }
    else
    {
    	$client->send("HELLO WORLD\n");
    	$clients[$client->sock] = $client;
    }
}

while (!empty($clients))
{
    $write = $error = array();
    $read = array_values($clients);
    $n = swoole_client_select($read, $write, $error, 0.6);
    if ($n > 0)
    {
        foreach ($read as $index => $c)
        {
            echo "Recv #{$c->sock}: " . $c->recv() . "\n";
            unset($clients[$c->sock]);
        }
    }
}

5、sync.php

<?php
$client = new swoole_client(SWOOLE_SOCK_TCP);
$count = 0;
//$client->set(array('open_eof_check' => true, 'package_eof' => "\r\n\r\n"));

//$client = new swoole_client(SWOOLE_SOCK_UNIX_DGRAM, SWOOLE_SOCK_SYNC); //同步阻塞
//if (!$client->connect(dirname(__DIR__).'/server/svr.sock', 0, -1, 1))

do_connect:
if (!$client->connect('127.0.0.1', 9501, -1))
{
    exit("connect failed. Error: {$client->errCode}\n");
}

var_dump($client->getsockname());
$client->send("hello world\r\n\r\n");

//for($i=0; $i < 3; $i ++)
{
    echo $client->recv();
    sleep(1);
}

$client->close();
$count++;
if ($count < 20)
{
    goto do_connect;
}

6、udp_async.php

<?php
$client = new swoole_client(SWOOLE_SOCK_UDP, SWOOLE_SOCK_ASYNC); //异步非阻塞

$client->on("connect", function(swoole_client $cli) {
    echo "connected\n";
    $cli->send("hello world\n");
});

$client->on('close', function($cli){
    echo "closed\n";
});

$client->on('error', function($cli){
    echo "error\n";
});

$client->on("receive", function(swoole_client $cli, $data){
	echo "received: $data\n";
	sleep(1);
	$cli->send("hello_".rand(1000,9999));
});

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

7、udp_sync.php

<?php
$client = new swoole_client(SWOOLE_SOCK_UDP, SWOOLE_SOCK_SYNC);
$client->connect('127.0.0.1', 9502);

for ($i = 0; $i < 100; $i++)
{
    $client->send("admin");
    echo $client->recv()."\n";
    sleep(1);
}



文 / Admin
评论
金玉良言
loading