Events.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. * 当客户端连接时触发
  61. * 如果业务不需此回调可以删除onConnect
  62. * @param int $client_id 连接id
  63. */
  64. /*public static function onConnect($client_id)
  65. {
  66. // 向当前client_id发送数据
  67. Gateway::sendToClient($client_id, "Hello $client_id");
  68. // 向所有人发送
  69. Gateway::sendToAll("$client_id login");
  70. }*/
  71. /*{
  72. 'action':'changestatus',
  73. "uid": "1",
  74. "type": "video",'audio'
  75. "value": "0",或‘1’
  76. "nowtime": "1234567890 ",
  77. "token": "edbf8a75-8e36-41f5-989f-7b3d067ccc83"
  78. }*/
  79. //修改用的通话状态
  80. private static function changestatus($message){
  81. //dump($message);
  82. if(isset($message['uid']) && isset($message['value'])){
  83. redis_set($message['uid'],$message['value']);
  84. }
  85. return;
  86. }
  87. //聊天扣钱
  88. private static function userchat(){
  89. $price = config('site.typing_min_price');
  90. //扣费
  91. Db::startTrans();
  92. if(isset($message['uid'])){
  93. $rs = model('wallet')->lockChangeAccountRemain($message['uid'],'gold',-$price,13,'','',0);
  94. if($rs['status'] === false){
  95. Db::rollback();
  96. }
  97. }
  98. Db::commit();
  99. }
  100. /**
  101. * 当客户端断开连接时
  102. * @param integer $client_id 客户端id
  103. */
  104. public static function onClose($client_id)
  105. {
  106. // debug
  107. /* echo "client:{$_SERVER['REMOTE_ADDR']}:{$_SERVER['REMOTE_PORT']} gateway:{$_SERVER['GATEWAY_ADDR']}:{$_SERVER['GATEWAY_PORT']} client_id:$client_id onClose:''\n";
  108. // 从房间的客户端列表中删除
  109. if(isset($_SESSION['room_id']))
  110. {
  111. $room_id = $_SESSION['room_id'];
  112. $new_message = array('type'=>'logout', 'from_client_id'=>$client_id, 'from_client_name'=>$_SESSION['client_name'], 'time'=>date('Y-m-d H:i:s'));
  113. Gateway::sendToGroup($room_id, json_encode($new_message));
  114. }*/
  115. }
  116. }