-
Notifications
You must be signed in to change notification settings - Fork 5
/
client.php
60 lines (53 loc) · 1.66 KB
/
client.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
//$clients = array();
//
//for ($i = 0; $i < 10; $i++) {
// $clients[$i] = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_SYNC); //异步非阻塞
// $clients[$i]->connect('127.0.0.1', 9501, 0.5);
// $ok = $clients[$i]->send("hello, ID = " . $i . "!");
// var_dump($ok);
//}
//
//for ($i = 0; $i < 10; $i++) {
// $clients[$i]->close();
//}
//$clients = array();
//for ($i = 0; $i < 2; $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]);
// }
// }
//}
$client = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC); //异步非阻塞
$client->on("connect", function(swoole_client $cli) {
$cli->send("GET / HTTP/1.1\r\n\r\n");
});
$client->on("receive", function(swoole_client $cli, $data) {
echo "Receive: $data";
sleep(1);
$cli->send("GET / HTTP/1.1\r\n\r\n");
});
$client->on("error", function(swoole_client $cli) {
exit("error\n");
});
$client->on("close", function(swoole_client $cli) {
echo "Connection close";
});
$client->connect('127.0.0.1', 9501, 0.5);
echo "connect to 127.0.0.1:9501";