ImapClientSwoole.php
1.6 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php
/**
* 连接代理地址
* @author:dc
* @time 2025/3/31 11:27
* Class ImapClientSwoole
*/
class ImapClientSwoole{
protected $host = '';
/**
* 连接
* @var \Swoole\Coroutine\Client
*/
protected $client;
public function __construct(string $host)
{
$this->host = $host;
}
/**
* 打开连接
* @param string $out_ip
* @param int $timeout
* @return string
* @author:dc
* @time 2025/3/31 10:27
*/
public function open(string $out_ip, int $timeout=3){
$client = new \Swoole\Coroutine\Client(SWOOLE_SOCK_TCP | SWOOLE_SSL);
$client->set([
'timeout'=> $timeout,
'ssl_verify_peer' => false,// 关闭证书验证
'bind_address' => $out_ip,
'bind_port' => 36002,
]);
$host = parse_url($this->host);
if(!$client->connect($host['host'],$host['port'],$timeout)){
throw new Exception($this->host." connection fail. ".$client->errMsg);
}
$this->client = $client;
}
/**
* 写
* @param string $cmd
* @return false|int
* @author:dc
* @time 2024/9/13 15:47
*/
public function write(string $cmd){
return $this->client->send($cmd);
}
/**
* 读
* @return false|string
* @author:dc
* @time 2024/9/13 15:49
*/
public function readLine(){
return $this->client->recv();
}
public function __destruct()
{
if(!empty($this->client)) $this->client->close();
unset($this->client);
// TODO: Implement __destruct() method.
}
}