Total Complexity | 41 |
Total Lines | 298 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like InteractsWithWebsocket often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use InteractsWithWebsocket, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | trait InteractsWithWebsocket |
||
17 | { |
||
18 | /** |
||
19 | * @var boolean |
||
20 | */ |
||
21 | protected $isWebsocket = false; |
||
22 | |||
23 | /** |
||
24 | * @var SwooleTW\Http\Websocket\HandlerContract |
||
|
|||
25 | */ |
||
26 | protected $websocketHandler; |
||
27 | |||
28 | /** |
||
29 | * @var SwooleTW\Http\Websocket\Parser |
||
30 | */ |
||
31 | protected $parser; |
||
32 | |||
33 | /** |
||
34 | * Websocket server events. |
||
35 | * |
||
36 | * @var array |
||
37 | */ |
||
38 | protected $wsEvents = ['open', 'message', 'close']; |
||
39 | |||
40 | /** |
||
41 | * "onOpen" listener. |
||
42 | * |
||
43 | * @param \Swoole\Websocket\Server $server |
||
44 | * @param \Swoole\Http\Request $swooleRequest |
||
45 | */ |
||
46 | public function onOpen($server, $swooleRequest) |
||
47 | { |
||
48 | $illuminateRequest = Request::make($swooleRequest)->toIlluminate(); |
||
49 | |||
50 | try { |
||
51 | $this->app['swoole.websocket']->reset(true)->setSender($swooleRequest->fd); |
||
52 | // set currnt request to sandbox |
||
53 | $this->app['swoole.sandbox']->setRequest($illuminateRequest); |
||
54 | // enable sandbox |
||
55 | $this->app['swoole.sandbox']->enable(); |
||
56 | // check if socket.io connection established |
||
57 | if (! $this->websocketHandler->onOpen($swooleRequest->fd, $illuminateRequest)) { |
||
58 | return; |
||
59 | } |
||
60 | // trigger 'connect' websocket event |
||
61 | if ($this->app['swoole.websocket']->eventExists('connect')) { |
||
62 | // set sandbox container to websocket pipeline |
||
63 | $this->app['swoole.websocket']->setContainer($this->app['swoole.sandbox']->getApplication()); |
||
64 | $this->app['swoole.websocket']->call('connect', $illuminateRequest); |
||
65 | } |
||
66 | } catch (Throwable $e) { |
||
67 | $this->logServerError($e); |
||
68 | } finally { |
||
69 | // disable and recycle sandbox resource |
||
70 | $this->app['swoole.sandbox']->disable(); |
||
71 | } |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * "onMessage" listener. |
||
76 | * |
||
77 | * @param \Swoole\Websocket\Server $server |
||
78 | * @param \Swoole\Websocket\Frame $frame |
||
79 | */ |
||
80 | public function onMessage($server, $frame) |
||
81 | { |
||
82 | try { |
||
83 | // execute parser strategies and skip non-message packet |
||
84 | if ($this->parser->execute($server, $frame)) { |
||
85 | return; |
||
86 | } |
||
87 | |||
88 | // decode raw message via parser |
||
89 | $payload = $this->parser->decode($frame); |
||
90 | |||
91 | $this->app['swoole.websocket']->reset(true)->setSender($frame->fd); |
||
92 | |||
93 | // enable sandbox |
||
94 | $this->app['swoole.sandbox']->enable(); |
||
95 | |||
96 | // dispatch message to registered event callback |
||
97 | if ($this->app['swoole.websocket']->eventExists($payload['event'])) { |
||
98 | $this->app['swoole.websocket']->call($payload['event'], $payload['data']); |
||
99 | } else { |
||
100 | $this->websocketHandler->onMessage($frame); |
||
101 | } |
||
102 | } catch (Throwable $e) { |
||
103 | $this->logServerError($e); |
||
104 | } finally { |
||
105 | // disable and recycle sandbox resource |
||
106 | $this->app['swoole.sandbox']->disable(); |
||
107 | } |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * "onClose" listener. |
||
112 | * |
||
113 | * @param \Swoole\Websocket\Server $server |
||
114 | * @param int $fd |
||
115 | * @param int $reactorId |
||
116 | */ |
||
117 | public function onClose($server, $fd, $reactorId) |
||
118 | { |
||
119 | if (! $this->isWebsocket($fd)) { |
||
120 | return; |
||
121 | } |
||
122 | |||
123 | try { |
||
124 | $this->app['swoole.websocket']->reset(true)->setSender($fd); |
||
125 | // trigger 'disconnect' websocket event |
||
126 | if ($this->app['swoole.websocket']->eventExists('disconnect')) { |
||
127 | $this->app['swoole.websocket']->call('disconnect'); |
||
128 | } else { |
||
129 | $this->websocketHandler->onClose($fd, $reactorId); |
||
130 | } |
||
131 | // leave all rooms |
||
132 | $this->app['swoole.websocket']->leave(); |
||
133 | } catch (Throwable $e) { |
||
134 | $this->logServerError($e); |
||
135 | } |
||
136 | } |
||
137 | |||
138 | /** |
||
139 | * Push websocket message to clients. |
||
140 | * |
||
141 | * @param \Swoole\Websocket\Server $server |
||
142 | * @param mixed $data |
||
143 | */ |
||
144 | public function pushMessage($server, array $data) |
||
145 | { |
||
146 | [$opcode, $sender, $fds, $broadcast, $assigned, $event, $message] = $this->normalizePushData($data); |
||
147 | $message = $this->parser->encode($event, $message); |
||
148 | |||
149 | // attach sender if not broadcast |
||
150 | if (! $broadcast && $sender && ! in_array($sender, $fds)) { |
||
151 | $fds[] = $sender; |
||
152 | } |
||
153 | |||
154 | // check if to broadcast all clients |
||
155 | if ($broadcast && empty($fds) && ! $assigned) { |
||
156 | foreach ($server->connections as $fd) { |
||
157 | if ($this->isWebsocket($fd)) { |
||
158 | $fds[] = $fd; |
||
159 | } |
||
160 | } |
||
161 | } |
||
162 | |||
163 | // push message to designated fds |
||
164 | foreach ($fds as $fd) { |
||
165 | if (($broadcast && $sender === (integer) $fd) || ! $server->exist($fd)) { |
||
166 | continue; |
||
167 | } |
||
168 | $server->push($fd, $message, $opcode); |
||
169 | } |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | * Set frame parser for websocket. |
||
174 | * |
||
175 | * @param \SwooleTW\Http\Websocket\Parser $parser |
||
176 | */ |
||
177 | public function setParser(Parser $parser) |
||
178 | { |
||
179 | $this->parser = $parser; |
||
180 | |||
181 | return $this; |
||
182 | } |
||
183 | |||
184 | /** |
||
185 | * Get frame parser for websocket. |
||
186 | */ |
||
187 | public function getParser() |
||
188 | { |
||
189 | return $this->parser; |
||
190 | } |
||
191 | |||
192 | /** |
||
193 | * Prepare settings if websocket is enabled. |
||
194 | */ |
||
195 | protected function prepareWebsocket() |
||
196 | { |
||
197 | $isWebsocket = $this->container['config']->get('swoole_http.websocket.enabled'); |
||
198 | $parser = $this->container['config']->get('swoole_websocket.parser'); |
||
199 | |||
200 | if ($isWebsocket) { |
||
201 | array_push($this->events, ...$this->wsEvents); |
||
202 | $this->isWebsocket = true; |
||
203 | $this->setParser(new $parser); |
||
204 | } |
||
205 | } |
||
206 | |||
207 | /** |
||
208 | * Check if it's a websocket fd. |
||
209 | */ |
||
210 | protected function isWebsocket(int $fd) |
||
211 | { |
||
212 | $info = $this->container['swoole.server']->connection_info($fd); |
||
213 | |||
214 | return array_key_exists('websocket_status', $info) && $info['websocket_status']; |
||
215 | } |
||
216 | |||
217 | /** |
||
218 | * Prepare websocket handler for onOpen and onClose callback. |
||
219 | */ |
||
220 | protected function prepareWebsocketHandler() |
||
221 | { |
||
222 | $handlerClass = $this->container['config']->get('swoole_websocket.handler'); |
||
223 | |||
224 | if (! $handlerClass) { |
||
225 | throw new Exception('Websocket handler is not set in swoole_websocket config'); |
||
226 | } |
||
227 | |||
228 | $this->setWebsocketHandler($this->app->make($handlerClass)); |
||
229 | } |
||
230 | |||
231 | /** |
||
232 | * Set websocket handler. |
||
233 | */ |
||
234 | public function setWebsocketHandler(HandlerContract $handler) |
||
235 | { |
||
236 | $this->websocketHandler = $handler; |
||
237 | |||
238 | return $this; |
||
239 | } |
||
240 | |||
241 | /** |
||
242 | * Get websocket handler. |
||
243 | */ |
||
244 | public function getWebsocketHandler() |
||
247 | } |
||
248 | |||
249 | /** |
||
250 | * Get websocket handler for onOpen and onClose callback. |
||
251 | */ |
||
252 | protected function getWebsocketRoom() |
||
253 | { |
||
254 | $driver = $this->container['config']->get('swoole_websocket.default'); |
||
255 | $configs = $this->container['config']->get("swoole_websocket.settings.{$driver}"); |
||
256 | $className = $this->container['config']->get("swoole_websocket.drivers.{$driver}"); |
||
257 | |||
258 | $websocketRoom = new $className($configs); |
||
259 | $websocketRoom->prepare(); |
||
260 | |||
261 | return $websocketRoom; |
||
262 | } |
||
263 | |||
264 | /** |
||
265 | * Bind room instance to Laravel app container. |
||
266 | */ |
||
267 | protected function bindRoom() |
||
273 | } |
||
274 | |||
275 | /** |
||
276 | * Bind websocket instance to Laravel app container. |
||
277 | */ |
||
278 | protected function bindWebsocket() |
||
279 | { |
||
280 | $this->app->singleton(Websocket::class, function ($app) { |
||
281 | return new Websocket($app['swoole.room'], new Pipeline($app)); |
||
282 | }); |
||
283 | $this->app->alias(Websocket::class, 'swoole.websocket'); |
||
284 | } |
||
285 | |||
286 | /** |
||
287 | * Load websocket routes file. |
||
288 | */ |
||
289 | protected function loadWebsocketRoutes() |
||
298 | } |
||
299 | |||
300 | /** |
||
301 | * Normalize data for message push. |
||
302 | */ |
||
303 | public function normalizePushData(array $data) |
||
314 | } |
||
315 | } |
||
316 |