Chinaunix首页 | 论坛 | 博客
  • 博客访问: 910000
  • 博文数量: 91
  • 博客积分: 803
  • 博客等级: 准尉
  • 技术积分: 1051
  • 用 户 组: 普通用户
  • 注册时间: 2012-05-24 13:42
文章分类

全部博文(91)

文章存档

2021年(1)

2020年(4)

2019年(4)

2018年(9)

2017年(11)

2016年(11)

2015年(6)

2014年(3)

2013年(28)

2012年(14)

分类: PHP

2017-07-10 18:23:27


点击(此处)折叠或打开

  1. <?php
  2. //守护进程化
  3. daemonize();

  4. //建立socket绑定
  5. if(($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) < 0)
  6. {
  7.         echo "failed to create socket: ".socket_strerror($sock)."\n";
  8.         exit();
  9. }
  10. $address = "192.168.84.72";
  11. $port = 9998;
  12. if(($ret = socket_bind($sock, $address, $port)) < 0)
  13. {
  14.         echo "failed to bind socket: ".socket_strerror($ret)."\n";
  15.         exit();
  16. }
  17. if( ( $ret = socket_listen( $sock, 0 ) ) < 0 )
  18. {
  19.         echo "failed to listen to socket: ".socket_strerror($ret)."\n";
  20.         exit();
  21. }
  22. socket_set_nonblock($sock);
  23. while (true)
  24. {
  25.         $conn = @socket_accept($sock);
  26.                 if( $conn === false ) {
  27.                         usleep(1000);
  28.                 } elseif( $conn > 0 ){

  29.                         $pid = pcntl_fork();
  30.                         //子进程
  31.                         if($pid == 0)
  32.                         {
  33.                                         //关闭socket监听
  34.                                         socket_close($sock);
  35.                                         include('redis.php');
  36.                                         socket_set_nonblock($conn);

  37.                                         //维护在线列表
  38.                                         $redis->sadd('online',(int)$conn);
  39.                                         while ($conn) {
  40.                                            //数据不为空进入队列
  41.                                            $recv = socket_read($conn, 1024);
  42.                                            if( !empty($recv) ) {
  43.                                                    $send_data=$recv;
  44.                                                    if($recv=="bye\r\n") {
  45.                                                                 socket_close($conn);
  46.                                                                 $redis->srem('oncline',(int)$conn);
  47.                                                                 exit(0);
  48.                                                    }
  49.                                                    $online = $redis->smembers('online');
  50.                                                    if(!empty($online)) {
  51.                                                         foreach($online as $one) {
  52.                                                                 if($one != (int)$conn){
  53.                                                                         $redis->lpush('chats_'.$one,$send_data);
  54.                                                                 }
  55.                                                         }
  56.                                                    }
  57.                                            }

  58.                                            //在队列中循环读消息
  59.                                            $msg = $redis->rpop('chats_'.(int)$conn);
  60.                                            if( !empty($msg) ) {
  61.                                                 socket_write($conn,$msg);
  62.                                            }
  63.                                            usleep(1000);
  64.                                         }
  65.                         }
  66.                         else
  67.                         {
  68.                                         //记录子进程id
  69.                                         $son_process_id[]=$pid;
  70.                                         socket_close($conn);
  71.                         }
  72.                 } else {
  73.             echo "error: ".socket_strerror($conn);
  74.             die;
  75.                 }
  76. }

  77. function daemonize()
  78. {
  79.         $pid = pcntl_fork();
  80.         if ($pid == -1)
  81.         {
  82.                 die("fork(1) failed!\n");
  83.         }
  84.         elseif ($pid > 0)
  85.         {
  86.                 //让由用户启动的进程退出
  87.                 exit(0);
  88.         }

  89.         //建立一个有别于终端的新session以脱离终端
  90.         posix_setsid();

  91.         $pid = pcntl_fork();
  92.         if ($pid == -1)
  93.         {
  94.                 die("fork(2) failed!\n");
  95.         }
  96.         elseif ($pid > 0)
  97.         {
  98.                 //父进程退出, 剩下子进程成为最终的独立进程
  99.                 exit(0);
  100.         }
  101. }

  102. ?>


点击(此处)折叠或打开

  1. <?php
  2. $ip = "127.0.0.1";
  3. $port = 6378;
  4. $redis = new Redis();
  5. $redis->pconnect($ip, $port);
  6. ?>


阅读(948) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~