Events.php 4.3 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. use \GatewayWorker\Lib\Gateway;
  21. use Workerman\Lib\Timer;
  22. // define('IN_WEB', true);
  23. define('WORKERNAME', 'Chat');
  24. // $sid =1;
  25. // $lid =1;
  26. // define('WWWROOT', dirname(__FILE__).'/../../');
  27. // require_once WWWROOT . 'core.php';
  28. // define( 'PATH_CFG', WWWROOT . 'config/'.$config['sidlist'][1].$isTset.'/');
  29. // require_once PATH_CFG . 'config.inc.php';
  30. // oo::setConfig( $config); //设置全局变量
  31. // oo::functions()->header();
  32. /**
  33. * 主逻辑
  34. * 主要是处理 onConnect onMessage onClose 三个方法
  35. * onConnect 和 onClose 如果不需要可以不用实现并删除
  36. */
  37. class Events
  38. {
  39. public static function onWorkerStart(){
  40. global $dbs;
  41. global $rediss;
  42. $dbs = oo::commonOprDb('common');
  43. $dbs->connect();
  44. $redisArr =['common','statistics'];
  45. foreach ($redisArr as $v){
  46. $rediss[$v] = oo::commonOprRedis($v);
  47. }
  48. $time_id = Timer::add(15, function () use (&$time_id,$redisArr) {
  49. global $dbs;
  50. global $rediss;
  51. if(!$dbs->ping()){//失败释放内存,重连
  52. echo date('Y-m-d H:i:s').'close-mysql'."\r\n";
  53. $dbs->close();
  54. oo::$objDb =[];
  55. $dbs = oo::commonOprDb('common');
  56. }
  57. foreach ($redisArr as $v){
  58. if(!$rediss[$v]->ping()){//失败释放内存,重连
  59. echo date('Y-m-d H:i:s').'close-redis'."\r\n";
  60. $rediss[$v]->close();
  61. oo::$objRedis =[];
  62. $rediss[$v] = oo::commonOprRedis('common');
  63. }
  64. }
  65. });
  66. }
  67. /**
  68. * 当客户端连接时触发
  69. * 如果业务不需此回调可以删除onConnect
  70. * @param int $client_id 连接id
  71. */
  72. public static function onConnect($client_id){
  73. $time_id = Timer::add(5, function () use ($client_id , &$time_id) {
  74. if(empty(Gateway::getSession($client_id))){
  75. Timer::del($time_id);
  76. Gateway::closeClient($client_id);
  77. }
  78. });
  79. }
  80. /**
  81. * 当客户端发来消息时触发
  82. * @param int $client_id 连接id
  83. * @param mixed $message 具体消息
  84. */
  85. public static function onMessage($client_id, $message){
  86. json_decode($message);
  87. if(json_last_error() == JSON_ERROR_NONE){
  88. $res =json_decode($message,true);
  89. $res['encryp'] = 0;
  90. }else{
  91. $res = ProtocolsEvent::wsDecode($message);
  92. $res['encryp'] = 1;
  93. }
  94. if($res['cmd'] == 0x2008){
  95. $str = json_encode(['cmd'=>0x600d,'code'=>1]);
  96. if($res['encryp']){
  97. $str = ProtocolsEvent::encode($str);
  98. }
  99. Gateway::sendToClient($client_id, $str);
  100. return;
  101. }
  102. if(in_array($res['cmd'],array_keys(ocmd::$Cmd))){
  103. $res['cmd'] = ocmd::$Cmd[$res['cmd']];
  104. $session = Gateway::getSession($client_id);
  105. if(isset($session[$client_id]['uid'])){
  106. $res['uid'] = intval($session[$client_id]['uid']);
  107. }
  108. $action = explode('_',$res['cmd']);
  109. self::CrazyGodEvents(ucfirst($action[0]),ucfirst($action[1]),$client_id,$res);
  110. }
  111. }
  112. /**
  113. * 选择业务类
  114. * @param $mod
  115. * @param $func
  116. * @param $client_id
  117. * @param mixed $data
  118. */
  119. public static function CrazyGodEvents($mod,$func, $client_id, $data = ''){
  120. require_once __DIR__ . '/../Function/' .$mod.'.php';
  121. $mod = $mod.'CrazyWorker';
  122. $obj = new $mod(isset($data['sid'])?$data['sid']:1,$client_id);
  123. $obj->$func($client_id,$data);
  124. }
  125. }