|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Jalle19\StatusManager; |
|
4
|
|
|
|
|
5
|
|
|
use Jalle19\StatusManager\Event\InstanceStatusUpdatesEvent; |
|
6
|
|
|
use Psr\Log\LoggerInterface; |
|
7
|
|
|
use Ratchet\ConnectionInterface; |
|
8
|
|
|
use Ratchet\Http\HttpServer; |
|
9
|
|
|
use Ratchet\MessageComponentInterface; |
|
10
|
|
|
use Ratchet\Server\IoServer; |
|
11
|
|
|
use Ratchet\WebSocket\WsServer; |
|
12
|
|
|
use React\EventLoop\LoopInterface; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Handles events related to the WebSocket. Events are either triggered from Ratchet (onOpen etc.) |
|
16
|
|
|
* or from the event dispatcher in StatusManager. |
|
17
|
|
|
* |
|
18
|
|
|
* @package Jalle19\StatusManager |
|
19
|
|
|
* @copyright Copyright © Sam Stenvall 2016- |
|
20
|
|
|
* @license https://www.gnu.org/licenses/gpl.html The GNU General Public License v2.0 |
|
21
|
|
|
*/ |
|
22
|
|
|
class WebSocketManager implements MessageComponentInterface |
|
23
|
|
|
{ |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var LoggerInterface the logger |
|
27
|
|
|
*/ |
|
28
|
|
|
private $_logger; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var Configuration |
|
32
|
|
|
*/ |
|
33
|
|
|
private $_configuration; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var IoServer the Websocket server |
|
37
|
|
|
*/ |
|
38
|
|
|
private $_websocket; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var \SplObjectStorage the connected clients |
|
42
|
|
|
*/ |
|
43
|
|
|
private $_connectedClients; |
|
44
|
|
|
|
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* WebSocketEventHandler constructor. |
|
48
|
|
|
* |
|
49
|
|
|
* @param LoggerInterface $logger |
|
50
|
|
|
* @param Configuration $configuration |
|
51
|
|
|
* @param LoopInterface $loop |
|
52
|
|
|
*/ |
|
53
|
|
|
public function __construct(LoggerInterface $logger, Configuration $configuration, LoopInterface $loop) |
|
54
|
|
|
{ |
|
55
|
|
|
$this->_logger = $logger; |
|
56
|
|
|
$this->_configuration = $configuration; |
|
57
|
|
|
$this->_connectedClients = new \SplObjectStorage(); |
|
58
|
|
|
|
|
59
|
|
|
// Create the WebSocket server |
|
60
|
|
|
$this->_websocket = IoServer::factory( |
|
61
|
|
|
new HttpServer(new WsServer($this)), |
|
62
|
|
|
$this->_configuration->getListenPort(), |
|
63
|
|
|
$this->_configuration->getListenAddress() |
|
64
|
|
|
); |
|
65
|
|
|
|
|
66
|
|
|
$this->_websocket->loop = $loop; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Called right before the main loop is started |
|
72
|
|
|
*/ |
|
73
|
|
|
public function onMainLoopStarted() |
|
74
|
|
|
{ |
|
75
|
|
|
$this->_logger->info('Starting the Websocket server on {address}:{port}', [ |
|
76
|
|
|
'address' => $this->_configuration->getListenAddress(), |
|
77
|
|
|
'port' => $this->_configuration->getListenPort(), |
|
78
|
|
|
]); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @param InstanceStatusUpdatesEvent $event |
|
84
|
|
|
*/ |
|
85
|
|
|
public function onInstanceStatusUpdates(InstanceStatusUpdatesEvent $event) |
|
86
|
|
|
{ |
|
87
|
|
|
$this->_logger->debug('Broadcasting statuses to all clients'); |
|
88
|
|
|
$messages = $event->getInstanceStatusCollection(); |
|
89
|
|
|
|
|
90
|
|
|
foreach ($this->_connectedClients as $client) |
|
91
|
|
|
{ |
|
92
|
|
|
/* @var ConnectionInterface $client */ |
|
93
|
|
|
$client->send(json_encode($messages)); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @inheritdoc |
|
100
|
|
|
*/ |
|
101
|
|
|
public function onOpen(ConnectionInterface $conn) |
|
102
|
|
|
{ |
|
103
|
|
|
$this->_logger->info('Got client connection'); |
|
104
|
|
|
$this->_connectedClients->attach($conn); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @inheritdoc |
|
110
|
|
|
*/ |
|
111
|
|
|
public function onClose(ConnectionInterface $conn) |
|
112
|
|
|
{ |
|
113
|
|
|
$this->_logger->info('Got client disconnect'); |
|
114
|
|
|
$this->_connectedClients->detach($conn); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @inheritdoc |
|
120
|
|
|
*/ |
|
121
|
|
|
public function onError(ConnectionInterface $conn, \Exception $e) |
|
122
|
|
|
{ |
|
123
|
|
|
// TODO: Implement onError() method. |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @inheritdoc |
|
129
|
|
|
*/ |
|
130
|
|
|
public function onMessage(ConnectionInterface $from, $msg) |
|
131
|
|
|
{ |
|
132
|
|
|
// TODO: Implement onMessage() method. |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
} |
|
136
|
|
|
|