Events.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. /**
  21. * 聊天主逻辑
  22. * 主要是处理 onMessage onClose
  23. */
  24. //use \GatewayWorker\Lib\Gateway;
  25. //use \GatewayWorker\Lib\DbConnection;
  26. use think\Db;
  27. use think\App;
  28. class Events
  29. {
  30. public static function onWorkerStart(){
  31. App::initCommon();
  32. /*$info = Db::name('admin')->find(1);
  33. dump($info);*/
  34. }
  35. /**
  36. * 有消息时
  37. * @param int $client_id
  38. * @param mixed $message
  39. */
  40. public static function onMessage($client_id, $message)
  41. {
  42. /*echo $message.PHP_EOL;
  43. file_put_contents('1.json',$message.PHP_EOL,FILE_APPEND);
  44. return;*/
  45. $message = json_decode($message,true);
  46. if(is_array($message)){
  47. self::changestatus($message);
  48. }else{
  49. echo 'default';
  50. }
  51. return ;
  52. /*if(is_array($message) && isset($message['action']) && $message['action'] == 'changestatus'){
  53. self::changestatus($message);
  54. }else{
  55. echo 'default';
  56. }
  57. return ;*/
  58. }
  59. /*{
  60. 'action':'changestatus',
  61. "uid": "1",
  62. "type": "video",'audio'
  63. "value": "0",或‘1’
  64. "nowtime": "1234567890 ",
  65. "token": "edbf8a75-8e36-41f5-989f-7b3d067ccc83"
  66. }*/
  67. private static function changestatus($message){
  68. //dump($message);
  69. if(isset($message['uid']) && isset($message['value'])){
  70. redis_set($message['uid'],$message['value']);
  71. }
  72. return;
  73. }
  74. /**
  75. * 当客户端断开连接时
  76. * @param integer $client_id 客户端id
  77. */
  78. public static function onClose($client_id)
  79. {
  80. // debug
  81. /* echo "client:{$_SERVER['REMOTE_ADDR']}:{$_SERVER['REMOTE_PORT']} gateway:{$_SERVER['GATEWAY_ADDR']}:{$_SERVER['GATEWAY_PORT']} client_id:$client_id onClose:''\n";
  82. // 从房间的客户端列表中删除
  83. if(isset($_SESSION['room_id']))
  84. {
  85. $room_id = $_SESSION['room_id'];
  86. $new_message = array('type'=>'logout', 'from_client_id'=>$client_id, 'from_client_name'=>$_SESSION['client_name'], 'time'=>date('Y-m-d H:i:s'));
  87. Gateway::sendToGroup($room_id, json_encode($new_message));
  88. }*/
  89. }
  90. }