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 | * The list of users by socket and their attached id. |
||
34 | * |
||
35 | * @var array |
||
36 | */ |
||
37 | protected $userSockets = []; |
||
38 | |||
39 | /** |
||
40 | * Wether the current instance accepts new connections. |
||
41 | * |
||
42 | * @var bool |
||
43 | */ |
||
44 | protected $acceptsNewConnections = true; |
||
45 | |||
46 | /** |
||
47 | * Create a new channel manager instance. |
||
48 | * |
||
49 | * @param LoopInterface $loop |
||
50 | * @param string|null $factoryClass |
||
51 | * @return void |
||
|
|||
52 | */ |
||
53 | public function __construct(LoopInterface $loop, $factoryClass = null) |
||
57 | |||
58 | /** |
||
59 | * Find the channel by app & name. |
||
60 | * |
||
61 | * @param string|int $appId |
||
62 | * @param string $channel |
||
63 | * @return null|BeyondCode\LaravelWebSockets\Channels\Channel |
||
64 | */ |
||
65 | public function find($appId, string $channel) |
||
69 | |||
70 | /** |
||
71 | * Find a channel by app & name or create one. |
||
72 | * |
||
73 | * @param string|int $appId |
||
74 | * @param string $channel |
||
75 | * @return BeyondCode\LaravelWebSockets\Channels\Channel |
||
76 | */ |
||
77 | public function findOrCreate($appId, string $channel) |
||
87 | |||
88 | /** |
||
89 | * Get the local connections, regardless of the channel |
||
90 | * they are connected to. |
||
91 | * |
||
92 | * @return \React\Promise\PromiseInterface |
||
93 | */ |
||
94 | public function getLocalConnections(): PromiseInterface |
||
109 | |||
110 | /** |
||
111 | * Get all channels for a specific app |
||
112 | * for the current instance. |
||
113 | * |
||
114 | * @param string|int $appId |
||
115 | * @return \React\Promise\PromiseInterface[array] |
||
116 | */ |
||
117 | public function getLocalChannels($appId): PromiseInterface |
||
123 | |||
124 | /** |
||
125 | * Get all channels for a specific app |
||
126 | * across multiple servers. |
||
127 | * |
||
128 | * @param string|int $appId |
||
129 | * @return \React\Promise\PromiseInterface[array] |
||
130 | */ |
||
131 | public function getGlobalChannels($appId): PromiseInterface |
||
135 | |||
136 | /** |
||
137 | * Remove connection from all channels. |
||
138 | * |
||
139 | * @param \Ratchet\ConnectionInterface $connection |
||
140 | * @return void |
||
141 | */ |
||
142 | public function unsubscribeFromAllChannels(ConnectionInterface $connection) |
||
166 | |||
167 | /** |
||
168 | * Subscribe the connection to a specific channel. |
||
169 | * |
||
170 | * @param \Ratchet\ConnectionInterface $connection |
||
171 | * @param string $channelName |
||
172 | * @param stdClass $payload |
||
173 | * @return void |
||
174 | */ |
||
175 | public function subscribeToChannel(ConnectionInterface $connection, string $channelName, stdClass $payload) |
||
181 | |||
182 | /** |
||
183 | * Unsubscribe the connection from the channel. |
||
184 | * |
||
185 | * @param \Ratchet\ConnectionInterface $connection |
||
186 | * @param string $channelName |
||
187 | * @param stdClass $payload |
||
188 | * @return void |
||
189 | */ |
||
190 | public function unsubscribeFromChannel(ConnectionInterface $connection, string $channelName, stdClass $payload) |
||
196 | |||
197 | /** |
||
198 | * Subscribe the connection to a specific channel. |
||
199 | * |
||
200 | * @param string|int $appId |
||
201 | * @return void |
||
202 | */ |
||
203 | public function subscribeToApp($appId) |
||
207 | |||
208 | /** |
||
209 | * Unsubscribe the connection from the channel. |
||
210 | * |
||
211 | * @param string|int $appId |
||
212 | * @return void |
||
213 | */ |
||
214 | public function unsubscribeFromApp($appId) |
||
218 | |||
219 | /** |
||
220 | * Get the connections count on the app |
||
221 | * for the current server instance. |
||
222 | * |
||
223 | * @param string|int $appId |
||
224 | * @param string|null $channelName |
||
225 | * @return \React\Promise\PromiseInterface |
||
226 | */ |
||
227 | public function getLocalConnectionsCount($appId, string $channelName = null): PromiseInterface |
||
244 | |||
245 | /** |
||
246 | * Get the connections count |
||
247 | * across multiple servers. |
||
248 | * |
||
249 | * @param string|int $appId |
||
250 | * @param string|null $channelName |
||
251 | * @return \React\Promise\PromiseInterface |
||
252 | */ |
||
253 | public function getGlobalConnectionsCount($appId, string $channelName = null): PromiseInterface |
||
257 | |||
258 | /** |
||
259 | * Broadcast the message across multiple servers. |
||
260 | * |
||
261 | * @param string|int $appId |
||
262 | * @param string|null $socketId |
||
263 | * @param string $channel |
||
264 | * @param stdClass $payload |
||
265 | * @param string|null $serverId |
||
266 | * @return bool |
||
267 | */ |
||
268 | public function broadcastAcrossServers($appId, ?string $socketId, string $channel, stdClass $payload, string $serverId = null) |
||
272 | |||
273 | /** |
||
274 | * Handle the user when it joined a presence channel. |
||
275 | * |
||
276 | * @param \Ratchet\ConnectionInterface $connection |
||
277 | * @param stdClass $user |
||
278 | * @param string $channel |
||
279 | * @param stdClass $payload |
||
280 | * @return void |
||
281 | */ |
||
282 | public function userJoinedPresenceChannel(ConnectionInterface $connection, stdClass $user, string $channel, stdClass $payload) |
||
287 | |||
288 | /** |
||
289 | * Handle the user when it left a presence channel. |
||
290 | * |
||
291 | * @param \Ratchet\ConnectionInterface $connection |
||
292 | * @param stdClass $user |
||
293 | * @param string $channel |
||
294 | * @param stdClass $payload |
||
295 | * @return void |
||
296 | */ |
||
297 | public function userLeftPresenceChannel(ConnectionInterface $connection, stdClass $user, string $channel) |
||
314 | |||
315 | /** |
||
316 | * Get the presence channel members. |
||
317 | * |
||
318 | * @param string|int $appId |
||
319 | * @param string $channel |
||
320 | * @return \React\Promise\PromiseInterface |
||
321 | */ |
||
322 | public function getChannelMembers($appId, string $channel): PromiseInterface |
||
332 | |||
333 | /** |
||
334 | * Get a member from a presence channel based on connection. |
||
335 | * |
||
336 | * @param \Ratchet\ConnectionInterface $connection |
||
337 | * @param string $channel |
||
338 | * @return \React\Promise\PromiseInterface |
||
339 | */ |
||
340 | public function getChannelMember(ConnectionInterface $connection, string $channel): PromiseInterface |
||
346 | |||
347 | /** |
||
348 | * Get the presence channels total members count. |
||
349 | * |
||
350 | * @param string|int $appId |
||
351 | * @param array $channelNames |
||
352 | * @return \React\Promise\PromiseInterface |
||
353 | */ |
||
354 | public function getChannelsMembersCount($appId, array $channelNames): PromiseInterface |
||
367 | |||
368 | /** |
||
369 | * Get the socket IDs for a presence channel member. |
||
370 | * |
||
371 | * @param string|int $userId |
||
372 | * @param string|int $appId |
||
373 | * @param string $channelName |
||
374 | * @return \React\Promise\PromiseInterface |
||
375 | */ |
||
376 | public function getMemberSockets($userId, $appId, $channelName): PromiseInterface |
||
382 | |||
383 | /** |
||
384 | * Keep tracking the connections availability when they pong. |
||
385 | * |
||
386 | * @param \Ratchet\ConnectionInterface $connection |
||
387 | * @return bool |
||
388 | */ |
||
389 | public function connectionPonged(ConnectionInterface $connection): bool |
||
393 | |||
394 | /** |
||
395 | * Remove the obsolete connections that didn't ponged in a while. |
||
396 | * |
||
397 | * @return bool |
||
398 | */ |
||
399 | public function removeObsoleteConnections(): bool |
||
403 | |||
404 | /** |
||
405 | * Mark the current instance as unable to accept new connections. |
||
406 | * |
||
407 | * @return $this |
||
408 | */ |
||
409 | public function declineNewConnections() |
||
415 | |||
416 | /** |
||
417 | * Check if the current server instance |
||
418 | * accepts new connections. |
||
419 | * |
||
420 | * @return bool |
||
421 | */ |
||
422 | public function acceptsNewConnections(): bool |
||
426 | |||
427 | /** |
||
428 | * Get the channel class by the channel name. |
||
429 | * |
||
430 | * @param string $channelName |
||
431 | * @return string |
||
432 | */ |
||
433 | protected function getChannelClassName(string $channelName): string |
||
445 | } |
||
446 |
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.