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!