123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- <?php
- /**
- * This file is part of workerman.
- *
- * Licensed under The MIT License
- * For full copyright and license information, please see the MIT-LICENSE.txt
- * Redistributions of files must retain the above copyright notice.
- *
- * @author walkor<walkor@workerman.net>
- * @copyright walkor<walkor@workerman.net>
- * @link http://www.workerman.net/
- * @license http://www.opensource.org/licenses/mit-license.php MIT License
- */
- /**
- * 用于检测业务代码死循环或者长时间阻塞等问题
- * 如果发现业务卡死,可以将下面declare打开(去掉//注释),并执行php start.php reload
- * 然后观察一段时间workerman.log看是否有process_timeout异常
- */
- declare(ticks=1);
- use \GatewayWorker\Lib\Gateway;
- use Workerman\Lib\Timer;
- // define('IN_WEB', true);
- define('WORKERNAME', 'Chat');
- // $sid =1;
- // $lid =1;
- // define('WWWROOT', dirname(__FILE__).'/../../');
- // require_once WWWROOT . 'core.php';
- // define( 'PATH_CFG', WWWROOT . 'config/'.$config['sidlist'][1].$isTset.'/');
- // require_once PATH_CFG . 'config.inc.php';
- // oo::setConfig( $config); //设置全局变量
- // oo::functions()->header();
- /**
- * 主逻辑
- * 主要是处理 onConnect onMessage onClose 三个方法
- * onConnect 和 onClose 如果不需要可以不用实现并删除
- */
- class Events
- {
- public static function onWorkerStart(){
- global $dbs;
- global $rediss;
- $dbs = oo::commonOprDb('common');
- $dbs->connect();
- $redisArr =['common','statistics'];
- foreach ($redisArr as $v){
- $rediss[$v] = oo::commonOprRedis($v);
- }
- $time_id = Timer::add(15, function () use (&$time_id,$redisArr) {
- global $dbs;
- global $rediss;
- if(!$dbs->ping()){//失败释放内存,重连
- echo date('Y-m-d H:i:s').'close-mysql'."\r\n";
- $dbs->close();
- oo::$objDb =[];
- $dbs = oo::commonOprDb('common');
- }
- foreach ($redisArr as $v){
- if(!$rediss[$v]->ping()){//失败释放内存,重连
- echo date('Y-m-d H:i:s').'close-redis'."\r\n";
- $rediss[$v]->close();
- oo::$objRedis =[];
- $rediss[$v] = oo::commonOprRedis('common');
- }
- }
- });
- }
- /**
- * 当客户端连接时触发
- * 如果业务不需此回调可以删除onConnect
- * @param int $client_id 连接id
- */
- public static function onConnect($client_id){
- $time_id = Timer::add(5, function () use ($client_id , &$time_id) {
- if(empty(Gateway::getSession($client_id))){
- Timer::del($time_id);
- Gateway::closeClient($client_id);
- }
- });
- }
- /**
- * 当客户端发来消息时触发
- * @param int $client_id 连接id
- * @param mixed $message 具体消息
- */
- public static function onMessage($client_id, $message){
- json_decode($message);
- if(json_last_error() == JSON_ERROR_NONE){
- $res =json_decode($message,true);
- $res['encryp'] = 0;
- }else{
- $res = ProtocolsEvent::wsDecode($message);
- $res['encryp'] = 1;
- }
-
- if($res['cmd'] == 0x2008){
- $str = json_encode(['cmd'=>0x600d,'code'=>1]);
- if($res['encryp']){
- $str = ProtocolsEvent::encode($str);
- }
- Gateway::sendToClient($client_id, $str);
- return;
- }
-
- if(in_array($res['cmd'],array_keys(ocmd::$Cmd))){
- $res['cmd'] = ocmd::$Cmd[$res['cmd']];
- $session = Gateway::getSession($client_id);
- if(isset($session[$client_id]['uid'])){
- $res['uid'] = intval($session[$client_id]['uid']);
- }
- $action = explode('_',$res['cmd']);
- self::CrazyGodEvents(ucfirst($action[0]),ucfirst($action[1]),$client_id,$res);
- }
- }
- /**
- * 选择业务类
- * @param $mod
- * @param $func
- * @param $client_id
- * @param mixed $data
- */
- public static function CrazyGodEvents($mod,$func, $client_id, $data = ''){
- require_once __DIR__ . '/../Function/' .$mod.'.php';
- $mod = $mod.'CrazyWorker';
- $obj = new $mod(isset($data['sid'])?$data['sid']:1,$client_id);
- $obj->$func($client_id,$data);
- }
- }
|