SwooleClient.php
1.4 KB
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
/**
* Created by PhpStorm.
* User: luoys
* Date: 2019/2/13
* Time: 11:42
*/
class SwooleClient
{
private $_client;
public function __construct($type = SWOOLE_SOCK_TCP, $is_sync = SWOOLE_SOCK_SYNC)
{
$this->_client = new swoole_client($type, $is_sync);
}
public function connect($host, $port = null, $timeout = null, $sock_flag = null)
{
if (!$this->_client->connect($host, $port, $timeout, $sock_flag)) {
exit("connect failed. Error: {$this->_client->errCode}\n");
}
}
public function send($data)
{
if ($this->_client->send($data) === false)
{
exit("send data failed. Error: {$this->_client->errCode} \n");
}
}
public function sendfile($filename, $offset, $length)
{
if ($this->_client->sendfile($filename, $offset, $length) === false) {
exit("{$filename} is not exist. \n");
}
}
public function sendto($ip, $port, $data)
{
$this->_client->sendto($ip, $port, $data);
}
public function recv($size = 65535, $flags = 0)
{
if (($recv = $this->_client->recv($size, $flags)) === false) {
exit("receive data failed. Error: {$this->_client->errCode} \n");
}
return $recv;
}
}
$client = new SwooleClient();
$client->connect('127.0.0.1', 9501);
$client->send('11223');
$recv = $client->recv();
echo $recv;