start_io.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. use Workerman\Worker;
  3. use Workerman\Timer;
  4. use PHPSocketIO\SocketIO;
  5. use Workerman\Protocols\Http\Request;
  6. use Workerman\Connection\TcpConnection;
  7. include __DIR__ . '/vendor/autoload.php';
  8. // 全局数组保存uid在线数据
  9. $uidConnectionMap = array();
  10. // 记录最后一次广播的在线用户数
  11. $last_online_count = 0;
  12. // 记录最后一次广播的在线页面数
  13. $last_online_page_count = 0;
  14. // PHPSocketIO服务
  15. $sender_io = new SocketIO(2120);
  16. // 客户端发起连接事件时,设置连接socket的各种事件回调
  17. $sender_io->on('connection', function($socket){
  18. // 当客户端发来登录事件时触发
  19. $socket->on('login', function ($uid)use($socket){
  20. global $uidConnectionMap, $last_online_count, $last_online_page_count;
  21. // 已经登录过了
  22. if(isset($socket->uid)){
  23. return;
  24. }
  25. // 更新对应uid的在线数据
  26. $uid = (string)$uid;
  27. if(!isset($uidConnectionMap[$uid]))
  28. {
  29. $uidConnectionMap[$uid] = 0;
  30. }
  31. // 这个uid有++$uidConnectionMap[$uid]个socket连接
  32. ++$uidConnectionMap[$uid];
  33. // 将这个连接加入到uid分组,方便针对uid推送数据
  34. $socket->join($uid);
  35. $socket->uid = $uid;
  36. // 更新这个socket对应页面的在线数据
  37. $socket->emit('update_online_count', "当前<b>{$last_online_count}</b>人在线,共打开<b>{$last_online_page_count}</b>个页面");
  38. });
  39. // 当客户端断开连接是触发(一般是关闭网页或者跳转刷新导致)
  40. $socket->on('disconnect', function () use($socket) {
  41. if(!isset($socket->uid))
  42. {
  43. return;
  44. }
  45. global $uidConnectionMap, $sender_io;
  46. // 将uid的在线socket数减一
  47. if(--$uidConnectionMap[$socket->uid] <= 0)
  48. {
  49. unset($uidConnectionMap[$socket->uid]);
  50. }
  51. });
  52. });
  53. // 当$sender_io启动后监听一个http端口,通过这个端口可以给任意uid或者所有uid推送数据
  54. $sender_io->on('workerStart', function(){
  55. // 监听一个http端口
  56. $inner_http_worker = new Worker('http://0.0.0.0:2121');
  57. // 当http客户端发来数据时触发
  58. $inner_http_worker->onMessage = function(TcpConnection $http_connection, Request $request){
  59. global $uidConnectionMap;
  60. $post = $request->post();
  61. $post = $post ? $post : $request->get();
  62. // 推送数据的url格式 type=publish&to=uid&content=xxxx
  63. switch(@$post['type']){
  64. case 'publish':
  65. $type = 'new_msg';
  66. break;
  67. case 'refreshSylb':
  68. $type = 'refreshSylb';
  69. break;
  70. default:
  71. return $http_connection->send('fail');
  72. }
  73. global $sender_io;
  74. $to = @$post['to'];
  75. $post['content'] = htmlspecialchars(@$post['content']);
  76. // 有指定uid则向uid所在socket组发送数据
  77. if($to){
  78. $sender_io->to($to)->emit($type, $post['content']);
  79. // 否则向所有uid推送数据
  80. }else{
  81. $sender_io->emit($type, @$post['content']);
  82. }
  83. // http接口返回,如果用户离线socket返回fail
  84. if($to && !isset($uidConnectionMap[$to])){
  85. return $http_connection->send('offline');
  86. }else{
  87. return $http_connection->send('ok');
  88. }
  89. };
  90. // 执行监听
  91. $inner_http_worker->listen();
  92. // 一个定时器,定时向所有uid推送当前uid在线数及在线页面数
  93. Timer::add(10, function(){
  94. global $uidConnectionMap, $sender_io, $last_online_count, $last_online_page_count;
  95. $online_count_now = count($uidConnectionMap);
  96. $online_page_count_now = array_sum($uidConnectionMap);
  97. // 只有在客户端在线数变化了才广播,减少不必要的客户端通讯
  98. if($last_online_count != $online_count_now || $last_online_page_count != $online_page_count_now)
  99. {
  100. $sender_io->emit('update_online_count', "当前<b>{$online_count_now}</b>人在线,共打开<b>{$online_page_count_now}</b>个页面");
  101. $last_online_count = $online_count_now;
  102. $last_online_page_count = $online_page_count_now;
  103. }
  104. });
  105. });
  106. if(!defined('GLOBAL_START'))
  107. {
  108. Worker::runAll();
  109. }