* @copyright walkor * @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); /** * 聊天主逻辑 * 主要是处理 onMessage onClose */ //use \GatewayWorker\Lib\Gateway; //use \GatewayWorker\Lib\DbConnection; use think\Db; use think\App; class Events { public static function onWorkerStart(){ App::initCommon(); /*$info = Db::name('admin')->find(1); dump($info);*/ } /** * 有消息时 * @param int $client_id * @param mixed $message */ public static function onMessage($client_id, $message) { /*echo $message.PHP_EOL; file_put_contents('1.json',$message.PHP_EOL,FILE_APPEND); return;*/ $message = json_decode($message,true); if(is_array($message)){ self::changestatus($message); }else{ //echo 'default'; } //return ; /*if(is_array($message) && isset($message['action']) && $message['action'] == 'changestatus'){ self::changestatus($message); }else{ echo 'default'; } return ;*/ } /** * 当客户端连接时触发 * 如果业务不需此回调可以删除onConnect * @param int $client_id 连接id */ /*public static function onConnect($client_id) { // 向当前client_id发送数据 Gateway::sendToClient($client_id, "Hello $client_id"); // 向所有人发送 Gateway::sendToAll("$client_id login"); }*/ /*{ 'action':'changestatus', "uid": "1", "type": "video",'audio' "value": "0",或‘1’ "nowtime": "1234567890 ", "token": "edbf8a75-8e36-41f5-989f-7b3d067ccc83" }*/ //修改用的通话状态 private static function changestatus($message){ //dump($message); if(isset($message['uid']) && isset($message['value'])){ redis_set($message['uid'],$message['value']); } return; } //聊天扣钱 private static function userchat(){ $price = config('site.typing_min_price'); //扣费 Db::startTrans(); if(isset($message['uid'])){ $rs = model('wallet')->lockChangeAccountRemain($message['uid'],'gold',-$price,13,'','',0); if($rs['status'] === false){ Db::rollback(); } } Db::commit(); } /** * 当客户端断开连接时 * @param integer $client_id 客户端id */ public static function onClose($client_id) { // debug /* echo "client:{$_SERVER['REMOTE_ADDR']}:{$_SERVER['REMOTE_PORT']} gateway:{$_SERVER['GATEWAY_ADDR']}:{$_SERVER['GATEWAY_PORT']} client_id:$client_id onClose:''\n"; // 从房间的客户端列表中删除 if(isset($_SESSION['room_id'])) { $room_id = $_SESSION['room_id']; $new_message = array('type'=>'logout', 'from_client_id'=>$client_id, 'from_client_name'=>$_SESSION['client_name'], 'time'=>date('Y-m-d H:i:s')); Gateway::sendToGroup($room_id, json_encode($new_message)); }*/ } }