Complex classes like WebSocketServer 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 WebSocketServer, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | class WebSocketServer extends WssMain implements WebSocketServerContract |
||
21 | { |
||
22 | |||
23 | private $clients = []; |
||
24 | // set any template You need ex.: GET /subscription/messenger/token |
||
25 | private $pathParams = []; |
||
26 | private $config; |
||
27 | private $handshakes = []; |
||
28 | private $headersUpgrade = []; |
||
29 | private $totalClients = 0; |
||
30 | private $maxClients = 1; |
||
31 | private $handler; |
||
32 | private $cureentConn; |
||
33 | |||
34 | // for the very 1st time must be true |
||
35 | private $stepRecursion = true; |
||
36 | |||
37 | private const MAX_BYTES_READ = 8192; |
||
38 | private const HEADER_BYTES_READ = 1024; |
||
39 | |||
40 | // stream non-blocking |
||
41 | public const NON_BLOCK = 0; |
||
42 | |||
43 | /** |
||
44 | * WebSocketServer constructor. |
||
45 | * |
||
46 | * @param WebSocket $handler |
||
47 | * @param ServerConfig $config |
||
48 | */ |
||
49 | public function __construct( |
||
59 | |||
60 | /** |
||
61 | * Runs main process - Anscestor with server socket on TCP |
||
62 | * |
||
63 | * @throws WebSocketException |
||
64 | * @throws ConnectionException |
||
65 | */ |
||
66 | public function run() |
||
80 | |||
81 | /** |
||
82 | * Recursive event loop that input intu recusion by remainder = 0 - thus when N users, |
||
83 | * and when forks equals true which prevents it from infinite recursive iterations |
||
84 | * |
||
85 | * @param resource $server server connection |
||
86 | * @param bool $fork flag to fork or run event loop |
||
87 | * @throws WebSocketException |
||
88 | * @throws ConnectionException |
||
89 | */ |
||
90 | private function eventLoop($server, bool $fork = false) |
||
103 | |||
104 | /** |
||
105 | * @param resource $server |
||
106 | * @throws WebSocketException |
||
107 | * @throws ConnectionException |
||
108 | */ |
||
109 | private function looping($server) |
||
110 | { |
||
111 | while (true) { |
||
112 | $this->totalClients = count($this->clients) + 1; |
||
113 | |||
114 | // maxClients prevents process fork on count down |
||
115 | if ($this->totalClients > $this->maxClients) { |
||
116 | $this->maxClients = $this->totalClients; |
||
117 | } |
||
118 | |||
119 | $doFork = $this->config->isForking() === true |
||
120 | && $this->totalClients !== 0 // avoid 0 process creation |
||
121 | && $this->stepRecursion === true // only once |
||
122 | && $this->maxClients === $this->totalClients // only if stack grows |
||
123 | && $this->totalClients % $this->config->getClientsPerFork() === 0; // only when N is there |
||
124 | if ($doFork) { |
||
125 | $this->stepRecursion = false; |
||
126 | $this->eventLoop($server, true); |
||
127 | } |
||
128 | |||
129 | if ($this->totalClients !== 0 && $this->maxClients > $this->totalClients |
||
130 | && $this->totalClients % $this->config->getClientsPerFork() === 0) { // there is less connection for amount of processes at this moment |
||
131 | exit(1); |
||
132 | } |
||
133 | |||
134 | //prepare readable sockets |
||
135 | $readSocks = $this->clients; |
||
136 | $readSocks[] = $server; |
||
137 | |||
138 | // clear socket resources that were closed, thus avoiding (stream_select(): supplied resource is not a valid stream resource) |
||
139 | foreach ($readSocks as $k => $sock) { |
||
140 | if (!is_resource($sock)) { |
||
141 | unset($readSocks[$k]); |
||
142 | } |
||
143 | } |
||
144 | |||
145 | //start reading and use a large timeout |
||
146 | if (!stream_select($readSocks, $write, $except, $this->config->getStreamSelectTimeout())) { |
||
147 | throw new WebSocketException('something went wrong while selecting', CommonsContract::SERVER_SELECT_ERROR); |
||
148 | } |
||
149 | |||
150 | //new client |
||
151 | if (in_array($server, $readSocks, false)) { |
||
152 | $this->acceptNewClient($server, $readSocks); |
||
153 | } |
||
154 | |||
155 | //message from existing client |
||
156 | $this->messagesWorker($readSocks); |
||
157 | } |
||
158 | } |
||
159 | |||
160 | /** |
||
161 | * @param resource $server |
||
162 | * @param array $readSocks |
||
163 | * @throws ConnectionException |
||
164 | */ |
||
165 | private function acceptNewClient($server, array &$readSocks) |
||
187 | |||
188 | /** |
||
189 | * @uses onMessage |
||
190 | * @uses onPing |
||
191 | * @uses onPong |
||
192 | * @param array $readSocks |
||
193 | */ |
||
194 | private function messagesWorker(array $readSocks) |
||
226 | |||
227 | /** |
||
228 | * Handshakes/upgrade and key parse |
||
229 | * |
||
230 | * @param resource $client Source client socket to write |
||
231 | * @param string $headers Headers that client has been sent |
||
232 | * @return string socket handshake key (Sec-WebSocket-Key)| false on parse error |
||
233 | * @throws ConnectionException |
||
234 | */ |
||
235 | private function handshake($client, string $headers): string |
||
255 | |||
256 | /** |
||
257 | * Sets an array of headers needed to upgrade server/client connection |
||
258 | * |
||
259 | * @param string $secWebSocketAccept base64 encoded Sec-WebSocket-Accept header |
||
260 | */ |
||
261 | private function setHeadersUpgrade($secWebSocketAccept) |
||
270 | |||
271 | /** |
||
272 | * Retreives headers from an array of headers to upgrade server/client connection |
||
273 | * |
||
274 | * @return string Headers to Upgrade communication connection |
||
275 | * @throws ConnectionException |
||
276 | */ |
||
277 | private function getHeadersUpgrade(): string |
||
293 | |||
294 | /** |
||
295 | * Parses parameters from GET on web-socket client connection before handshake |
||
296 | * |
||
297 | * @param string $headers |
||
298 | */ |
||
299 | private function setPathParams(string $headers) |
||
322 | } |
||
323 |
If you suppress an error, we recommend checking for the error condition explicitly: