1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BeyondCode\LaravelWebSockets\ChannelManagers; |
4
|
|
|
|
5
|
|
|
use BeyondCode\LaravelWebSockets\Channels\Channel; |
6
|
|
|
use BeyondCode\LaravelWebSockets\Channels\PresenceChannel; |
7
|
|
|
use BeyondCode\LaravelWebSockets\Channels\PrivateChannel; |
8
|
|
|
use BeyondCode\LaravelWebSockets\Contracts\ChannelManager; |
9
|
|
|
use Illuminate\Support\Str; |
10
|
|
|
use Ratchet\ConnectionInterface; |
11
|
|
|
use React\EventLoop\LoopInterface; |
12
|
|
|
use React\Promise\FulfilledPromise; |
13
|
|
|
use React\Promise\PromiseInterface; |
14
|
|
|
use stdClass; |
15
|
|
|
|
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) |
54
|
|
|
{ |
55
|
|
|
// |
56
|
|
|
} |
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) |
66
|
|
|
{ |
67
|
|
|
return $this->channels[$appId][$channel] ?? null; |
68
|
|
|
} |
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) |
78
|
|
|
{ |
79
|
|
|
if (! $channelInstance = $this->find($appId, $channel)) { |
80
|
|
|
$class = $this->getChannelClassName($channel); |
81
|
|
|
|
82
|
|
|
$this->channels[$appId][$channel] = new $class($channel); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $this->channels[$appId][$channel]; |
86
|
|
|
} |
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 |
95
|
|
|
{ |
96
|
|
|
$connections = collect($this->channels) |
97
|
|
|
->map(function ($channelsWithConnections, $appId) { |
|
|
|
|
98
|
|
|
return collect($channelsWithConnections)->values(); |
99
|
|
|
}) |
100
|
|
|
->values()->collapse() |
101
|
|
|
->map(function ($channel) { |
102
|
|
|
return collect($channel->getConnections()); |
103
|
|
|
}) |
104
|
|
|
->values()->collapse() |
105
|
|
|
->toArray(); |
106
|
|
|
|
107
|
|
|
return new FulfilledPromise($connections); |
|
|
|
|
108
|
|
|
} |
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 |
118
|
|
|
{ |
119
|
|
|
return new FulfilledPromise( |
|
|
|
|
120
|
|
|
$this->channels[$appId] ?? [] |
121
|
|
|
); |
122
|
|
|
} |
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 |
132
|
|
|
{ |
133
|
|
|
return $this->getLocalChannels($appId); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Remove connection from all channels. |
138
|
|
|
* |
139
|
|
|
* @param \Ratchet\ConnectionInterface $connection |
140
|
|
|
* @return void |
141
|
|
|
*/ |
142
|
|
|
public function unsubscribeFromAllChannels(ConnectionInterface $connection) |
143
|
|
|
{ |
144
|
|
|
if (! isset($connection->app)) { |
|
|
|
|
145
|
|
|
return; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
$this->getLocalChannels($connection->app->id) |
|
|
|
|
149
|
|
|
->then(function ($channels) use ($connection) { |
150
|
|
|
collect($channels)->each->unsubscribe($connection); |
151
|
|
|
|
152
|
|
|
collect($channels) |
153
|
|
|
->reject->hasConnections() |
154
|
|
|
->each(function (Channel $channel, string $channelName) use ($connection) { |
155
|
|
|
unset($this->channels[$connection->app->id][$channelName]); |
156
|
|
|
}); |
157
|
|
|
}); |
158
|
|
|
|
159
|
|
|
$this->getLocalChannels($connection->app->id) |
|
|
|
|
160
|
|
|
->then(function ($channels) use ($connection) { |
161
|
|
|
if (count($channels) === 0) { |
162
|
|
|
unset($this->channels[$connection->app->id]); |
163
|
|
|
} |
164
|
|
|
}); |
165
|
|
|
} |
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) |
176
|
|
|
{ |
177
|
|
|
$channel = $this->findOrCreate($connection->app->id, $channelName); |
|
|
|
|
178
|
|
|
|
179
|
|
|
$channel->subscribe($connection, $payload); |
180
|
|
|
} |
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) |
191
|
|
|
{ |
192
|
|
|
$channel = $this->findOrCreate($connection->app->id, $channelName); |
|
|
|
|
193
|
|
|
|
194
|
|
|
$channel->unsubscribe($connection, $payload); |
195
|
|
|
} |
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) |
204
|
|
|
{ |
205
|
|
|
// |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Unsubscribe the connection from the channel. |
210
|
|
|
* |
211
|
|
|
* @param string|int $appId |
212
|
|
|
* @return void |
213
|
|
|
*/ |
214
|
|
|
public function unsubscribeFromApp($appId) |
215
|
|
|
{ |
216
|
|
|
// |
217
|
|
|
} |
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 |
228
|
|
|
{ |
229
|
|
|
return $this->getLocalChannels($appId) |
230
|
|
|
->then(function ($channels) use ($channelName) { |
231
|
|
|
return collect($channels) |
232
|
|
|
->when(! is_null($channelName), function ($collection) use ($channelName) { |
233
|
|
|
return $collection->filter(function (Channel $channel) use ($channelName) { |
234
|
|
|
return $channel->getName() === $channelName; |
235
|
|
|
}); |
236
|
|
|
}) |
237
|
|
|
->flatMap(function (Channel $channel) { |
238
|
|
|
return collect($channel->getConnections())->pluck('socketId'); |
239
|
|
|
}) |
240
|
|
|
->unique() |
241
|
|
|
->count(); |
242
|
|
|
}); |
243
|
|
|
} |
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 |
254
|
|
|
{ |
255
|
|
|
return $this->getLocalConnectionsCount($appId, $channelName); |
256
|
|
|
} |
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) |
269
|
|
|
{ |
270
|
|
|
return true; |
271
|
|
|
} |
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) |
283
|
|
|
{ |
284
|
|
|
$this->users["{$connection->app->id}:{$channel}"][$connection->socketId] = json_encode($user); |
|
|
|
|
285
|
|
|
$this->userSockets["{$connection->app->id}:{$channel}:{$user->user_id}"][] = $connection->socketId; |
|
|
|
|
286
|
|
|
} |
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) |
298
|
|
|
{ |
299
|
|
|
unset($this->users["{$connection->app->id}:{$channel}"][$connection->socketId]); |
300
|
|
|
|
301
|
|
|
$deletableSocketKey = array_search( |
302
|
|
|
$connection->socketId, |
|
|
|
|
303
|
|
|
$this->userSockets["{$connection->app->id}:{$channel}:{$user->user_id}"] |
|
|
|
|
304
|
|
|
); |
305
|
|
|
|
306
|
|
|
if ($deletableSocketKey !== false) { |
307
|
|
|
unset($this->userSockets["{$connection->app->id}:{$channel}:{$user->user_id}"][$deletableSocketKey]); |
308
|
|
|
|
309
|
|
|
if (count($this->userSockets["{$connection->app->id}:{$channel}:{$user->user_id}"]) === 0) { |
|
|
|
|
310
|
|
|
unset($this->userSockets["{$connection->app->id}:{$channel}:{$user->user_id}"]); |
311
|
|
|
} |
312
|
|
|
} |
313
|
|
|
} |
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 |
323
|
|
|
{ |
324
|
|
|
$members = $this->users["{$appId}:{$channel}"] ?? []; |
325
|
|
|
|
326
|
|
|
$members = collect($members)->map(function ($user) { |
327
|
|
|
return json_decode($user); |
328
|
|
|
})->unique('user_id')->toArray(); |
329
|
|
|
|
330
|
|
|
return new FulfilledPromise($members); |
|
|
|
|
331
|
|
|
} |
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 |
341
|
|
|
{ |
342
|
|
|
$member = $this->users["{$connection->app->id}:{$channel}"][$connection->socketId] ?? null; |
|
|
|
|
343
|
|
|
|
344
|
|
|
return new FulfilledPromise($member); |
|
|
|
|
345
|
|
|
} |
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 |
355
|
|
|
{ |
356
|
|
|
$results = collect($channelNames) |
357
|
|
|
->reduce(function ($results, $channel) use ($appId) { |
358
|
|
|
$results[$channel] = isset($this->users["{$appId}:{$channel}"]) |
359
|
|
|
? count($this->users["{$appId}:{$channel}"]) |
360
|
|
|
: 0; |
361
|
|
|
|
362
|
|
|
return $results; |
363
|
|
|
}, []); |
364
|
|
|
|
365
|
|
|
return new FulfilledPromise($results); |
|
|
|
|
366
|
|
|
} |
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 |
377
|
|
|
{ |
378
|
|
|
return new FulfilledPromise( |
|
|
|
|
379
|
|
|
$this->userSockets["{$appId}:{$channelName}:{$userId}"] ?? [] |
380
|
|
|
); |
381
|
|
|
} |
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 |
390
|
|
|
{ |
391
|
|
|
return true; |
392
|
|
|
} |
393
|
|
|
|
394
|
|
|
/** |
395
|
|
|
* Remove the obsolete connections that didn't ponged in a while. |
396
|
|
|
* |
397
|
|
|
* @return bool |
398
|
|
|
*/ |
399
|
|
|
public function removeObsoleteConnections(): bool |
400
|
|
|
{ |
401
|
|
|
return true; |
402
|
|
|
} |
403
|
|
|
|
404
|
|
|
/** |
405
|
|
|
* Mark the current instance as unable to accept new connections. |
406
|
|
|
* |
407
|
|
|
* @return $this |
408
|
|
|
*/ |
409
|
|
|
public function declineNewConnections() |
410
|
|
|
{ |
411
|
|
|
$this->acceptsNewConnections = false; |
412
|
|
|
|
413
|
|
|
return $this; |
414
|
|
|
} |
415
|
|
|
|
416
|
|
|
/** |
417
|
|
|
* Check if the current server instance |
418
|
|
|
* accepts new connections. |
419
|
|
|
* |
420
|
|
|
* @return bool |
421
|
|
|
*/ |
422
|
|
|
public function acceptsNewConnections(): bool |
423
|
|
|
{ |
424
|
|
|
return $this->acceptsNewConnections; |
425
|
|
|
} |
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 |
434
|
|
|
{ |
435
|
|
|
if (Str::startsWith($channelName, 'private-')) { |
436
|
|
|
return PrivateChannel::class; |
437
|
|
|
} |
438
|
|
|
|
439
|
|
|
if (Str::startsWith($channelName, 'presence-')) { |
440
|
|
|
return PresenceChannel::class; |
441
|
|
|
} |
442
|
|
|
|
443
|
|
|
return Channel::class; |
444
|
|
|
} |
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.