Events.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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('gateway_test')->insertGetId(['msg'=>'onWorkerStart','datetime'=>date('Y-m-d H:i:s')]);
  33. }
  34. /**
  35. * 有消息时
  36. * @param int $client_id
  37. * @param mixed $message
  38. */
  39. public static function onMessage($client_id, $message)
  40. {
  41. //echo $message.PHP_EOL;
  42. //file_put_contents('1.json',$message.PHP_EOL,FILE_APPEND);
  43. //$info = Db::name('gateway_test')->insertGetId(['msg'=>'[msg]'.$message,'datetime'=>date('Y-m-d H:i:s')]);
  44. Gateway::sendToAll($client_id.'说:'.$message);
  45. return;
  46. /* $message = json_decode($message,true);
  47. if(is_array($message)){
  48. self::changestatus($message);
  49. }else{
  50. //echo 'default';
  51. }*/
  52. //return ;
  53. /*if(is_array($message) && isset($message['action']) && $message['action'] == 'changestatus'){
  54. self::changestatus($message);
  55. }else{
  56. echo 'default';
  57. }
  58. return ;*/
  59. }
  60. /**
  61. * 当客户端连接时触发
  62. * 如果业务不需此回调可以删除onConnect
  63. * @param int $client_id 连接id
  64. */
  65. public static function onConnect($client_id)
  66. {
  67. // 向当前client_id发送数据
  68. Gateway::sendToClient($client_id, "Hello $client_id");
  69. // 向所有人发送
  70. Gateway::sendToAll("$client_id login");
  71. }
  72. /*{
  73. 'action':'changestatus',
  74. "uid": "1",
  75. "type": "video",'audio'
  76. "value": "0",或‘1’
  77. "nowtime": "1234567890 ",
  78. "token": "edbf8a75-8e36-41f5-989f-7b3d067ccc83"
  79. }*/
  80. //修改用的通话状态
  81. private static function changestatus($message){
  82. //dump($message);
  83. if(isset($message['uid']) && isset($message['value'])){
  84. $redis = newredis();
  85. $redis->set('gg_matching_uid_'.$message['uid'], $message['value'], null);
  86. }
  87. return;
  88. }
  89. //聊天扣钱
  90. private static function userchat(){
  91. $price = config('site.typing_min_price');
  92. //扣费
  93. Db::startTrans();
  94. if(isset($message['uid'])){
  95. $rs = model('wallet')->lockChangeAccountRemain($message['uid'],'gold',-$price,13,'','',0);
  96. if($rs['status'] === false){
  97. Db::rollback();
  98. }
  99. }
  100. Db::commit();
  101. }
  102. /**
  103. * 当客户端断开连接时
  104. * @param integer $client_id 客户端id
  105. */
  106. public static function onClose($client_id)
  107. {
  108. // debug
  109. /* echo "client:{$_SERVER['REMOTE_ADDR']}:{$_SERVER['REMOTE_PORT']} gateway:{$_SERVER['GATEWAY_ADDR']}:{$_SERVER['GATEWAY_PORT']} client_id:$client_id onClose:''\n";
  110. // 从房间的客户端列表中删除
  111. if(isset($_SESSION['room_id']))
  112. {
  113. $room_id = $_SESSION['room_id'];
  114. $new_message = array('type'=>'logout', 'from_client_id'=>$client_id, 'from_client_name'=>$_SESSION['client_name'], 'time'=>date('Y-m-d H:i:s'));
  115. Gateway::sendToGroup($room_id, json_encode($new_message));
  116. }*/
  117. }
  118. public static function onWorkerStop(){
  119. //$info = Db::name('gateway_test')->insertGetId(['msg'=>'onWorkerStop','datetime'=>date('Y-m-d H:i:s')]);
  120. }
  121. }