Please tell me how to check for disconnection of the client from the WebSocket server. There is such a code:

 function startWebsocketServer() { $socket = stream_socket_server("tcp://0.0.0.0:8080", $errno, $errstr); if (!$socket) { die("$errstr ($errno)\n"); } $connects = array(); while (true) { //Ρ„ΠΎΡ€ΠΌΠΈΡ€ΡƒΠ΅ΠΌ массив ΠΏΡ€ΠΎΡΠ»ΡƒΡˆΠΈΠ²Π°Π΅ΠΌΡ‹Ρ… сокСтов: $read = $connects; $read []= $socket; $write = $except = null; if (!stream_select($read, $write, $except, null)) {//ΠΎΠΆΠΈΠ΄Π°Π΅ΠΌ сокСты доступныС для чтСния (Π±Π΅Π· Ρ‚Π°ΠΉΠΌΠ°ΡƒΡ‚Π°) break; } if (in_array($socket, $read)) { //Π΅ΡΡ‚ΡŒ Π½ΠΎΠ²ΠΎΠ΅ соСдинСниС //ΠΏΡ€ΠΈΠ½ΠΈΠΌΠ°Π΅ΠΌ Π½ΠΎΠ²ΠΎΠ΅ соСдинСниС ΠΈ ΠΏΡ€ΠΎΠΈΠ·Π²ΠΎΠ΄ΠΈΠΌ Ρ€ΡƒΠΊΠΎΠΏΠΎΠΆΠ°Ρ‚ΠΈΠ΅: if (($connect = stream_socket_accept($socket, -1)) && $info = handshake($connect)) { $connects[] = $connect;//добавляСм Π΅Π³ΠΎ Π² список Π½Π΅ΠΎΠ±Ρ…ΠΎΠ΄ΠΈΠΌΡ‹Ρ… для ΠΎΠ±Ρ€Π°Π±ΠΎΡ‚ΠΊΠΈ onOpen($connect, $info);//Π²Ρ‹Π·Ρ‹Π²Π°Π΅ΠΌ ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»ΡŒΡΠΊΠΈΠΉ сцСнарий } unset($read[ array_search($socket, $read) ]); } foreach($read as $connect) {//ΠΎΠ±Ρ€Π°Π±Π°Ρ‚Ρ‹Π²Π°Π΅ΠΌ всС соСдинСния $data = fread($connect, 100000); if (!$data) { //соСдинСниС Π±Ρ‹Π»ΠΎ Π·Π°ΠΊΡ€Ρ‹Ρ‚ΠΎ fclose($connect); unset($connects[ array_search($connect, $connects) ]); onClose($connect);//Π²Ρ‹Π·Ρ‹Π²Π°Π΅ΠΌ ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»ΡŒΡΠΊΠΈΠΉ сцСнарий continue; } /* * onMessage($connects, $data) для ΠΎΡ‚ΠΏΡ€Π°Π²ΠΊΠΈ ΠΊΠ°ΠΆΠ΄ΠΎΠΌΡƒ ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»ΡŽ своСго сообщСния * Π·Π°ΠΌΠ΅Π½ΠΈΡ‚ΡŒ $connects Π½Π° $connect ΠΈ ΡƒΠ±Ρ€Π°Ρ‚ΡŒ Ρ†ΠΈΠΊΠ» Π² Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΈ onMessage */ onMessage($connects, $data);//Π²Ρ‹Π·Ρ‹Π²Π°Π΅ΠΌ ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»ΡŒΡΠΊΠΈΠΉ сцСнарий, для ΠΎΡ‚ΠΏΡ€Π°Π²ΠΊΠΈ ΠΎΠ΄Π½ΠΎΠΌ } } fclose($socket); } 

It is working, but there is one problem if the client disconnects incorrectly - i.e. for example, the connection to the Internet was interrupted, then the script after some time begins to slow down terribly, until the same client again establishes connection with the server! How to implement a check here that the client has disconnected and remove a resource that is not working from the $ connects array? If you close the browser tab, then shutting down the client passes correctly and there is no problem!

0