1 | <?php |
||
32 | class WebSocketServer |
||
33 | { |
||
34 | /** |
||
35 | * @var int |
||
36 | */ |
||
37 | private $port; |
||
38 | |||
39 | /** |
||
40 | * @var string |
||
41 | */ |
||
42 | private $host; |
||
43 | |||
44 | /** |
||
45 | * @var ServerHandshake |
||
46 | */ |
||
47 | private $handshake; |
||
48 | |||
49 | /** |
||
50 | * @var MessageHandlerInterface[] |
||
51 | */ |
||
52 | private $messageHandlers; |
||
53 | |||
54 | /** |
||
55 | * @var Connection[] |
||
56 | */ |
||
57 | private $connections; |
||
58 | |||
59 | /** |
||
60 | * @var LoopInterface |
||
61 | */ |
||
62 | private $loop; |
||
63 | |||
64 | /** |
||
65 | * @var ServerInterface |
||
66 | */ |
||
67 | private $server; |
||
68 | |||
69 | /** |
||
70 | * @var MessageProcessor |
||
71 | */ |
||
72 | private $messageProcessor; |
||
73 | |||
74 | /** |
||
75 | * @var array |
||
76 | */ |
||
77 | private $config; |
||
78 | |||
79 | /** |
||
80 | * @var LoggerInterface |
||
81 | */ |
||
82 | private $logger; |
||
83 | |||
84 | /** |
||
85 | * @param int $port The number of the port to bind |
||
86 | * @param string $host The host to listen on (by default 127.0.0.1) |
||
87 | * @param array $config |
||
88 | */ |
||
89 | 9 | public function __construct($port, $host = '127.0.0.1', $config = []) |
|
102 | |||
103 | /** |
||
104 | * @param MessageHandlerInterface|string $messageHandler An instance of a class as string |
||
105 | * @param string $uri The URI you want to bind on |
||
106 | */ |
||
107 | 5 | public function setMessageHandler($messageHandler, $uri = '*') |
|
124 | |||
125 | /** |
||
126 | * Launch the WebSocket server and an infinite loop that act on event. |
||
127 | * |
||
128 | * @throws \Exception |
||
129 | */ |
||
130 | 5 | public function start() |
|
131 | { |
||
132 | 5 | if ($this->config['prod'] && \extension_loaded('xdebug')) { |
|
133 | throw new \Exception('xdebug is enabled, it\'s a performance issue. Disable that extension or specify "prod" option to false.'); |
||
134 | } |
||
135 | |||
136 | 5 | $this->loop = $this->loop ?? \React\EventLoop\Factory::create(); |
|
137 | 5 | $this->server = $this->server ?? new \React\Socket\TcpServer($this->host . ':' . $this->port, $this->loop); |
|
138 | |||
139 | 5 | if ($this->config['ssl']) { |
|
140 | $this->server = new \React\Socket\SecureServer($this->server, $this->loop, array_merge([ |
||
141 | 'local_cert' => $this->config['certFile'], |
||
142 | 'passphrase' => $this->config['passphrase'], |
||
143 | ], $this->config['sslContextOptions'])); |
||
144 | $this->getLogger()->info('Enabled ssl'); |
||
145 | } |
||
146 | |||
147 | $this->server->on('connection', function (ConnectionInterface $socketStream) { |
||
148 | 5 | $this->onNewConnection($socketStream); |
|
149 | 5 | }); |
|
150 | |||
151 | 5 | $this->getLogger()->info('Listening on ' . $this->host . ':' . $this->port); |
|
152 | |||
153 | 5 | $this->loop->run(); |
|
154 | 5 | } |
|
155 | |||
156 | /** |
||
157 | * @param ConnectionInterface $socketStream |
||
158 | */ |
||
159 | 5 | private function onNewConnection(ConnectionInterface $socketStream) |
|
160 | { |
||
161 | $connection = new Connection($socketStream, function ($uri, Connection $connection) { |
||
162 | 5 | return $this->getMessageHandler($uri, $connection); |
|
163 | 5 | }, $this->loop, $this->messageProcessor); |
|
164 | |||
165 | $socketStream->on('end', function () use($connection) { |
||
166 | 1 | $this->onDisconnect($connection); |
|
167 | 5 | }); |
|
168 | |||
169 | 5 | $connection->setLogger($this->getLogger()); |
|
170 | 5 | $connection->getLogger()->info(sprintf('Ip "%s" establish connection', $connection->getIp())); |
|
171 | 5 | $this->connections[] = $connection; |
|
172 | 5 | } |
|
173 | |||
174 | /** |
||
175 | * |
||
176 | * @param Connection $connection |
||
177 | */ |
||
178 | 1 | private function onDisconnect(Connection $connection) |
|
183 | |||
184 | /** |
||
185 | * Remove a Connection instance by his object id |
||
186 | * @param Connection $connection |
||
187 | * @throws RuntimeException This method throw an exception if the $connection instance object isn't findable in websocket server's connections |
||
188 | */ |
||
189 | 1 | private function removeConnection(Connection $connection) |
|
202 | |||
203 | /** |
||
204 | * @param string $uri |
||
205 | * @param Connection $connection |
||
206 | * @return MessageHandlerInterface|null |
||
207 | */ |
||
208 | 5 | private function getMessageHandler(string $uri, Connection $connection) |
|
231 | |||
232 | /** |
||
233 | * Build the message processor with configuration |
||
234 | */ |
||
235 | 8 | private function buildMessageProcessor() |
|
253 | |||
254 | /** |
||
255 | * Sets the configuration |
||
256 | * |
||
257 | * @param array $config |
||
258 | * @throws ConfigException |
||
259 | */ |
||
260 | 9 | private function setConfig(array $config) |
|
277 | |||
278 | /** |
||
279 | * @return SimpleLogger|LoggerInterface |
||
280 | */ |
||
281 | 5 | public function getLogger() |
|
289 | |||
290 | /** |
||
291 | * Allows you to set a custom logger |
||
292 | * |
||
293 | * @param LoggerInterface $logger |
||
294 | * @return WebSocketServer |
||
295 | */ |
||
296 | 2 | public function setLogger(LoggerInterface $logger) |
|
302 | |||
303 | /** |
||
304 | * Allows to specify a loop that will be used instead of the reactphp generated loop. |
||
305 | * |
||
306 | * @param LoopInterface $loop |
||
307 | * @return WebSocketServer |
||
308 | */ |
||
309 | 5 | public function setLoop(LoopInterface $loop) |
|
315 | |||
316 | /** |
||
317 | * @param ServerInterface $server |
||
318 | * @return WebSocketServer |
||
319 | */ |
||
320 | 5 | public function setSocketServer(ServerInterface $server) |
|
326 | } |
||
327 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.