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) |
||
79 | |||
80 | /** |
||
81 | * Send message into channel. |
||
82 | * |
||
83 | * @param string $channel |
||
84 | * @param array $data |
||
85 | * @param string $client |
||
86 | * @return mixed |
||
87 | */ |
||
88 | public function publish($channel, array $data, $client = null) |
||
98 | |||
99 | /** |
||
100 | * Send message into multiple channel. |
||
101 | * |
||
102 | * @param array $channels |
||
103 | * @param array $data |
||
104 | * @param string $client |
||
105 | * @return mixed |
||
106 | */ |
||
107 | public function broadcast(array $channels, array $data, $client = null) |
||
117 | |||
118 | /** |
||
119 | * Get channel presence information (all clients currently subscribed on this channel). |
||
120 | * |
||
121 | * @param string $channel |
||
122 | * @return mixed |
||
123 | */ |
||
124 | public function presence($channel) |
||
128 | |||
129 | /** |
||
130 | * Get channel history information (list of last messages sent into channel). |
||
131 | * |
||
132 | * @param string $channel |
||
133 | * @return mixed |
||
134 | */ |
||
135 | public function history($channel) |
||
139 | |||
140 | /** |
||
141 | * Unsubscribe user from channel. |
||
142 | * |
||
143 | * @param string $user_id |
||
144 | * @param string $channel |
||
145 | * @return mixed |
||
146 | */ |
||
147 | public function unsubscribe($user_id, $channel = null) |
||
157 | |||
158 | /** |
||
159 | * Disconnect user by its ID. |
||
160 | * |
||
161 | * @param string $user_id |
||
162 | * @return mixed |
||
163 | */ |
||
164 | public function disconnect($user_id) |
||
168 | |||
169 | /** |
||
170 | * Get channels information (list of currently active channels). |
||
171 | * |
||
172 | * @return mixed |
||
173 | */ |
||
174 | public function channels() |
||
178 | |||
179 | /** |
||
180 | * Get stats information about running server nodes. |
||
181 | * |
||
182 | * @return mixed |
||
183 | */ |
||
184 | public function stats() |
||
188 | |||
189 | /** |
||
190 | * Generate token. |
||
191 | * |
||
192 | * @param string $userOrClient |
||
193 | * @param string $timestampOrChannel |
||
194 | * @param string $info |
||
195 | * @return string |
||
196 | */ |
||
197 | public function generateToken($userOrClient, $timestampOrChannel, $info = '') |
||
206 | |||
207 | /** |
||
208 | * Generate api sign. |
||
209 | * |
||
210 | * @param string $data |
||
211 | * @return string |
||
212 | */ |
||
213 | public function generateApiSign($data) |
||
220 | |||
221 | /** |
||
222 | * Get secret key. |
||
223 | * |
||
224 | * @return string |
||
225 | */ |
||
226 | protected function getSecret() |
||
230 | |||
231 | /** |
||
232 | * Send message to centrifuge server. |
||
233 | * |
||
234 | * @param string $method |
||
235 | * @param array $params |
||
236 | * @return mixed |
||
237 | */ |
||
238 | protected function send($method, array $params = []) |
||
257 | |||
258 | /** |
||
259 | * Send message to centrifuge server from http client. |
||
260 | * |
||
261 | * @param string $method |
||
262 | * @param array $params |
||
263 | * @return mixed |
||
264 | */ |
||
265 | protected function httpSend($method, array $params = []) |
||
300 | |||
301 | /** |
||
302 | * Prepare URL to send the http request. |
||
303 | * |
||
304 | * @return string |
||
305 | */ |
||
306 | protected function prepareUrl() |
||
317 | |||
318 | /** |
||
319 | * Send message to centrifuge server from redis client. |
||
320 | * @param $method |
||
321 | * @param array $params |
||
322 | * @return array |
||
323 | * @throws PredisException |
||
324 | */ |
||
325 | protected function redisSend($method, array $params = []) |
||
341 | |||
342 | /** |
||
343 | * Get queue key for redis engine. |
||
344 | * |
||
345 | * @return string |
||
346 | */ |
||
347 | protected function getQueueKey() |
||
358 | } |
||
359 |