File tree Expand file tree Collapse file tree 3 files changed +46
-1
lines changed Expand file tree Collapse file tree 3 files changed +46
-1
lines changed Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace BeyondCode \LaravelWebSockets \LaravelEcho \Http \Controllers ;
4
+
5
+ use BeyondCode \LaravelWebSockets \LaravelEcho \Pusher \Channels \ChannelManager ;
6
+ use BeyondCode \LaravelWebSockets \LaravelEcho \Pusher \Channels \PresenceChannel ;
7
+ use BeyondCode \LaravelWebSockets \LaravelEcho \Pusher \Exceptions \InvalidSignatureException ;
8
+ use Illuminate \Http \Request ;
9
+ use Illuminate \Support \Collection ;
10
+ use Symfony \Component \HttpKernel \Exception \HttpException ;
11
+
12
+ class FetchUsers extends EchoController
13
+ {
14
+ /** @var \BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Channels\ChannelManager */
15
+ protected $ channelManager ;
16
+
17
+ public function __construct (ChannelManager $ channelManager )
18
+ {
19
+ $ this ->channelManager = $ channelManager ;
20
+ }
21
+
22
+ public function __invoke (Request $ request )
23
+ {
24
+ $ channel = $ this ->channelManager ->find ($ request ->appId , $ request ->channelName );
25
+
26
+ if (is_null ($ channel )) {
27
+ throw new HttpException (404 , 'Unknown channel " ' .$ request ->channelName .'" ' );
28
+ }
29
+
30
+ if (! $ channel instanceof PresenceChannel) {
31
+ throw new HttpException (400 , 'Invalid presence channel " ' .$ request ->channelName .'" ' );
32
+ }
33
+
34
+ return [
35
+ 'users ' => Collection::make ($ channel ->getUsers ())->map (function ($ user ) {
36
+ return ['id ' => $ user ->user_id ];
37
+ })->values ()
38
+ ];
39
+ }
40
+ }
Original file line number Diff line number Diff line change @@ -8,6 +8,11 @@ class PresenceChannel extends Channel
8
8
{
9
9
protected $ users = [];
10
10
11
+ public function getUsers (): array
12
+ {
13
+ return $ this ->users ;
14
+ }
15
+
11
16
/*
12
17
* @link https://pusher.com/docs/pusher_protocol#presence-channel-events
13
18
*/
Original file line number Diff line number Diff line change @@ -72,7 +72,7 @@ public function echo()
72
72
$ this ->get ('/apps/{appId}/status ' , LaravelEcho \Http \Controllers \StatusController::class);
73
73
$ this ->get ('/apps/{appId}/channels ' , LaravelEcho \Http \Controllers \StatusController::class);
74
74
$ this ->get ('/apps/{appId}/channels/{channelName} ' , LaravelEcho \Http \Controllers \FetchChannel::class);
75
- $ this ->get ('/apps/{appId}/channels/{channelName}/users ' , LaravelEcho \Http \Controllers \StatusController ::class);
75
+ $ this ->get ('/apps/{appId}/channels/{channelName}/users ' , LaravelEcho \Http \Controllers \FetchUsers ::class);
76
76
77
77
$ this ->post ('/apps/{appId}/events ' , LaravelEcho \Http \Controllers \TriggerEvent::class);
78
78
}
You can’t perform that action at this time.
0 commit comments