|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Jalle19\StatusManager\Manager; |
|
4
|
|
|
|
|
5
|
|
|
use Jalle19\StatusManager\Configuration\Configuration; |
|
6
|
|
|
use Jalle19\StatusManager\Event\Events; |
|
7
|
|
|
use Psr\Log\LoggerInterface; |
|
8
|
|
|
use Ratchet\Http\HttpServer; |
|
9
|
|
|
use Ratchet\Server\IoServer; |
|
10
|
|
|
use Ratchet\WebSocket\WsServer; |
|
11
|
|
|
use React\EventLoop\LoopInterface; |
|
12
|
|
|
use React\Socket\Server as ServerSocket; |
|
13
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcher; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Class WebSocketManager |
|
17
|
|
|
* @package Jalle19\StatusManager\Manager |
|
18
|
|
|
*/ |
|
19
|
|
|
class WebSocketManager extends AbstractClientManager |
|
20
|
|
|
{ |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var IoServer the Websocket server |
|
24
|
|
|
*/ |
|
25
|
|
|
private $_websocket; |
|
26
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* WebSocketManager constructor. |
|
30
|
|
|
* |
|
31
|
|
|
* @param Configuration $configuration |
|
32
|
|
|
* @param LoggerInterface $logger |
|
33
|
|
|
* @param EventDispatcher $eventDispatcher |
|
34
|
|
|
* @param LoopInterface $loop |
|
35
|
|
|
*/ |
|
36
|
|
|
public function __construct( |
|
37
|
|
|
Configuration $configuration, |
|
38
|
|
|
LoggerInterface $logger, |
|
39
|
|
|
EventDispatcher $eventDispatcher, |
|
40
|
|
|
LoopInterface $loop |
|
41
|
|
|
) { |
|
42
|
|
|
parent::__construct($configuration, $logger, $eventDispatcher); |
|
43
|
|
|
|
|
44
|
|
|
// Create the socket to listen on |
|
45
|
|
|
$socket = new ServerSocket($loop); |
|
46
|
|
|
$socket->listen($configuration->getListenPort(), $configuration->getListenAddress()); |
|
47
|
|
|
|
|
48
|
|
|
// Create the WebSocket server |
|
49
|
|
|
$this->_websocket = new IoServer(new HttpServer(new WsServer($this)), $socket, $loop); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @inheritdoc |
|
55
|
|
|
*/ |
|
56
|
|
|
public static function getSubscribedEvents() |
|
57
|
|
|
{ |
|
58
|
|
|
return array_merge(parent::getSubscribedEvents(), [ |
|
59
|
|
|
Events::MAIN_LOOP_STARTING => 'onMainLoopStarted', |
|
60
|
|
|
]); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Called right before the main loop is started |
|
66
|
|
|
*/ |
|
67
|
|
|
public function onMainLoopStarted() |
|
68
|
|
|
{ |
|
69
|
|
|
$this->logger->notice('Starting the Websocket server on {address}:{port}', [ |
|
70
|
|
|
'address' => $this->configuration->getListenAddress(), |
|
71
|
|
|
'port' => $this->configuration->getListenPort(), |
|
72
|
|
|
]); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
} |
|
76
|
|
|
|