当前位置:Gxlcms > PHP教程 > socket-phppthreads主线程如何获取到子线程的scoket资源?

socket-phppthreads主线程如何获取到子线程的scoket资源?

时间:2021-07-01 10:21:17 帮助过:2人阅读

php pthreads主线程如何获取到子线程的scoket资源?
最近公司要做的一个业务是,要用amqp的consume方法监听队列,同时与多个终端保持连接,一旦队列里有消息就马上下发给终端,不能有延迟,
我想到的是只能用线程来做,如果用多进程来实现,socket资源是不能共享的,但是现在用了pthreads 扩展也不行,从主线程中无法获取到子线程中的连接,大侠们请教下怎么弄

  1. <code> <!--?php
  2. class PrintServer extends Thread{
  3. public $connections = array();
  4. public function __construct() {
  5. $this--->connections = array();
  6. }
  7. public function run(){
  8. $connections = array();
  9. //监听端口
  10. $PORT = 8888;
  11. $sock = socket_create_listen($PORT);
  12. if (!$sock) {
  13. exit(1);
  14. }
  15. //不阻塞
  16. socket_set_nonblock($sock);
  17. while (true) {
  18. // $readfds = array_merge($this->connections, array($sock));
  19. $readfds = array_merge($connections, array($sock));
  20. $writefds = array();
  21. //选择一个连接,获取读、写连接通道
  22. $e= null;
  23. $flag = socket_select($readfds, $writefds, $e, $t = 5);
  24. if ($flag) {
  25. foreach ($readfds as $rfd) {
  26. if ($rfd == $sock) {
  27. //接受客户端连接
  28. $newconn = socket_accept($sock);
  29. $i = (int)$newconn;
  30. //将当前客户端连接放如socket_select选择
  31. $connections[$i] = $newconn;
  32. //输入的连接资源缓存容器
  33. $writefds[$i] = $newconn;
  34. var_dump($connections);
  35. $this->connections = $connections;
  36. var_dump($this->connections);
  37. echo "Welcome to the PHP Chat Server!\n";
  38. continue;
  39. }
  40. }
  41. }
  42. echo "a\n";
  43. }
  44. }
  45. }
  46. $my = new PrintServer();
  47. $my->start();
  48. while(true){
  49. var_dump($my->connections);
  50. sleep(3);
  51. }
  52. </code>

回复内容:

php pthreads主线程如何获取到子线程的scoket资源?
最近公司要做的一个业务是,要用amqp的consume方法监听队列,同时与多个终端保持连接,一旦队列里有消息就马上下发给终端,不能有延迟,
我想到的是只能用线程来做,如果用多进程来实现,socket资源是不能共享的,但是现在用了pthreads 扩展也不行,从主线程中无法获取到子线程中的连接,大侠们请教下怎么弄

  1. <code> <!--?php
  2. class PrintServer extends Thread{
  3. public $connections = array();
  4. public function __construct() {
  5. $this--->connections = array();
  6. }
  7. public function run(){
  8. $connections = array();
  9. //监听端口
  10. $PORT = 8888;
  11. $sock = socket_create_listen($PORT);
  12. if (!$sock) {
  13. exit(1);
  14. }
  15. //不阻塞
  16. socket_set_nonblock($sock);
  17. while (true) {
  18. // $readfds = array_merge($this->connections, array($sock));
  19. $readfds = array_merge($connections, array($sock));
  20. $writefds = array();
  21. //选择一个连接,获取读、写连接通道
  22. $e= null;
  23. $flag = socket_select($readfds, $writefds, $e, $t = 5);
  24. if ($flag) {
  25. foreach ($readfds as $rfd) {
  26. if ($rfd == $sock) {
  27. //接受客户端连接
  28. $newconn = socket_accept($sock);
  29. $i = (int)$newconn;
  30. //将当前客户端连接放如socket_select选择
  31. $connections[$i] = $newconn;
  32. //输入的连接资源缓存容器
  33. $writefds[$i] = $newconn;
  34. var_dump($connections);
  35. $this->connections = $connections;
  36. var_dump($this->connections);
  37. echo "Welcome to the PHP Chat Server!\n";
  38. continue;
  39. }
  40. }
  41. }
  42. echo "a\n";
  43. }
  44. }
  45. }
  46. $my = new PrintServer();
  47. $my->start();
  48. while(true){
  49. var_dump($my->connections);
  50. sleep(3);
  51. }
  52. </code>

人气教程排行