1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BeyondCode\LaravelWebSockets\PubSub\Drivers; |
4
|
|
|
|
5
|
|
|
use stdClass; |
6
|
|
|
use React\EventLoop\LoopInterface; |
7
|
|
|
use React\Promise\FulfilledPromise; |
8
|
|
|
use React\Promise\PromiseInterface; |
9
|
|
|
use BeyondCode\LaravelWebSockets\PubSub\ReplicationInterface; |
10
|
|
|
|
11
|
|
|
class LocalClient implements ReplicationInterface |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Mapping of the presence JSON data for users in each channel. |
15
|
|
|
* |
16
|
|
|
* @var string[][] |
17
|
|
|
*/ |
18
|
|
|
protected $channelData = []; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Boot the pub/sub provider (open connections, initial subscriptions, etc). |
22
|
|
|
* |
23
|
|
|
* @param LoopInterface $loop |
24
|
|
|
* @return self |
25
|
|
|
*/ |
26
|
|
|
public function boot(LoopInterface $loop) : ReplicationInterface |
27
|
|
|
{ |
28
|
|
|
return $this; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Publish a payload on a specific channel, for a specific app. |
33
|
|
|
* |
34
|
|
|
* @param string $appId |
35
|
|
|
* @param string $channel |
36
|
|
|
* @param stdClass $payload |
37
|
|
|
* @return bool |
38
|
|
|
*/ |
39
|
|
|
public function publish(string $appId, string $channel, stdClass $payload) : bool |
40
|
|
|
{ |
41
|
|
|
// Nothing to do, nobody to publish to |
42
|
|
|
return true; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Subscribe to receive messages for a channel. |
47
|
|
|
* |
48
|
|
|
* @param string $appId |
49
|
|
|
* @param string $channel |
50
|
|
|
* @return bool |
51
|
|
|
*/ |
52
|
|
|
public function subscribe(string $appId, string $channel) : bool |
53
|
|
|
{ |
54
|
|
|
return true; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Unsubscribe from a channel. |
59
|
|
|
* |
60
|
|
|
* @param string $appId |
61
|
|
|
* @param string $channel |
62
|
|
|
* @return bool |
63
|
|
|
*/ |
64
|
|
|
public function unsubscribe(string $appId, string $channel) : bool |
65
|
|
|
{ |
66
|
|
|
return true; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Add a member to a channel. To be called when they have |
71
|
|
|
* subscribed to the channel. |
72
|
|
|
* |
73
|
|
|
* @param string $appId |
74
|
|
|
* @param string $channel |
75
|
|
|
* @param string $socketId |
76
|
|
|
* @param string $data |
77
|
|
|
*/ |
78
|
|
|
public function joinChannel(string $appId, string $channel, string $socketId, string $data) |
79
|
|
|
{ |
80
|
|
|
$this->channelData["$appId:$channel"][$socketId] = $data; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Remove a member from the channel. To be called when they have |
85
|
|
|
* unsubscribed from the channel. |
86
|
|
|
* |
87
|
|
|
* @param string $appId |
88
|
|
|
* @param string $channel |
89
|
|
|
* @param string $socketId |
90
|
|
|
*/ |
91
|
|
|
public function leaveChannel(string $appId, string $channel, string $socketId) |
92
|
|
|
{ |
93
|
|
|
unset($this->channelData["$appId:$channel"][$socketId]); |
94
|
|
|
if (empty($this->channelData["$appId:$channel"])) { |
95
|
|
|
unset($this->channelData["$appId:$channel"]); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Retrieve the full information about the members in a presence channel. |
101
|
|
|
* |
102
|
|
|
* @param string $appId |
103
|
|
|
* @param string $channel |
104
|
|
|
* @return PromiseInterface |
105
|
|
|
*/ |
106
|
|
|
public function channelMembers(string $appId, string $channel) : PromiseInterface |
107
|
|
|
{ |
108
|
|
|
$members = $this->channelData["$appId:$channel"] ?? []; |
109
|
|
|
|
110
|
|
|
// The data is expected as objects, so we need to JSON decode |
111
|
|
|
$members = array_map(function ($user) { |
112
|
|
|
return json_decode($user); |
113
|
|
|
}, $members); |
114
|
|
|
|
115
|
|
|
return new FulfilledPromise($members); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Get the amount of users subscribed for each presence channel. |
120
|
|
|
* |
121
|
|
|
* @param string $appId |
122
|
|
|
* @param array $channelNames |
123
|
|
|
* @return PromiseInterface |
124
|
|
|
*/ |
125
|
|
|
public function channelMemberCounts(string $appId, array $channelNames) : PromiseInterface |
126
|
|
|
{ |
127
|
|
|
$results = []; |
128
|
|
|
|
129
|
|
|
// Count the number of users per channel |
130
|
|
|
foreach ($channelNames as $channel) { |
131
|
|
|
$results[$channel] = isset($this->channelData["$appId:$channel"]) |
132
|
|
|
? count($this->channelData["$appId:$channel"]) |
133
|
|
|
: 0; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return new FulfilledPromise($results); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|