forked from kugouming/Swoole-Framework
/
client.php
48 lines (37 loc) · 1.11 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
<?php
Class Client{
protected $client;
Public function __construct($host,$port){
$this->client = new swoole_client(SWOOLE_SOCK_TCP , SWOOLE_SOCK_ASYNC);
//设置事件回调函数
$this->client->on("error", array($this , 'onError'));
$this->client->on("close", array($this , 'onClose'));
$this->client->on("receive", array($this , 'onReceive'));
$this->client->on("connect", array($this , 'onConnect'));
//发起网络连接
$this->client->connect($host, $port);
$this->isConnected() or die;
}
Public function onError($client){
echo "Connect failed". PHP_EOL;
}
Public function onClose($client){
echo "Connection close". PHP_EOL;
}
public function onConnect($client){
echo "Connection open". PHP_EOL;
}
Public function send($data){
$this->client->send($data);
}
Public function onReceive(swoole_client $client, $data){
$data = $this->client->recv();
print_r($data);
}
private function isConnected(){
return $this->client->isConnected();
}
}
$client = new Client('127.0.0.1',9501);
$data = json_encode(array('code'=>'4001','data'=>array('id'=>1)));
$client->send($data);