diff --git a/.gitattributes b/.gitattributes index c7c0e5b..abecaee 100644 --- a/.gitattributes +++ b/.gitattributes @@ -2,4 +2,6 @@ /tests export-ignore /phpunit.xml export-ignore /.gitattributes export-ignore -/.gitignore export-ignore \ No newline at end of file +/.gitignore export-ignore +/CODE_OF_CONDUCT.md export-ignore +/CONTRIBUTING.md export-ignore \ No newline at end of file diff --git a/src/Components/WssMain.php b/src/Components/WssMain.php index 9aa7c05..095900c 100644 --- a/src/Components/WssMain.php +++ b/src/Components/WssMain.php @@ -146,4 +146,31 @@ private function getTypeByOpCode(string $firstByteBinary, array &$decodedData) break; } } + + /** + * Checks if there are less connections for amount of processes + * @param int $totalClients + * @param int $maxClients + */ + protected function lessConnThanProc(int $totalClients, int $maxClients): void + { + if ($totalClients !== 0 && $maxClients > $totalClients + && $totalClients % $this->config->getClientsPerFork() === 0) { + exit(1); + } + } + + /** + * Clean socket resources that were closed, + * thus avoiding (stream_select(): supplied resource is not a valid stream resource) + * @param array $readSocks + */ + protected function cleanSocketResources(array &$readSocks): void + { + foreach ($readSocks as $k => $sock) { + if (!is_resource($sock)) { + unset($readSocks[$k]); + } + } + } } diff --git a/src/WebSocketServer.php b/src/WebSocketServer.php index 8a09d7f..e01f7b9 100755 --- a/src/WebSocketServer.php +++ b/src/WebSocketServer.php @@ -20,10 +20,11 @@ */ class WebSocketServer extends WssMain implements WebSocketServerContract { + protected $config; + private $clients = []; // set any template You need ex.: GET /subscription/messenger/token private $pathParams = []; - private $config; private $handshakes = []; private $headersUpgrade = []; private $totalClients = 0; @@ -128,22 +129,12 @@ private function looping($server) $this->stepRecursion = false; $this->eventLoop($server, true); } - - if ($this->totalClients !== 0 && $this->maxClients > $this->totalClients - && $this->totalClients % $this->config->getClientsPerFork() === 0) { // there is less connection for amount of processes at this moment - exit(1); - } + $this->lessConnThanProc($this->totalClients, $this->maxClients); //prepare readable sockets $readSocks = $this->clients; $readSocks[] = $server; - - // clear socket resources that were closed, thus avoiding (stream_select(): supplied resource is not a valid stream resource) - foreach ($readSocks as $k => $sock) { - if (!is_resource($sock)) { - unset($readSocks[$k]); - } - } + $this->cleanSocketResources($readSocks); //start reading and use a large timeout if (!stream_select($readSocks, $write, $except, $this->config->getStreamSelectTimeout())) {