\app\push\controller\KefuHandler::class, 'live' => \app\push\controller\Push::class ]; /** * 当客户端发来消息时触发 * @param int $client_id 连接id * @param mixed $message 具体消息 */ public static function onMessage($client_id, $message) { $message_data = json_decode($message, true); if (!$message_data) return; try { if (!isset($message_data['type'])) throw new \Exception('缺少消息参数类型'); //消息回調处理 $channel = isset($message_data['channel']) ? $message_data['channel'] : 'live'; $className = self::$eventClassName[$channel]; $evevtName = $className . '::instance'; if (is_callable($evevtName)) $evevtName()->start($message_data['type'], $client_id, $message_data); else throw new \Exception('消息处理回调不存在。[' + $evevtName + ']'); } catch (\Exception $e) { var_dump([ 'file' => $e->getFile(), 'code' => $e->getCode(), 'msg' => $e->getMessage(), 'line' => $e->getLine() ]); } } /** * 当用户连接时触发的方法 * @param integer $client_id 连接的客户端 * @return void */ public static function onConnect($client_id) { Gateway::sendToClient($client_id, json_encode(array( 'type' => 'init', 'client_id' => $client_id ))); } /**用户进入时连接 * @param $client_id * @param $data */ public static function onWebSocketConnect($client_id, $data) { $channel = array_key_exists('channel', $data['get']) ? $data['get']['channel'] : ""; if ($channel === "kefu") { // 客服 // Log::write($kefuId); } else { // live Gateway::bindUid($client_id, $data['get']['uid']); LiveUser::setLiveUserOnline($data['get']['room'], $data['get']['uid'], 1); } } /** * 当用户断开连接时触发的方法 * @param integer $client_id 断开连接的客户端 * @return void */ public static function onClose($client_id) { if (isset($_SESSION['is_kefu']) && isset($_SESSION['uid'])) { $is_kefu = $_SESSION['is_kefu']; $uid = $_SESSION['uid']; if ($is_kefu) { KefuUserRecordModel::update([ 'online' => 0 ], [ 'is_kefu_send' => 0, 'to_uid' => $uid ]); KefuModel::update([ 'online' => 0 ], [ 'id' => $uid ]); } else { KefuUserRecordModel::update([ 'online' => 0 ], [ 'is_kefu_send' => 1, 'to_uid' => $uid ]); if (isset($_SESSION['chat_kefu_uid']) && count($_SESSION['chat_kefu_uid'])) { Gateway::sendToUid($_SESSION['chat_kefu_uid'], json_encode([ 'type' => 'offline', 'data' => [ 'self' => false, 'uid' => $uid ] ])); } } } Gateway::sendToClient($client_id, json_encode([ 'type' => 'logout', 'message' => "client[$client_id]" ])); } /** * 当进程启动时 * @param integer $businessWorker 进程实例 */ public static function onWorkerStart($worker) { //在进程1上开启定时器 每self::$interval秒执行 self::$worker = $worker; if ($worker->id === 0) { $last = time(); $task = [6 => $last, 10 => $last, 30 => $last, 60 => $last, 180 => $last, 300 => $last]; self::$timer = Timer::add(self::$interval, function () use (&$task) { try { $now = time(); Hook::exec(self::$evevtRunClass); foreach ($task as $sec => &$time) { if (($now - $time) >= $sec) { $time = $now; Hook::exec(self::$evevtRunClass, 'task_' . $sec); } } } catch (\Throwable $e) { } }); } } /** * 当进程关闭时 * @param integer $businessWorker 进程实例 */ public static function onWorkerStop($worker) { if ($worker->id === 0) Timer::del(self::$timer); } }