======================================= PHP websockets ======================================= A quick introduction to test php sockets. 1. create and run listener on the command line. 2. telnet to the socket and type something. ======================================= ed /non/web/directory/socket.php --------------------------------------- $client) { // read input for each client if(in_array($client, $read)) { if(false === ($buf = socket_read($client, 2048, PHP_BINARY_READ))) { print('socket_read() failed: reason: '.socket_strerror(socket_last_error($client)).PHP_EOL); break 2; } if(!$buf = trim($buf)) { continue; } if($buf == 'quit') { unset($clients[$key]); socket_close($client); break; } if($buf == 'shutdown') { socket_close($client); break 2; } // some feedback on the client side $talkback = 'You '.$key.' said: '.$buf.PHP_EOL; socket_write($client, $talkback, strlen($talkback)); // some feedback on the server side print('Client '.$key.' said: '.$buf.PHP_EOL); } } } socket_close($socket); ?> ======================================= Start the listener on the server --------------------------------------- root@server:~ # php /non/web/directory/socket.php ======================================= Connect to the listener --------------------------------------- user@client:~ # telnet 192.168.99.100 8080 Trying 192.168.99.100... Connected to 192.168.99.100. Escape character is '^]'. Welcome to the PHP Test Server. You are customer number: 0 To quit, type "quit". To shut down the server type "shutdown" hello world! ======================================= done =======================================