Events.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /**
  3. * This file is part of workerman.
  4. *
  5. * Licensed under The MIT License
  6. * For full copyright and license information, please see the MIT-LICENSE.txt
  7. * Redistributions of files must retain the above copyright notice.
  8. *
  9. * @author walkor<walkor@workerman.net>
  10. * @copyright walkor<walkor@workerman.net>
  11. * @link http://www.workerman.net/
  12. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  13. */
  14. /**
  15. * 用于检测业务代码死循环或者长时间阻塞等问题
  16. * 如果发现业务卡死,可以将下面declare打开(去掉//注释),并执行php start.php reload
  17. * 然后观察一段时间workerman.log看是否有process_timeout异常
  18. */
  19. //declare(ticks=1);
  20. use \GatewayWorker\Lib\Gateway;
  21. /**
  22. * 主逻辑
  23. * 主要是处理 onConnect onMessage onClose 三个方法
  24. * onConnect 和 onClose 如果不需要可以不用实现并删除
  25. */
  26. class Events
  27. {
  28. /**
  29. * 当客户端连接时触发
  30. * 如果业务不需此回调可以删除onConnect
  31. *
  32. * @param int $client_id 连接id
  33. */
  34. public static function onConnect($client_id)
  35. {
  36. global $num;
  37. // 向当前client_id发送数据
  38. //Gateway::sendToClient($client_id, "Hello $client_id\r\n");
  39. // 向所有人发送
  40. // Gateway::sendToAll("$client_id login\r\n");
  41. echo "connect".++$num.":".$client_id."\n";
  42. // print_r("connect".++$num.":".$client_id."\n";);
  43. Gateway::sendToClient($client_id,json_encode([
  44. 'state'=>'init',
  45. 'client_id'=>$client_id
  46. ]));
  47. }
  48. /**
  49. * 当客户端发来消息时触发
  50. * @param int $client_id 连接id
  51. * @param mixed $message 具体消息
  52. */
  53. public static function onMessage($client_id, $message)
  54. {
  55. // 向所有人发送
  56. $message_data = json_decode($message,true);
  57. if(!$message_data){
  58. return;
  59. }
  60. switch($message_data['state']){
  61. case "bind":
  62. $fromid = $message_data['fromid'];
  63. Gateway::bindUid($client_id, $fromid);
  64. return;
  65. case "save":
  66. $text = nl2br(htmlspecialchars($message_data['data']));
  67. $toid = $message_data['toid'];
  68. $type = $message_data['type'];
  69. $date=[
  70. 'state'=>'new_content',
  71. 'type'=>$type,
  72. 'data'=>$text,
  73. 'time'=>time()
  74. ];
  75. if(Gateway::isUidOnline($toid)){
  76. $date['isread']= 1;
  77. Gateway::sendToUid($toid, json_encode($date));
  78. }else{
  79. $date['isread']=0;
  80. }
  81. $date['state']="save";
  82. Gateway::sendToUid($fromid,json_encode($date));
  83. // Gateway::sendToAll(json_encode($date));
  84. return;
  85. // case "say_img":
  86. // $toid = $message_data['toid'];
  87. // $fromid =$message_data['fromid'];
  88. // $img_name = $message_data['data'];
  89. // $date=[
  90. // 'type'=>'say_img',
  91. // 'fromid'=>$fromid,
  92. // 'toid'=>$toid,
  93. // 'img_name'=>$img_name
  94. // ];
  95. // Gateway::sendToUid($toid,json_encode($date));
  96. // return;
  97. case "online":
  98. $toid = $message_data['toid'];
  99. $fromid = $message_data['fromid'];
  100. $status = Gateway::isUidOnline($toid);
  101. Gateway::sendToUid($fromid,json_encode(['state'=>"online","status"=>$status]));
  102. return;
  103. case "online_duihua":
  104. $toid = $message_data['toid'];
  105. $fromid = $message_data['fromid'];
  106. $status = Gateway::isUidOnline($toid);
  107. Gateway::sendToUid($fromid,json_encode(['state'=>"online","status"=>$status]));
  108. return;
  109. }
  110. }
  111. /**
  112. * 当用户断开连接时触发
  113. * @param int $client_id 连接id
  114. */
  115. public static function onClose($client_id)
  116. {
  117. // 向所有人发送
  118. // GateWay::sendToAll("$client_id logout\r\n");
  119. }
  120. }