1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BeyondCode\LaravelWebSockets\PubSub\Drivers; |
4
|
|
|
|
5
|
|
|
use BeyondCode\LaravelWebSockets\PubSub\ReplicationInterface; |
6
|
|
|
use React\EventLoop\LoopInterface; |
7
|
|
|
use React\Promise\FulfilledPromise; |
8
|
|
|
use React\Promise\PromiseInterface; |
9
|
|
|
use stdClass; |
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
|
|
|
* @param string|null $factoryClass |
25
|
|
|
* @return self |
26
|
|
|
*/ |
27
|
|
|
public function boot(LoopInterface $loop, $factoryClass = null): ReplicationInterface |
28
|
|
|
{ |
29
|
|
|
return $this; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Publish a payload on a specific channel, for a specific app. |
34
|
|
|
* |
35
|
|
|
* @param string $appId |
36
|
|
|
* @param string $channel |
37
|
|
|
* @param stdClass $payload |
38
|
|
|
* @return bool |
39
|
|
|
*/ |
40
|
|
|
public function publish($appId, string $channel, stdClass $payload): bool |
41
|
|
|
{ |
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($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($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
|
|
|
* @return void |
78
|
|
|
*/ |
79
|
|
|
public function joinChannel($appId, string $channel, string $socketId, string $data) |
80
|
|
|
{ |
81
|
|
|
$this->channelData["{$appId}:{$channel}"][$socketId] = $data; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Remove a member from the channel. To be called when they have |
86
|
|
|
* unsubscribed from the channel. |
87
|
|
|
* |
88
|
|
|
* @param string $appId |
89
|
|
|
* @param string $channel |
90
|
|
|
* @param string $socketId |
91
|
|
|
* @return void |
92
|
|
|
*/ |
93
|
|
|
public function leaveChannel($appId, string $channel, string $socketId) |
94
|
|
|
{ |
95
|
|
|
unset($this->channelData["{$appId}:{$channel}"][$socketId]); |
96
|
|
|
|
97
|
|
|
if (empty($this->channelData["{$appId}:{$channel}"])) { |
98
|
|
|
unset($this->channelData["{$appId}:{$channel}"]); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Retrieve the full information about the members in a presence channel. |
104
|
|
|
* |
105
|
|
|
* @param string $appId |
106
|
|
|
* @param string $channel |
107
|
|
|
* @return PromiseInterface |
108
|
|
|
*/ |
109
|
|
|
public function channelMembers($appId, string $channel): PromiseInterface |
110
|
|
|
{ |
111
|
|
|
$members = $this->channelData["{$appId}:{$channel}"] ?? []; |
112
|
|
|
|
113
|
|
|
$members = array_map(function ($user) { |
114
|
|
|
return json_decode($user); |
115
|
|
|
}, $members); |
116
|
|
|
|
117
|
|
|
return new FulfilledPromise($members); |
|
|
|
|
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Get the amount of users subscribed for each presence channel. |
122
|
|
|
* |
123
|
|
|
* @param string $appId |
124
|
|
|
* @param array $channelNames |
125
|
|
|
* @return PromiseInterface |
126
|
|
|
*/ |
127
|
|
|
public function channelMemberCounts($appId, array $channelNames): PromiseInterface |
128
|
|
|
{ |
129
|
|
|
$results = []; |
130
|
|
|
|
131
|
|
|
// Count the number of users per channel |
132
|
|
|
foreach ($channelNames as $channel) { |
133
|
|
|
$results[$channel] = isset($this->channelData["{$appId}:{$channel}"]) |
134
|
|
|
? count($this->channelData["{$appId}:{$channel}"]) |
135
|
|
|
: 0; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
return new FulfilledPromise($results); |
|
|
|
|
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.