@@ -209,7 +209,7 @@ discard block |
||
209 | 209 | */ |
210 | 210 | public function shouldBroadcast(): bool |
211 | 211 | { |
212 | - return $this->broadcast && empty($this->descriptors) && ! $this->assigned; |
|
212 | + return $this->broadcast && empty($this->descriptors) && !$this->assigned; |
|
213 | 213 | } |
214 | 214 | |
215 | 215 | /** |
@@ -221,7 +221,7 @@ discard block |
||
221 | 221 | */ |
222 | 222 | protected function getWebsocketConnections(): array |
223 | 223 | { |
224 | - return array_filter(iterator_to_array($this->server->connections), function ($fd) { |
|
224 | + return array_filter(iterator_to_array($this->server->connections), function($fd) { |
|
225 | 225 | return $this->server->isEstablished($fd); |
226 | 226 | }); |
227 | 227 | } |
@@ -233,7 +233,7 @@ discard block |
||
233 | 233 | */ |
234 | 234 | public function shouldPushToDescriptor(int $fd): bool |
235 | 235 | { |
236 | - if (! $this->server->isEstablished($fd)) { |
|
236 | + if (!$this->server->isEstablished($fd)) { |
|
237 | 237 | return false; |
238 | 238 | } |
239 | 239 | |
@@ -250,7 +250,7 @@ discard block |
||
250 | 250 | public function push($payload): void |
251 | 251 | { |
252 | 252 | // attach sender if not broadcast |
253 | - if (! $this->broadcast && $this->sender && ! $this->hasDescriptor($this->sender)) { |
|
253 | + if (!$this->broadcast && $this->sender && !$this->hasDescriptor($this->sender)) { |
|
254 | 254 | $this->addDescriptor($this->sender); |
255 | 255 | } |
256 | 256 |
@@ -30,8 +30,8 @@ |
||
30 | 30 | */ |
31 | 31 | public function write(int $masterPid, int $managerPid): void |
32 | 32 | { |
33 | - if (! is_writable($this->pidFile) |
|
34 | - && ! is_writable(dirname($this->pidFile)) |
|
33 | + if (!is_writable($this->pidFile) |
|
34 | + && !is_writable(dirname($this->pidFile)) |
|
35 | 35 | ) { |
36 | 36 | throw new \RuntimeException( |
37 | 37 | sprintf('Pid file "%s" is not writable', $this->pidFile) |
@@ -133,7 +133,7 @@ discard block |
||
133 | 133 | */ |
134 | 134 | protected function stop() |
135 | 135 | { |
136 | - if (! $this->isRunning()) { |
|
136 | + if (!$this->isRunning()) { |
|
137 | 137 | $this->error("Failed! There is no swoole_http_server process running."); |
138 | 138 | |
139 | 139 | return; |
@@ -173,7 +173,7 @@ discard block |
||
173 | 173 | */ |
174 | 174 | protected function reload() |
175 | 175 | { |
176 | - if (! $this->isRunning()) { |
|
176 | + if (!$this->isRunning()) { |
|
177 | 177 | $this->error("Failed! There is no swoole_http_server process running."); |
178 | 178 | |
179 | 179 | return; |
@@ -181,7 +181,7 @@ discard block |
||
181 | 181 | |
182 | 182 | $this->info('Reloading swoole_http_server...'); |
183 | 183 | |
184 | - if (! $this->killProcess(SIGUSR1)) { |
|
184 | + if (!$this->killProcess(SIGUSR1)) { |
|
185 | 185 | $this->error('> failure'); |
186 | 186 | |
187 | 187 | return; |
@@ -241,7 +241,7 @@ discard block |
||
241 | 241 | { |
242 | 242 | $this->action = $this->argument('action'); |
243 | 243 | |
244 | - if (! in_array($this->action, ['start', 'stop', 'restart', 'reload', 'infos'], true)) { |
|
244 | + if (!in_array($this->action, ['start', 'stop', 'restart', 'reload', 'infos'], true)) { |
|
245 | 245 | $this->error( |
246 | 246 | "Invalid argument '{$this->action}'. Expected 'start', 'stop', 'restart', 'reload' or 'infos'." |
247 | 247 | ); |
@@ -262,7 +262,7 @@ discard block |
||
262 | 262 | $filter = Arr::get($this->config, 'hot_reload.filter'); |
263 | 263 | $log = Arr::get($this->config, 'hot_reload.log'); |
264 | 264 | |
265 | - $cb = function (FSEvent $event) use ($server, $log) { |
|
265 | + $cb = function(FSEvent $event) use ($server, $log) { |
|
266 | 266 | $log ? $this->info(FSOutput::format($event)) : null; |
267 | 267 | $server->reload(); |
268 | 268 | }; |
@@ -281,7 +281,7 @@ discard block |
||
281 | 281 | { |
282 | 282 | $pids = $this->laravel->make(PidManager::class)->read(); |
283 | 283 | |
284 | - if (! count($pids)) { |
|
284 | + if (!count($pids)) { |
|
285 | 285 | return false; |
286 | 286 | } |
287 | 287 | |
@@ -315,7 +315,7 @@ discard block |
||
315 | 315 | $start = time(); |
316 | 316 | |
317 | 317 | do { |
318 | - if (! $this->isRunning()) { |
|
318 | + if (!$this->isRunning()) { |
|
319 | 319 | break; |
320 | 320 | } |
321 | 321 | |
@@ -345,13 +345,13 @@ discard block |
||
345 | 345 | exit(1); |
346 | 346 | } |
347 | 347 | |
348 | - if (! extension_loaded('swoole')) { |
|
348 | + if (!extension_loaded('swoole')) { |
|
349 | 349 | $this->error('Can\'t detect Swoole extension installed.'); |
350 | 350 | |
351 | 351 | exit(1); |
352 | 352 | } |
353 | 353 | |
354 | - if (! version_compare(swoole_version(), '4.3.1', 'ge')) { |
|
354 | + if (!version_compare(swoole_version(), '4.3.1', 'ge')) { |
|
355 | 355 | $this->error('Your Swoole version must be higher than `4.3.1`.'); |
356 | 356 | |
357 | 357 | exit(1); |
@@ -363,15 +363,15 @@ discard block |
||
363 | 363 | */ |
364 | 364 | protected function registerAccessLog() |
365 | 365 | { |
366 | - $this->laravel->singleton(OutputStyle::class, function () { |
|
366 | + $this->laravel->singleton(OutputStyle::class, function() { |
|
367 | 367 | return new OutputStyle($this->input, $this->output); |
368 | 368 | }); |
369 | 369 | |
370 | - $this->laravel->singleton(AccessOutput::class, function () { |
|
370 | + $this->laravel->singleton(AccessOutput::class, function() { |
|
371 | 371 | return new AccessOutput(new ConsoleOutput); |
372 | 372 | }); |
373 | 373 | |
374 | - $this->laravel->singleton(AccessLog::class, function (Container $container) { |
|
374 | + $this->laravel->singleton(AccessLog::class, function(Container $container) { |
|
375 | 375 | return new AccessLog($container->make(AccessOutput::class)); |
376 | 376 | }); |
377 | 377 | } |
@@ -88,11 +88,11 @@ discard block |
||
88 | 88 | // enable sandbox |
89 | 89 | $sandbox->enable(); |
90 | 90 | // call customized handshake handler |
91 | - if ($response && ! $this->app->make($handshakeHandler)->handle($swooleRequest, $response)) { |
|
91 | + if ($response && !$this->app->make($handshakeHandler)->handle($swooleRequest, $response)) { |
|
92 | 92 | return; |
93 | 93 | } |
94 | 94 | // check if socket.io connection established |
95 | - if (! $this->websocketHandler->onOpen($swooleRequest->fd, $illuminateRequest)) { |
|
95 | + if (!$this->websocketHandler->onOpen($swooleRequest->fd, $illuminateRequest)) { |
|
96 | 96 | return; |
97 | 97 | } |
98 | 98 | // trigger 'connect' websocket event |
@@ -156,7 +156,7 @@ discard block |
||
156 | 156 | */ |
157 | 157 | public function onClose($server, $fd, $reactorId) |
158 | 158 | { |
159 | - if (! $this->isServerWebsocket($fd) || ! $server instanceof WebsocketServer) { |
|
159 | + if (!$this->isServerWebsocket($fd) || !$server instanceof WebsocketServer) { |
|
160 | 160 | return; |
161 | 161 | } |
162 | 162 | |
@@ -186,7 +186,7 @@ discard block |
||
186 | 186 | */ |
187 | 187 | protected function isWebsocketPushPacket($packet) |
188 | 188 | { |
189 | - if (! is_array($packet)) { |
|
189 | + if (!is_array($packet)) { |
|
190 | 190 | return false; |
191 | 191 | } |
192 | 192 | |
@@ -241,7 +241,7 @@ discard block |
||
241 | 241 | $config = $this->container->make('config'); |
242 | 242 | $parser = $config->get('swoole_websocket.parser'); |
243 | 243 | |
244 | - if (! $this->isServerWebsocket = $config->get('swoole_http.websocket.enabled')) { |
|
244 | + if (!$this->isServerWebsocket = $config->get('swoole_http.websocket.enabled')) { |
|
245 | 245 | return; |
246 | 246 | } |
247 | 247 | |
@@ -279,7 +279,7 @@ discard block |
||
279 | 279 | { |
280 | 280 | $handlerClass = $this->container->make('config')->get('swoole_websocket.handler'); |
281 | 281 | |
282 | - if (! $handlerClass) { |
|
282 | + if (!$handlerClass) { |
|
283 | 283 | throw new WebsocketNotSetInConfigException; |
284 | 284 | } |
285 | 285 | |
@@ -340,7 +340,7 @@ discard block |
||
340 | 340 | */ |
341 | 341 | protected function bindRoom(): void |
342 | 342 | { |
343 | - $this->app->singleton(RoomContract::class, function () { |
|
343 | + $this->app->singleton(RoomContract::class, function() { |
|
344 | 344 | return $this->websocketRoom; |
345 | 345 | }); |
346 | 346 | |
@@ -352,7 +352,7 @@ discard block |
||
352 | 352 | */ |
353 | 353 | protected function bindWebsocket() |
354 | 354 | { |
355 | - $this->app->singleton(Websocket::class, function (Container $app) { |
|
355 | + $this->app->singleton(Websocket::class, function(Container $app) { |
|
356 | 356 | return new Websocket($app->make(RoomContract::class), new Pipeline($app)); |
357 | 357 | }); |
358 | 358 | |
@@ -367,7 +367,7 @@ discard block |
||
367 | 367 | $routePath = $this->container->make('config') |
368 | 368 | ->get('swoole_websocket.route_file'); |
369 | 369 | |
370 | - if (! file_exists($routePath)) { |
|
370 | + if (!file_exists($routePath)) { |
|
371 | 371 | $routePath = __DIR__ . '/../../routes/websocket.php'; |
372 | 372 | } |
373 | 373 | |
@@ -383,7 +383,7 @@ discard block |
||
383 | 383 | */ |
384 | 384 | public function isWebsocketPushPayload($payload): bool |
385 | 385 | { |
386 | - if (! is_array($payload)) { |
|
386 | + if (!is_array($payload)) { |
|
387 | 387 | return false; |
388 | 388 | } |
389 | 389 |
@@ -44,7 +44,7 @@ |
||
44 | 44 | try { |
45 | 45 | $this->auth->setRequest($request); |
46 | 46 | if ($user = $this->auth->authenticate()) { |
47 | - $request->setUserResolver(function () use ($user) { |
|
47 | + $request->setUserResolver(function() use ($user) { |
|
48 | 48 | return $user; |
49 | 49 | }); |
50 | 50 | } |
@@ -115,7 +115,7 @@ discard block |
||
115 | 115 | $this->createTables(); |
116 | 116 | $this->prepareWebsocket(); |
117 | 117 | |
118 | - if (! $this->container->make(Server::class)->taskworker) { |
|
118 | + if (!$this->container->make(Server::class)->taskworker) { |
|
119 | 119 | $this->setSwooleServerListeners(); |
120 | 120 | } |
121 | 121 | } |
@@ -128,7 +128,7 @@ discard block |
||
128 | 128 | $server = $this->container->make(Server::class); |
129 | 129 | foreach ($this->events as $event) { |
130 | 130 | $listener = Str::camel("on_$event"); |
131 | - $callback = method_exists($this, $listener) ? [$this, $listener] : function () use ($event) { |
|
131 | + $callback = method_exists($this, $listener) ? [$this, $listener] : function() use ($event) { |
|
132 | 132 | $this->container->make('events')->dispatch("swoole.$event", func_get_args()); |
133 | 133 | }; |
134 | 134 | |
@@ -322,7 +322,7 @@ discard block |
||
322 | 322 | */ |
323 | 323 | protected function bindSandbox() |
324 | 324 | { |
325 | - $this->app->singleton(Sandbox::class, function ($app) { |
|
325 | + $this->app->singleton(Sandbox::class, function($app) { |
|
326 | 326 | return new Sandbox($app, $this->framework); |
327 | 327 | }); |
328 | 328 | |
@@ -410,7 +410,7 @@ discard block |
||
410 | 410 | */ |
411 | 411 | protected function normalizeException(Throwable $e) |
412 | 412 | { |
413 | - if (! $e instanceof Exception) { |
|
413 | + if (!$e instanceof Exception) { |
|
414 | 414 | $e = new FatalThrowableError($e); |
415 | 415 | } |
416 | 416 |
@@ -10,214 +10,214 @@ |
||
10 | 10 | */ |
11 | 11 | class RedisRoom implements RoomContract |
12 | 12 | { |
13 | - /** |
|
13 | + /** |
|
14 | 14 | * The Redis factory implementation. |
15 | 15 | * |
16 | 16 | * @var \Illuminate\Contracts\Redis\Factory |
17 | 17 | */ |
18 | 18 | public $redis; |
19 | 19 | |
20 | - /** |
|
21 | - * @var array |
|
22 | - */ |
|
23 | - protected $config; |
|
24 | - |
|
25 | - /** |
|
26 | - * RedisRoom constructor. |
|
27 | - * |
|
28 | - * @param array $config |
|
29 | - */ |
|
30 | - public function __construct(array $config, RedisFactory $redis) |
|
31 | - { |
|
32 | - $this->config = $config; |
|
33 | - $this->redis = $redis; |
|
34 | - } |
|
35 | - |
|
36 | - /** |
|
37 | - * @param \Illuminate\Contracts\Redis\Factory|null $redis |
|
38 | - * |
|
39 | - * @return \Ofcold\HttpSwoole\Websocket\Rooms\RoomContract |
|
40 | - */ |
|
41 | - public function prepare(RedisFactory $redis = null): RoomContract |
|
42 | - { |
|
43 | - $this->cleanRooms(); |
|
44 | - |
|
45 | - return $this; |
|
46 | - } |
|
47 | - |
|
48 | - /** |
|
49 | - * Get redis client. |
|
50 | - */ |
|
51 | - public function getRedis() |
|
52 | - { |
|
53 | - return $this->connection(); |
|
54 | - } |
|
55 | - |
|
56 | - /** |
|
57 | - * Add multiple socket fds to a room. |
|
58 | - * |
|
59 | - * @param int fd |
|
60 | - * @param array|string rooms |
|
61 | - */ |
|
62 | - public function add(int $fd, $rooms) |
|
63 | - { |
|
64 | - $rooms = is_array($rooms) ? $rooms : [$rooms]; |
|
65 | - |
|
66 | - $this->addValue($fd, $rooms, RoomContract::DESCRIPTORS_KEY); |
|
67 | - |
|
68 | - foreach ($rooms as $room) { |
|
69 | - $this->addValue($room, [$fd], RoomContract::ROOMS_KEY); |
|
70 | - } |
|
71 | - } |
|
72 | - |
|
73 | - /** |
|
74 | - * Delete multiple socket fds from a room. |
|
75 | - * |
|
76 | - * @param int fd |
|
77 | - * @param array|string rooms |
|
78 | - */ |
|
79 | - public function delete(int $fd, $rooms) |
|
80 | - { |
|
81 | - $rooms = is_array($rooms) ? $rooms : [$rooms]; |
|
82 | - $rooms = count($rooms) ? $rooms : $this->getRooms($fd); |
|
83 | - |
|
84 | - $this->removeValue($fd, $rooms, RoomContract::DESCRIPTORS_KEY); |
|
85 | - |
|
86 | - foreach ($rooms as $room) { |
|
87 | - $this->removeValue($room, [$fd], RoomContract::ROOMS_KEY); |
|
88 | - } |
|
89 | - } |
|
90 | - |
|
91 | - /** |
|
92 | - * Add value to redis. |
|
93 | - * |
|
94 | - * @param $key |
|
95 | - * @param array $values |
|
96 | - * @param string $table |
|
97 | - * |
|
98 | - * @return $this |
|
99 | - */ |
|
100 | - public function addValue($key, array $values, string $table) |
|
101 | - { |
|
102 | - $this->checkTable($table); |
|
103 | - $redisKey = $this->getKey($key, $table); |
|
104 | - |
|
105 | - $this->connection()->pipeline(function ($pipe) use ($redisKey, $values) { |
|
106 | - foreach ($values as $value) { |
|
107 | - $pipe->sadd($redisKey, $value); |
|
108 | - } |
|
109 | - }); |
|
110 | - |
|
111 | - return $this; |
|
112 | - } |
|
113 | - |
|
114 | - /** |
|
115 | - * Remove value from reddis. |
|
116 | - * |
|
117 | - * @param $key |
|
118 | - * @param array $values |
|
119 | - * @param string $table |
|
120 | - * |
|
121 | - * @return $this |
|
122 | - */ |
|
123 | - public function removeValue($key, array $values, string $table) |
|
124 | - { |
|
125 | - $this->checkTable($table); |
|
126 | - $redisKey = $this->getKey($key, $table); |
|
127 | - |
|
128 | - $this->connection()->pipeline(function ($pipe) use ($redisKey, $values) { |
|
129 | - foreach ($values as $value) { |
|
130 | - $pipe->srem($redisKey, $value); |
|
131 | - } |
|
132 | - }); |
|
133 | - |
|
134 | - return $this; |
|
135 | - } |
|
136 | - |
|
137 | - /** |
|
138 | - * Get all sockets by a room key. |
|
139 | - * |
|
140 | - * @param string room |
|
141 | - * |
|
142 | - * @return array |
|
143 | - */ |
|
144 | - public function getClients(string $room) |
|
145 | - { |
|
146 | - return $this->getValue($room, RoomContract::ROOMS_KEY) ?? []; |
|
147 | - } |
|
148 | - |
|
149 | - /** |
|
150 | - * Get all rooms by a fd. |
|
151 | - * |
|
152 | - * @param int fd |
|
153 | - * |
|
154 | - * @return array |
|
155 | - */ |
|
156 | - public function getRooms(int $fd) |
|
157 | - { |
|
158 | - return $this->getValue($fd, RoomContract::DESCRIPTORS_KEY) ?? []; |
|
159 | - } |
|
160 | - |
|
161 | - /** |
|
162 | - * Check table for rooms and descriptors. |
|
163 | - * |
|
164 | - * @param string $table |
|
165 | - */ |
|
166 | - protected function checkTable(string $table) |
|
167 | - { |
|
168 | - if (! in_array($table, [RoomContract::ROOMS_KEY, RoomContract::DESCRIPTORS_KEY])) { |
|
169 | - throw new \InvalidArgumentException("Invalid table name: `{$table}`."); |
|
170 | - } |
|
171 | - } |
|
172 | - |
|
173 | - /** |
|
174 | - * Get value. |
|
175 | - * |
|
176 | - * @param string $key |
|
177 | - * @param string $table |
|
178 | - * |
|
179 | - * @return array |
|
180 | - */ |
|
181 | - public function getValue(string $key, string $table) |
|
182 | - { |
|
183 | - $this->checkTable($table); |
|
184 | - |
|
185 | - $result = $this->connection()->smembers($this->getKey($key, $table)); |
|
186 | - |
|
187 | - // Try to fix occasional non-array returned result |
|
188 | - return is_array($result) ? $result : []; |
|
189 | - } |
|
190 | - |
|
191 | - /** |
|
192 | - * Get key. |
|
193 | - * |
|
194 | - * @param string $key |
|
195 | - * @param string $table |
|
196 | - * |
|
197 | - * @return string |
|
198 | - */ |
|
199 | - public function getKey(string $key, string $table) |
|
200 | - { |
|
201 | - return "{$table}:{$key}"; |
|
202 | - } |
|
203 | - |
|
204 | - /** |
|
205 | - * Clean all rooms. |
|
206 | - */ |
|
207 | - protected function cleanRooms(): void |
|
208 | - { |
|
209 | - if (count($keys = $this->connection()->keys("*"))) { |
|
210 | - $this->connection()->del($keys); |
|
211 | - } |
|
212 | - } |
|
213 | - |
|
214 | - /** |
|
215 | - * Get the Redis connection instance. |
|
216 | - * |
|
217 | - * @return \Illuminate\Redis\Connections\Connection |
|
218 | - */ |
|
219 | - public function connection() |
|
220 | - { |
|
221 | - return $this->redis->connection('swoole'); |
|
222 | - } |
|
20 | + /** |
|
21 | + * @var array |
|
22 | + */ |
|
23 | + protected $config; |
|
24 | + |
|
25 | + /** |
|
26 | + * RedisRoom constructor. |
|
27 | + * |
|
28 | + * @param array $config |
|
29 | + */ |
|
30 | + public function __construct(array $config, RedisFactory $redis) |
|
31 | + { |
|
32 | + $this->config = $config; |
|
33 | + $this->redis = $redis; |
|
34 | + } |
|
35 | + |
|
36 | + /** |
|
37 | + * @param \Illuminate\Contracts\Redis\Factory|null $redis |
|
38 | + * |
|
39 | + * @return \Ofcold\HttpSwoole\Websocket\Rooms\RoomContract |
|
40 | + */ |
|
41 | + public function prepare(RedisFactory $redis = null): RoomContract |
|
42 | + { |
|
43 | + $this->cleanRooms(); |
|
44 | + |
|
45 | + return $this; |
|
46 | + } |
|
47 | + |
|
48 | + /** |
|
49 | + * Get redis client. |
|
50 | + */ |
|
51 | + public function getRedis() |
|
52 | + { |
|
53 | + return $this->connection(); |
|
54 | + } |
|
55 | + |
|
56 | + /** |
|
57 | + * Add multiple socket fds to a room. |
|
58 | + * |
|
59 | + * @param int fd |
|
60 | + * @param array|string rooms |
|
61 | + */ |
|
62 | + public function add(int $fd, $rooms) |
|
63 | + { |
|
64 | + $rooms = is_array($rooms) ? $rooms : [$rooms]; |
|
65 | + |
|
66 | + $this->addValue($fd, $rooms, RoomContract::DESCRIPTORS_KEY); |
|
67 | + |
|
68 | + foreach ($rooms as $room) { |
|
69 | + $this->addValue($room, [$fd], RoomContract::ROOMS_KEY); |
|
70 | + } |
|
71 | + } |
|
72 | + |
|
73 | + /** |
|
74 | + * Delete multiple socket fds from a room. |
|
75 | + * |
|
76 | + * @param int fd |
|
77 | + * @param array|string rooms |
|
78 | + */ |
|
79 | + public function delete(int $fd, $rooms) |
|
80 | + { |
|
81 | + $rooms = is_array($rooms) ? $rooms : [$rooms]; |
|
82 | + $rooms = count($rooms) ? $rooms : $this->getRooms($fd); |
|
83 | + |
|
84 | + $this->removeValue($fd, $rooms, RoomContract::DESCRIPTORS_KEY); |
|
85 | + |
|
86 | + foreach ($rooms as $room) { |
|
87 | + $this->removeValue($room, [$fd], RoomContract::ROOMS_KEY); |
|
88 | + } |
|
89 | + } |
|
90 | + |
|
91 | + /** |
|
92 | + * Add value to redis. |
|
93 | + * |
|
94 | + * @param $key |
|
95 | + * @param array $values |
|
96 | + * @param string $table |
|
97 | + * |
|
98 | + * @return $this |
|
99 | + */ |
|
100 | + public function addValue($key, array $values, string $table) |
|
101 | + { |
|
102 | + $this->checkTable($table); |
|
103 | + $redisKey = $this->getKey($key, $table); |
|
104 | + |
|
105 | + $this->connection()->pipeline(function ($pipe) use ($redisKey, $values) { |
|
106 | + foreach ($values as $value) { |
|
107 | + $pipe->sadd($redisKey, $value); |
|
108 | + } |
|
109 | + }); |
|
110 | + |
|
111 | + return $this; |
|
112 | + } |
|
113 | + |
|
114 | + /** |
|
115 | + * Remove value from reddis. |
|
116 | + * |
|
117 | + * @param $key |
|
118 | + * @param array $values |
|
119 | + * @param string $table |
|
120 | + * |
|
121 | + * @return $this |
|
122 | + */ |
|
123 | + public function removeValue($key, array $values, string $table) |
|
124 | + { |
|
125 | + $this->checkTable($table); |
|
126 | + $redisKey = $this->getKey($key, $table); |
|
127 | + |
|
128 | + $this->connection()->pipeline(function ($pipe) use ($redisKey, $values) { |
|
129 | + foreach ($values as $value) { |
|
130 | + $pipe->srem($redisKey, $value); |
|
131 | + } |
|
132 | + }); |
|
133 | + |
|
134 | + return $this; |
|
135 | + } |
|
136 | + |
|
137 | + /** |
|
138 | + * Get all sockets by a room key. |
|
139 | + * |
|
140 | + * @param string room |
|
141 | + * |
|
142 | + * @return array |
|
143 | + */ |
|
144 | + public function getClients(string $room) |
|
145 | + { |
|
146 | + return $this->getValue($room, RoomContract::ROOMS_KEY) ?? []; |
|
147 | + } |
|
148 | + |
|
149 | + /** |
|
150 | + * Get all rooms by a fd. |
|
151 | + * |
|
152 | + * @param int fd |
|
153 | + * |
|
154 | + * @return array |
|
155 | + */ |
|
156 | + public function getRooms(int $fd) |
|
157 | + { |
|
158 | + return $this->getValue($fd, RoomContract::DESCRIPTORS_KEY) ?? []; |
|
159 | + } |
|
160 | + |
|
161 | + /** |
|
162 | + * Check table for rooms and descriptors. |
|
163 | + * |
|
164 | + * @param string $table |
|
165 | + */ |
|
166 | + protected function checkTable(string $table) |
|
167 | + { |
|
168 | + if (! in_array($table, [RoomContract::ROOMS_KEY, RoomContract::DESCRIPTORS_KEY])) { |
|
169 | + throw new \InvalidArgumentException("Invalid table name: `{$table}`."); |
|
170 | + } |
|
171 | + } |
|
172 | + |
|
173 | + /** |
|
174 | + * Get value. |
|
175 | + * |
|
176 | + * @param string $key |
|
177 | + * @param string $table |
|
178 | + * |
|
179 | + * @return array |
|
180 | + */ |
|
181 | + public function getValue(string $key, string $table) |
|
182 | + { |
|
183 | + $this->checkTable($table); |
|
184 | + |
|
185 | + $result = $this->connection()->smembers($this->getKey($key, $table)); |
|
186 | + |
|
187 | + // Try to fix occasional non-array returned result |
|
188 | + return is_array($result) ? $result : []; |
|
189 | + } |
|
190 | + |
|
191 | + /** |
|
192 | + * Get key. |
|
193 | + * |
|
194 | + * @param string $key |
|
195 | + * @param string $table |
|
196 | + * |
|
197 | + * @return string |
|
198 | + */ |
|
199 | + public function getKey(string $key, string $table) |
|
200 | + { |
|
201 | + return "{$table}:{$key}"; |
|
202 | + } |
|
203 | + |
|
204 | + /** |
|
205 | + * Clean all rooms. |
|
206 | + */ |
|
207 | + protected function cleanRooms(): void |
|
208 | + { |
|
209 | + if (count($keys = $this->connection()->keys("*"))) { |
|
210 | + $this->connection()->del($keys); |
|
211 | + } |
|
212 | + } |
|
213 | + |
|
214 | + /** |
|
215 | + * Get the Redis connection instance. |
|
216 | + * |
|
217 | + * @return \Illuminate\Redis\Connections\Connection |
|
218 | + */ |
|
219 | + public function connection() |
|
220 | + { |
|
221 | + return $this->redis->connection('swoole'); |
|
222 | + } |
|
223 | 223 | } |
@@ -102,7 +102,7 @@ discard block |
||
102 | 102 | $this->checkTable($table); |
103 | 103 | $redisKey = $this->getKey($key, $table); |
104 | 104 | |
105 | - $this->connection()->pipeline(function ($pipe) use ($redisKey, $values) { |
|
105 | + $this->connection()->pipeline(function($pipe) use ($redisKey, $values) { |
|
106 | 106 | foreach ($values as $value) { |
107 | 107 | $pipe->sadd($redisKey, $value); |
108 | 108 | } |
@@ -125,7 +125,7 @@ discard block |
||
125 | 125 | $this->checkTable($table); |
126 | 126 | $redisKey = $this->getKey($key, $table); |
127 | 127 | |
128 | - $this->connection()->pipeline(function ($pipe) use ($redisKey, $values) { |
|
128 | + $this->connection()->pipeline(function($pipe) use ($redisKey, $values) { |
|
129 | 129 | foreach ($values as $value) { |
130 | 130 | $pipe->srem($redisKey, $value); |
131 | 131 | } |
@@ -165,7 +165,7 @@ discard block |
||
165 | 165 | */ |
166 | 166 | protected function checkTable(string $table) |
167 | 167 | { |
168 | - if (! in_array($table, [RoomContract::ROOMS_KEY, RoomContract::DESCRIPTORS_KEY])) { |
|
168 | + if (!in_array($table, [RoomContract::ROOMS_KEY, RoomContract::DESCRIPTORS_KEY])) { |
|
169 | 169 | throw new \InvalidArgumentException("Invalid table name: `{$table}`."); |
170 | 170 | } |
171 | 171 | } |