1 | <?php |
||
16 | class LocalChannelManager implements ChannelManager |
||
17 | { |
||
18 | /** |
||
19 | * The list of stored channels. |
||
20 | * |
||
21 | * @var array |
||
22 | */ |
||
23 | protected $channels = []; |
||
24 | |||
25 | /** |
||
26 | * The list of users that joined the presence channel. |
||
27 | * |
||
28 | * @var array |
||
29 | */ |
||
30 | protected $users = []; |
||
31 | |||
32 | /** |
||
33 | * Wether the current instance accepts new connections. |
||
34 | * |
||
35 | * @var bool |
||
36 | */ |
||
37 | protected $acceptsNewConnections = true; |
||
38 | |||
39 | /** |
||
40 | * Create a new channel manager instance. |
||
41 | * |
||
42 | * @param LoopInterface $loop |
||
43 | * @param string|null $factoryClass |
||
44 | * @return void |
||
|
|||
45 | */ |
||
46 | public function __construct(LoopInterface $loop, $factoryClass = null) |
||
50 | |||
51 | /** |
||
52 | * Find the channel by app & name. |
||
53 | * |
||
54 | * @param string|int $appId |
||
55 | * @param string $channel |
||
56 | * @return null|BeyondCode\LaravelWebSockets\Channels\Channel |
||
57 | */ |
||
58 | public function find($appId, string $channel) |
||
62 | |||
63 | /** |
||
64 | * Find a channel by app & name or create one. |
||
65 | * |
||
66 | * @param string|int $appId |
||
67 | * @param string $channel |
||
68 | * @return BeyondCode\LaravelWebSockets\Channels\Channel |
||
69 | */ |
||
70 | public function findOrCreate($appId, string $channel) |
||
80 | |||
81 | /** |
||
82 | * Get the local connections, regardless of the channel |
||
83 | * they are connected to. |
||
84 | * |
||
85 | * @return \React\Promise\PromiseInterface |
||
86 | */ |
||
87 | public function getLocalConnections(): PromiseInterface |
||
102 | |||
103 | /** |
||
104 | * Get all channels for a specific app |
||
105 | * for the current instance. |
||
106 | * |
||
107 | * @param string|int $appId |
||
108 | * @return \React\Promise\PromiseInterface[array] |
||
109 | */ |
||
110 | public function getLocalChannels($appId): PromiseInterface |
||
116 | |||
117 | /** |
||
118 | * Get all channels for a specific app |
||
119 | * across multiple servers. |
||
120 | * |
||
121 | * @param string|int $appId |
||
122 | * @return \React\Promise\PromiseInterface[array] |
||
123 | */ |
||
124 | public function getGlobalChannels($appId): PromiseInterface |
||
128 | |||
129 | /** |
||
130 | * Remove connection from all channels. |
||
131 | * |
||
132 | * @param \Ratchet\ConnectionInterface $connection |
||
133 | * @return void |
||
134 | */ |
||
135 | public function unsubscribeFromAllChannels(ConnectionInterface $connection) |
||
159 | |||
160 | /** |
||
161 | * Subscribe the connection to a specific channel. |
||
162 | * |
||
163 | * @param \Ratchet\ConnectionInterface $connection |
||
164 | * @param string $channelName |
||
165 | * @param stdClass $payload |
||
166 | * @return void |
||
167 | */ |
||
168 | public function subscribeToChannel(ConnectionInterface $connection, string $channelName, stdClass $payload) |
||
174 | |||
175 | /** |
||
176 | * Unsubscribe the connection from the channel. |
||
177 | * |
||
178 | * @param \Ratchet\ConnectionInterface $connection |
||
179 | * @param string $channelName |
||
180 | * @param stdClass $payload |
||
181 | * @return void |
||
182 | */ |
||
183 | public function unsubscribeFromChannel(ConnectionInterface $connection, string $channelName, stdClass $payload) |
||
189 | |||
190 | /** |
||
191 | * Subscribe the connection to a specific channel. |
||
192 | * |
||
193 | * @param string|int $appId |
||
194 | * @return void |
||
195 | */ |
||
196 | public function subscribeToApp($appId) |
||
200 | |||
201 | /** |
||
202 | * Unsubscribe the connection from the channel. |
||
203 | * |
||
204 | * @param string|int $appId |
||
205 | * @return void |
||
206 | */ |
||
207 | public function unsubscribeFromApp($appId) |
||
211 | |||
212 | /** |
||
213 | * Get the connections count on the app |
||
214 | * for the current server instance. |
||
215 | * |
||
216 | * @param string|int $appId |
||
217 | * @param string|null $channelName |
||
218 | * @return \React\Promise\PromiseInterface |
||
219 | */ |
||
220 | public function getLocalConnectionsCount($appId, string $channelName = null): PromiseInterface |
||
237 | |||
238 | /** |
||
239 | * Get the connections count |
||
240 | * across multiple servers. |
||
241 | * |
||
242 | * @param string|int $appId |
||
243 | * @param string|null $channelName |
||
244 | * @return \React\Promise\PromiseInterface |
||
245 | */ |
||
246 | public function getGlobalConnectionsCount($appId, string $channelName = null): PromiseInterface |
||
250 | |||
251 | /** |
||
252 | * Broadcast the message across multiple servers. |
||
253 | * |
||
254 | * @param string|int $appId |
||
255 | * @param string $channel |
||
256 | * @param stdClass $payload |
||
257 | * @return bool |
||
258 | */ |
||
259 | public function broadcastAcrossServers($appId, string $channel, stdClass $payload) |
||
263 | |||
264 | /** |
||
265 | * Handle the user when it joined a presence channel. |
||
266 | * |
||
267 | * @param \Ratchet\ConnectionInterface $connection |
||
268 | * @param stdClass $user |
||
269 | * @param string $channel |
||
270 | * @param stdClass $payload |
||
271 | * @return void |
||
272 | */ |
||
273 | public function userJoinedPresenceChannel(ConnectionInterface $connection, stdClass $user, string $channel, stdClass $payload) |
||
277 | |||
278 | /** |
||
279 | * Handle the user when it left a presence channel. |
||
280 | * |
||
281 | * @param \Ratchet\ConnectionInterface $connection |
||
282 | * @param stdClass $user |
||
283 | * @param string $channel |
||
284 | * @param stdClass $payload |
||
285 | * @return void |
||
286 | */ |
||
287 | public function userLeftPresenceChannel(ConnectionInterface $connection, stdClass $user, string $channel) |
||
291 | |||
292 | /** |
||
293 | * Get the presence channel members. |
||
294 | * |
||
295 | * @param string|int $appId |
||
296 | * @param string $channel |
||
297 | * @return \React\Promise\PromiseInterface |
||
298 | */ |
||
299 | public function getChannelMembers($appId, string $channel): PromiseInterface |
||
309 | |||
310 | /** |
||
311 | * Get a member from a presence channel based on connection. |
||
312 | * |
||
313 | * @param \Ratchet\ConnectionInterface $connection |
||
314 | * @param string $channel |
||
315 | * @return \React\Promise\PromiseInterface |
||
316 | */ |
||
317 | public function getChannelMember(ConnectionInterface $connection, string $channel): PromiseInterface |
||
323 | |||
324 | /** |
||
325 | * Get the presence channels total members count. |
||
326 | * |
||
327 | * @param string|int $appId |
||
328 | * @param array $channelNames |
||
329 | * @return \React\Promise\PromiseInterface |
||
330 | */ |
||
331 | public function getChannelsMembersCount($appId, array $channelNames): PromiseInterface |
||
344 | |||
345 | /** |
||
346 | * Keep tracking the connections availability when they pong. |
||
347 | * |
||
348 | * @param \Ratchet\ConnectionInterface $connection |
||
349 | * @return bool |
||
350 | */ |
||
351 | public function connectionPonged(ConnectionInterface $connection): bool |
||
355 | |||
356 | /** |
||
357 | * Remove the obsolete connections that didn't ponged in a while. |
||
358 | * |
||
359 | * @return bool |
||
360 | */ |
||
361 | public function removeObsoleteConnections(): bool |
||
365 | |||
366 | /** |
||
367 | * Mark the current instance as unable to accept new connections. |
||
368 | * |
||
369 | * @return $this |
||
370 | */ |
||
371 | public function declineNewConnections() |
||
377 | |||
378 | /** |
||
379 | * Check if the current server instance |
||
380 | * accepts new connections. |
||
381 | * |
||
382 | * @return bool |
||
383 | */ |
||
384 | public function acceptsNewConnections(): bool |
||
388 | |||
389 | /** |
||
390 | * Get the channel class by the channel name. |
||
391 | * |
||
392 | * @param string $channelName |
||
393 | * @return string |
||
394 | */ |
||
395 | protected function getChannelClassName(string $channelName): string |
||
407 | } |
||
408 |
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.