-
Notifications
You must be signed in to change notification settings - Fork 0
/
do.php
71 lines (61 loc) · 1.27 KB
/
do.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
61
62
63
64
65
66
67
68
69
70
71
<?php
date_default_timezone_set('Asia/Chongqing');
class sClient{
var $fp;
var $stop = 0;
function __construct($host, $port){
$fp = fsockopen($host, $port, $errno, $errstr, 30);
if (!$fp){
throw new Exception($errstr);
}
$this->fp = $fp;
}
function isWork(){
return $this->stop == 0;
}
function get(){
$str = fread($this->fp, 10240);
if ($str === false){
$this->stop = 1;
}
$data = json_decode($str);
if ($data){
$tmp = (array) $data;
if (isset($tmp['data'][1])){
$tmp['data'][1] = (array) $tmp['data'][1];
}
$data = $tmp;
if (isset($data['data'][1]['Content-Type']) && $data['data'][1]['Content-Type'] == 'text/x-msmsgscontrol'){
$data['type'] = 'control';
}else{
if (isset($data['data'][2])){
$data['msg'] = trim($data['data'][2][0]);
}
}
}else{
$data = array('type'=>'error', 'data'=>'json decode error:' . $str);
}
return $data;
}
function send($msg){
$ret = fwrite($this->fp, $msg);
if ($ret === false){
$this->stop = 1;
}
}
}
$n = new sClient('127.0.0.1', 9876);
while($n->isWork()){
$msg = $n->get();
if ($msg['type'] == 'msg'){
if ($msg['msg'] == ''){
print_r($msg);
}else{
var_dump($msg['msg']);
$n->send('abc' . $msg['msg']);
}
}else{
//don some thing
}
}
?>