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 array |
||
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 | 7 | 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 | 3 | 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 | 3 | public function start() |
|
131 | { |
||
132 | 3 | 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 | 3 | $this->loop = $this->loop ?? \React\EventLoop\Factory::create(); |
|
137 | 3 | $this->server = $this->server ?? new \React\Socket\TcpServer($this->port, $this->loop); |
|
138 | |||
139 | 3 | 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 | 3 | $this->server->on('connection', function ($socketStream) { |
|
148 | 3 | $this->onNewConnection($socketStream); |
|
149 | 3 | }); |
|
150 | |||
151 | 3 | $this->getLogger()->info('Listening on ' . $this->host . ':' . $this->port); |
|
152 | |||
153 | 3 | $this->loop->run(); |
|
154 | 3 | } |
|
155 | |||
156 | /** |
||
157 | * @param ConnectionInterface $socketStream |
||
158 | */ |
||
159 | private function onNewConnection(ConnectionInterface $socketStream) |
||
160 | { |
||
161 | 3 | $connection = new Connection($socketStream, function ($uri, Connection $connection) { |
|
162 | 3 | return $this->getMessageHandler($uri, $connection); |
|
163 | 3 | }, $this->loop, $this->messageProcessor); |
|
164 | |||
165 | 3 | $connection->setLogger($this->getLogger()); |
|
166 | 3 | $this->connections[] = $connection; |
|
167 | 3 | } |
|
168 | |||
169 | /** |
||
170 | * @param string $uri |
||
171 | * @param Connection $connection |
||
172 | * @return MessageHandlerInterface|null |
||
173 | */ |
||
174 | 3 | private function getMessageHandler(string $uri, Connection $connection) |
|
|
|||
175 | { |
||
176 | 3 | $handler = null; |
|
177 | |||
178 | 3 | if (!empty($this->messageHandlers[$uri])) { |
|
179 | 2 | $handler = $this->messageHandlers[$uri]; |
|
180 | } |
||
181 | |||
182 | 3 | if (null === $handler && !empty($this->messageHandlers['*'])) { |
|
183 | $handler = $this->messageHandlers['*']; |
||
184 | } |
||
185 | |||
186 | 3 | if (null !== $handler) { |
|
187 | 2 | if (\is_string($handler)) { |
|
188 | $handler = new $handler; |
||
189 | } |
||
190 | |||
191 | 2 | return $handler; |
|
192 | } |
||
193 | |||
194 | 1 | return null; |
|
195 | } |
||
196 | |||
197 | /** |
||
198 | * Build the message processor with configuration |
||
199 | */ |
||
200 | 6 | private function buildMessageProcessor() |
|
201 | { |
||
202 | 6 | $this->messageProcessor = new MessageProcessor( |
|
203 | 6 | false, |
|
204 | 6 | new FrameFactory($this->config['frame']), |
|
205 | 6 | new MessageFactory($this->config['message']) |
|
206 | ); |
||
207 | 6 | $this->messageProcessor->addHandler(new PingFrameHandler()); |
|
208 | 6 | $this->messageProcessor->addHandler(new CloseFrameHandler()); |
|
209 | 6 | $this->messageProcessor->addHandler(new WrongOpcodeFrameHandler()); |
|
210 | 6 | $this->messageProcessor->addHandler(new RsvCheckFrameHandler()); |
|
211 | |||
212 | 6 | foreach ($this->config['messageHandlers'] as $handler) { |
|
213 | 1 | if (!$handler instanceof MessageHandlerInterface) { |
|
214 | 1 | throw new RuntimeException(sprintf('%s is not an instance of MessageHandlerInterface but must be !', get_class($handler))); |
|
215 | } |
||
216 | } |
||
217 | 5 | } |
|
218 | |||
219 | /** |
||
220 | * Sets the configuration |
||
221 | * |
||
222 | * @param array $config |
||
223 | * @throws ConfigException |
||
224 | */ |
||
225 | 7 | private function setConfig(array $config) |
|
226 | { |
||
227 | 7 | $this->config = \array_merge([ |
|
228 | 7 | 'frame' => [], |
|
229 | 'message' => [], |
||
230 | 'messageHandlers' => [], |
||
231 | 'prod' => true, |
||
232 | 'ssl' => false, |
||
233 | 'certFile' => '', |
||
234 | 'passphrase' => '', |
||
235 | 'sslContextOptions' => [], |
||
236 | 7 | ], $config); |
|
237 | |||
238 | 7 | if ($this->config['ssl'] && !is_file($this->config['certFile'])) { |
|
239 | 1 | throw new ConfigException('With ssl configuration, you need to specify a certificate file.'); |
|
240 | } |
||
241 | 6 | } |
|
242 | |||
243 | /** |
||
244 | * @return SimpleLogger|LoggerInterface |
||
245 | */ |
||
246 | 3 | public function getLogger() |
|
254 | |||
255 | /** |
||
256 | * Allows you to set a custom logger |
||
257 | * |
||
258 | * @param LoggerInterface $logger |
||
259 | * @return WebSocketServer |
||
260 | */ |
||
261 | public function setLogger(LoggerInterface $logger) |
||
267 | |||
268 | /** |
||
269 | * Allows to specify a loop that will be used instead of the reactphp generated loop. |
||
270 | * |
||
271 | * @param LoopInterface $loop |
||
272 | * @return WebSocketServer |
||
273 | */ |
||
274 | 3 | public function setLoop(LoopInterface $loop) |
|
280 | |||
281 | /** |
||
282 | * @param ServerInterface $server |
||
283 | * @return WebSocketServer |
||
284 | */ |
||
285 | 3 | public function setSocketServer(ServerInterface $server) |
|
291 | } |
||
292 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.