How does the main thread of PHP pthreads get scoket resources to child threads?
A recent business to do is to use the AMQP consume method to listen to the queue, while maintaining a connection with multiple terminals, once there is a message in the queue immediately sent to the terminal, there can be no delay,
I think of is only to use the thread to do, if the use of multi-process to achieve, the socket resources can not be shared, but now with the pthreads extension is not, from the main thread can not get to the child thread connection, the heroes ask how to get
Connections = Array (); Public Function Run () {$connections = array (); Listening Port $PORT = 8888; $sock = Socket_create_listen ($PORT); if (! $sock) {exit (1); }//does not block Socket_set_nonblock ($sock); while (true) {//$readfds = Array_merge ($this->connections, Array ($sock)); $readfds = Array_merge ($connections, Array ($sock)); $writefds = Array (); Select a connection to get the read, write connection channel $e = NULL; $flag = Socket_select ($readfds, $writefds, $e, $t = 5); if ($flag) {foreach ($readfds as $RFD) {if ($rfd = = $sock) {// Accept client Connections $newconn = socket_accept ($sock); $i = (int) $newconn; Place the current client connection as Socket_select select $connections [$i] = $newconn; Enter the connection resource cache container $writefds [$i] = $newconn; Var_dump ($connections); $this->connections = $connections; Var_dump ($this->connections); echo "Welcome to the PHP Chat server!\n"; Continue }}} echo "a\n"; }}} $my = new PrintServer (); $my->start (); while (true) {Var_dump ($my->connections); Sleep (3);}
Reply content:
How does the main thread of PHP pthreads get scoket resources to child threads?
A recent business to do is to use the AMQP consume method to listen to the queue, while maintaining a connection with multiple terminals, once there is a message in the queue immediately sent to the terminal, there can be no delay,
I think of is only to use the thread to do, if the use of multi-process to achieve, the socket resources can not be shared, but now with the pthreads extension is not, from the main thread can not get to the child thread connection, the heroes ask how to get
Connections = Array (); Public Function Run () {$connections = array (); Listening Port $PORT = 8888; $sock = Socket_create_listen ($PORT); if (! $sock) {exit (1); }//does not block Socket_set_nonblock ($sock); while (true) {//$readfds = Array_merge ($this->connections, Array ($sock)); $readfds = Array_merge ($connections, Array ($sock)); $writefds = Array (); Select a connection to get the read, write connection channel $e = NULL; $flag = Socket_select ($readfds, $writefds, $e, $t = 5); if ($flag) {foreach ($readfds as $RFD) {if ($rfd = = $sock) {// Accept client Connections $newconn = socket_accept ($sock); $i = (int) $newconn; Place the current client connection as Socket_select select $connections [$i] = $newconn; Enter the connection resource cache container $writefds [$i] = $newconn; Var_dump ($connections); $this->connections = $connections; Var_dump ($this->connections); echo "Welcome to the PHP Chat server!\n"; Continue }}} echo "a\n"; }}} $my = new PrintServer (); $my->start (); while (true) {Var_dump ($my->connections); Sleep (3);}