Events.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. $message = json_decode($message,true);
  43. if($message['action'] == 'changestatus'){
  44. self::changestatus($message);
  45. }else{
  46. echo 'default';
  47. dump($message);
  48. }
  49. return ;
  50. }
  51. /*{
  52. 'action':'changestatus',
  53. "uid": "1",
  54. "type": "video",'audio'
  55. "value": "0",或‘1’
  56. "nowtime": "1234567890 ",
  57. "token": "edbf8a75-8e36-41f5-989f-7b3d067ccc83"
  58. }*/
  59. private static function changestatus($message){
  60. //dump($message);
  61. if(isset($message['uid']) && isset($message['value'])){
  62. redis_set($message['uid'],$message['value']);
  63. }
  64. return;
  65. }
  66. /**
  67. * 当客户端断开连接时
  68. * @param integer $client_id 客户端id
  69. */
  70. public static function onClose($client_id)
  71. {
  72. // debug
  73. /* echo "client:{$_SERVER['REMOTE_ADDR']}:{$_SERVER['REMOTE_PORT']} gateway:{$_SERVER['GATEWAY_ADDR']}:{$_SERVER['GATEWAY_PORT']} client_id:$client_id onClose:''\n";
  74. // 从房间的客户端列表中删除
  75. if(isset($_SESSION['room_id']))
  76. {
  77. $room_id = $_SESSION['room_id'];
  78. $new_message = array('type'=>'logout', 'from_client_id'=>$client_id, 'from_client_name'=>$_SESSION['client_name'], 'time'=>date('Y-m-d H:i:s'));
  79. Gateway::sendToGroup($room_id, json_encode($new_message));
  80. }*/
  81. }
  82. }