-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnect.php
More file actions
51 lines (42 loc) · 1.13 KB
/
Connect.php
File metadata and controls
51 lines (42 loc) · 1.13 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
<?php
/**
* 连接 Message Center 中间层
*/
namespace App\Services;
use ZMQ;
use ZMQContext;
use ZMQSocket;
class Connect
{
const TOKEN_SERVER = 'tcp://127.0.0.1:12000';
const PUSH_SERVER = 'tcp://127.0.0.1:12001';
const ONLINE_SERVER = 'tcp://127.0.0.1:12002';
private $socket;
private $dns;
public function __construct($dns)
{
$this->dns = $dns;
$context = new ZMQContext();
$this->socket = new ZMQSocket($context, ZMQ::SOCKET_DEALER);
$this->socket->setSockOpt(ZMQ::SOCKOPT_LINGER, 2000);
$this->socket->connect($dns);
}
public function __call($name, $arguments)
{
$this->socket->send(json_encode([
'method'=>$name,
'params'=>$arguments
]));
//return $arguments;
$response = json_decode($this->socket->recv(), true);
if($response['ok']){
return isset($response['data']) ? $response['data'] : true;
}else{
throw new \Exception($response['error']);
}
}
public function __destruct()
{
$this->socket->disconnect($this->dns);
}
}