1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Pusher.php |
4
|
|
|
* |
5
|
|
|
* @copyright More in license.md |
6
|
|
|
* @license http://www.ipublikuj.eu |
7
|
|
|
* @author Adam Kadlec http://www.ipublikuj.eu |
8
|
|
|
* @package iPublikuj:WebSocketsZMQ! |
9
|
|
|
* @subpackage Pusher |
10
|
|
|
* @since 1.0.0 |
11
|
|
|
* |
12
|
|
|
* @date 01.03.17 |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
declare(strict_types = 1); |
16
|
|
|
|
17
|
|
|
namespace IPub\WebSocketsZMQ\Pusher; |
18
|
|
|
|
19
|
|
|
use ZMQ; |
20
|
|
|
use ZMQContext; |
21
|
|
|
use ZMQSocket; |
22
|
|
|
use ZMQSocketException; |
23
|
|
|
use RuntimeException; |
24
|
|
|
|
25
|
|
|
use Psr\Log; |
26
|
|
|
|
27
|
|
|
use IPub; |
28
|
|
|
use IPub\WebSocketsZMQ; |
29
|
|
|
|
30
|
|
|
use IPub\WebSockets\Router; |
31
|
|
|
|
32
|
|
|
use IPub\WebSocketsWAMP\PushMessages; |
33
|
|
|
use IPub\WebSocketsWAMP\Serializers; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* ZeroMQ message pusher |
37
|
1 |
|
* |
38
|
|
|
* @package iPublikuj:WebSocketsZMQ! |
39
|
|
|
* @subpackage Pushers |
40
|
|
|
* |
41
|
|
|
* @author Adam Kadlec <[email protected]> |
42
|
|
|
*/ |
43
|
|
|
final class Pusher extends PushMessages\Pusher |
44
|
|
|
{ |
45
|
|
|
/** |
46
|
|
|
* @var WebSocketsZMQ\Configuration |
47
|
|
|
*/ |
48
|
|
|
private $configuration; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var Log\LoggerInterface |
52
|
|
|
*/ |
53
|
|
|
private $logger; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var ZMQSocket |
57
|
|
|
*/ |
58
|
|
|
private $socket; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param WebSocketsZMQ\Configuration $configuration |
62
|
|
|
* @param Router\LinkGenerator $linkGenerator |
63
|
|
|
* @param Serializers\PushMessageSerializer $serializer |
64
|
|
|
* @param Log\LoggerInterface|NULL $logger |
65
|
|
|
*/ |
66
|
1 |
View Code Duplication |
public function __construct( |
|
|
|
|
67
|
|
|
WebSocketsZMQ\Configuration $configuration, |
68
|
1 |
|
Router\LinkGenerator $linkGenerator, |
69
|
1 |
|
Serializers\PushMessageSerializer $serializer, |
70
|
1 |
|
Log\LoggerInterface $logger = NULL |
71
|
|
|
) { |
72
|
|
|
parent::__construct('zmq', $serializer, $linkGenerator); |
73
|
|
|
|
74
|
|
|
$this->configuration = $configuration; |
75
|
|
|
$this->logger = $logger === NULL ? new Log\NullLogger : $logger; |
|
|
|
|
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* {@inheritdoc} |
80
|
|
|
* |
81
|
|
|
* @throws ZMQSocketException |
82
|
|
|
*/ |
83
|
|
|
public function close() : void |
84
|
|
|
{ |
85
|
|
|
if ($this->isConnected() === FALSE) { |
86
|
|
|
return; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$this->socket->disconnect($this->configuration->getHost() . ':' . $this->configuration->getPort()); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* {@inheritdoc} |
94
|
|
|
* |
95
|
|
|
* @throws ZMQSocketException |
96
|
|
|
*/ |
97
|
|
|
protected function doPush(string $data, array $context = []) : void |
98
|
|
|
{ |
99
|
|
|
if ($this->isConnected() === FALSE) { |
100
|
|
|
if (!extension_loaded('zmq')) { |
101
|
|
|
throw new RuntimeException(sprintf( |
102
|
|
|
'%s pusher require %s php extension', |
103
|
|
|
get_class($this), |
104
|
|
|
$this->getName() |
|
|
|
|
105
|
|
|
)); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$context = new ZMQContext(1, $this->configuration->isPersistent()); |
109
|
1 |
|
|
110
|
|
|
$this->socket = new ZMQSocket($context, ZMQ::SOCKET_PUSH); |
111
|
|
|
$this->socket->connect($this->configuration->getProtocol() . '://' . $this->configuration->getHost() . ':' . $this->configuration->getPort()); |
112
|
|
|
|
113
|
|
|
$this->setConnected(); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
$this->socket->send($data); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.