Authored by xiaofeng.yao@yoho.cn

推流增加心跳保活接口

... ... @@ -134,4 +134,13 @@ class Room extends \yii\db\ActiveRecord
{
return $this->hasOne(RoomQchannel::className(), ['room_id'=>'room_id']);
}
/**
* 关联表-房间附属信息
* @return type
*/
public function getRoomInfo()
{
return $this->hasOne(RoomInfo::className(), ['room_id'=>'room_id']);
}
}
... ...
<?php
namespace common\models;
use Yii;
/**
* This is the model class for table "{{%room_info}}".
*
* @property integer $room_id
* @property integer $last_time
*/
class RoomInfo extends \yii\db\ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return '{{%room_info}}';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['room_id'], 'required'],
[['room_id', 'last_time'], 'integer'],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'room_id' => 'Room ID',
'last_time' => 'Last Time',
];
}
}
... ...
... ... @@ -78,38 +78,33 @@ class RoomController extends Controller
->all();
if ($result) {
$TIME_MAX = 30 * 60;
$flag = false;
foreach ($result as $room) {
if ($room->roomQchannel) {
$channel_id = (string)$room->roomQchannel->channel_id;
$ret = QcloudApiClient::self()->DescribeLVBChannel($channel_id);
if (!$ret) {
$this->_log(QcloudApiClient::self()->error());
return;
}
//腾讯云返回不是直播中的置为直播结束
if ($ret['channelInfo'][0]['channel_status'] != self::QCLOUD_LVING_STAT) {
/*$room->setAttributes([
'living' => Params::LIVE_END,
'unstart_sort' => 0,
'living_sort' => 0
]);
if ($room->save(false)) {
$this->_log("room[{$room->room_id}] closed success");
} else {
$this->_log("room[{$room->room_id}] closed faild");
}*/
//是否要调用统一的结束接口,如果掉用的话存在资讯抛异常问题,那么房间就一直结束不了
try {
//当前时间 > 最后一次存活时间+阈值
if (!$room->roomInfo || (time() < ($room->roomInfo->last_time + $TIME_MAX))) {
continue;
}
try {
if ($room->roomQchannel) {
$channel_id = (string)$room->roomQchannel->channel_id;
$ret = QcloudApiClient::self()->DescribeLVBChannel($channel_id);
if (!$ret) {
$this->_log(QcloudApiClient::self()->error());
continue;
}
//腾讯云返回不是直播中的置为直播结束
if ($ret['channelInfo'][0]['channel_status'] != self::QCLOUD_LVING_STAT) {
$video_res = (new Video())->setVideoStop($room->room_id);
if($video_res){
if ($video_res) {
$this->_log("room[{$room->room_id}] closed success");
}else{
} else {
$this->_log("room[{$room->room_id}] closed faild");
}
} catch (\Exception $e) {
$this->_log("room[{$room->room_id}] exception:".$e->getMessage());
}
}
} catch (\Exception $e) {
$this->_log("room[{$room->room_id}] exception:" . $e->getMessage());
}
}
}
... ...
... ... @@ -6,6 +6,7 @@ use soa\controllers\BaseController;
use common\models\RoomNums;
use common\models\Video;
use common\models\Room;
use common\models\RoomInfo;
/**
* Room controller
... ... @@ -351,6 +352,45 @@ class RoomController extends BaseController
}
/**
* 推流端隔一分钟上报,证明存活
*/
public function actionKeeplive()
{
$requests = $this->requests();
$room_id = $requests['room_id'];
do{
$retArr = [];
if(!$room_id){
$retArr = [
'code' => Yii::$app->params['failed_code'],
'messsage' => '缺少room_id'
];
break;
}
//更新房间的最后一次访问时间(活跃时间)
$res = Yii::$app->db->createCommand('
INSERT INTO {{%room_info}} (`room_id`,`last_time`)
VALUES (:room_id,:last_time)
ON DUPLICATE KEY UPDATE `last_time`=:last_time')
->bindValue(':room_id', $room_id)
->bindValue(':last_time', time())
->execute();
if($res){
$retArr = [
'code' => Yii::$app->params['success_code'],
'messsage' => '更新成功'
];
}else{
$retArr = [
'code' => Yii::$app->params['failed_code'],
'messsage' => '更新失败'
];
}
}while(false);
$this->renderJson($retArr['code'], $retArr['messsage']);
}
/**
* 获取评论表名称
* @param $room_id
* @return string
... ...