1 | <?php |
||
12 | class Centrifuge implements CentrifugeContract |
||
13 | { |
||
14 | const REDIS_SUFFIX = '.api'; |
||
15 | |||
16 | const API_PATH = '/api'; |
||
17 | |||
18 | /** |
||
19 | * @var \GuzzleHttp\Client |
||
20 | */ |
||
21 | protected $httpClient; |
||
22 | |||
23 | /** |
||
24 | * @var \Predis\Client |
||
25 | */ |
||
26 | protected $redisClient; |
||
27 | |||
28 | /** |
||
29 | * @var array |
||
30 | */ |
||
31 | protected $config; |
||
32 | |||
33 | /** |
||
34 | * @var array |
||
35 | */ |
||
36 | protected $redisMethods = ['publish', 'broadcast', 'unsubscribe', 'disconnect']; |
||
37 | |||
38 | /** |
||
39 | * Create a new Centrifuge instance. |
||
40 | * |
||
41 | * @param array $config |
||
42 | * @param \GuzzleHttp\Client $httpClient |
||
43 | * @param \Predis\Client|null $redisClient |
||
44 | */ |
||
45 | public function __construct(array $config, HttpClient $httpClient, RedisClient $redisClient = null) |
||
52 | |||
53 | /** |
||
54 | * Init centrifuge configuration. |
||
55 | * |
||
56 | * @param array $config |
||
57 | * @return array |
||
58 | */ |
||
59 | protected function initConfiguration(array $config) |
||
77 | |||
78 | /** |
||
79 | * Send message into channel. |
||
80 | * |
||
81 | * @param string $channel |
||
82 | * @param array $data |
||
83 | * @param string $client |
||
84 | * @return mixed |
||
85 | */ |
||
86 | public function publish(string $channel, array $data, $client = null) |
||
96 | |||
97 | /** |
||
98 | * Send message into multiple channel. |
||
99 | * |
||
100 | * @param array $channels |
||
101 | * @param array $data |
||
102 | * @param string $client |
||
103 | * @return mixed |
||
104 | */ |
||
105 | public function broadcast(array $channels, array $data, $client = null) |
||
115 | |||
116 | /** |
||
117 | * Get channel presence information (all clients currently subscribed on this channel). |
||
118 | * |
||
119 | * @param string $channel |
||
120 | * @return mixed |
||
121 | */ |
||
122 | public function presence(string $channel) |
||
123 | { |
||
124 | return $this->send('presence', ['channel' => $channel]); |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * Get channel history information (list of last messages sent into channel). |
||
129 | * |
||
130 | * @param string $channel |
||
131 | * @return mixed |
||
132 | */ |
||
133 | public function history(string $channel) |
||
137 | |||
138 | /** |
||
139 | * Unsubscribe user from channel. |
||
140 | * |
||
141 | * @param string $user_id |
||
142 | * @param string $channel |
||
143 | * @return mixed |
||
144 | */ |
||
145 | public function unsubscribe($user_id, $channel = null) |
||
155 | |||
156 | /** |
||
157 | * Disconnect user by its ID. |
||
158 | * |
||
159 | * @param string $user_id |
||
160 | * @return mixed |
||
161 | */ |
||
162 | public function disconnect($user_id) |
||
166 | |||
167 | /** |
||
168 | * Get channels information (list of currently active channels). |
||
169 | * |
||
170 | * @return mixed |
||
171 | */ |
||
172 | public function channels() |
||
176 | |||
177 | /** |
||
178 | * Get stats information about running server nodes. |
||
179 | * |
||
180 | * @return mixed |
||
181 | */ |
||
182 | public function stats() |
||
186 | |||
187 | /** |
||
188 | * Generate token. |
||
189 | * |
||
190 | * @param string $userOrClient |
||
191 | * @param string $timestampOrChannel |
||
192 | * @param string $info |
||
193 | * @return string |
||
194 | */ |
||
195 | public function generateToken($userOrClient, $timestampOrChannel, $info = '') |
||
204 | |||
205 | /** |
||
206 | * Generate api sign. |
||
207 | * |
||
208 | * @param string $data |
||
209 | * @return string |
||
210 | */ |
||
211 | public function generateApiSign($data) |
||
218 | |||
219 | /** |
||
220 | * Get secret key. |
||
221 | * |
||
222 | * @return string |
||
223 | */ |
||
224 | protected function getSecret() |
||
228 | |||
229 | /** |
||
230 | * Send message to centrifuge server. |
||
231 | * |
||
232 | * @param string $method |
||
233 | * @param array $params |
||
234 | * @return mixed |
||
235 | */ |
||
236 | protected function send(string $method, array $params = []) |
||
254 | |||
255 | /** |
||
256 | * Send message to centrifuge server from http client. |
||
257 | * |
||
258 | * @param string $method |
||
259 | * @param array $params |
||
260 | * @return mixed |
||
261 | */ |
||
262 | protected function httpSend(string $method, array $params = []) |
||
285 | |||
286 | /** |
||
287 | * Prepare URL to send the http request. |
||
288 | * |
||
289 | * @return string |
||
290 | */ |
||
291 | protected function prepareUrl() |
||
302 | |||
303 | /** |
||
304 | * Send message to centrifuge server from redis client. |
||
305 | * |
||
306 | * @param string $method |
||
307 | * @param array $params |
||
308 | * @return mixed |
||
309 | */ |
||
310 | protected function redisSend(string $method, array $params = []) |
||
326 | |||
327 | /** |
||
328 | * Get queue key for redis engine. |
||
329 | * |
||
330 | * @return string |
||
331 | */ |
||
332 | protected function getQueueKey() |
||
343 | } |
||
344 |