版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yun_bao_2144899870/article/details/88392304
网络视频直播系统中的礼物功能是直播软件开发者们最喜欢的功能之一,因为这既是直播平台获利的手段之一,又是直播反馈链中的一环,那么在开发直播app软件时,如何为直播系统实现送礼物功能呢?我们一起来看看吧。
public function sendGift($uid,$liveuid,$stream,$giftid,$giftcount) {
$uid//送礼用户的id
$liveuid //主播的id
$stream //直播间的流,主要用来确定直播间防止送错主播礼物
$giftid // 礼物的id
$giftcount // 礼物的数量
/* 礼物信息 */
$giftinfo=DI()->notorm->gift //首先获取礼物的信息
->select("type,mark,giftname,gifticon,needcoin,swftype,swf,swftime")
->where('id=?',$giftid)
->fetchOne();
if(!$giftinfo){ //判断一下礼物是否存在
/* 礼物信息不存在 */
return 1002;
}
$total= $giftinfo['needcoin']*$giftcount; //礼物的价格*礼物的数量计算出需要花费的金额
$addtime=time();
$type='expend'; //支出的标识
$action='sendgift'; //消费的标识
/* 更新用户余额 消费 */
$ifok =DI()->notorm->users //送出礼物的用户进行金额扣费
->where('id = ? and coin >=?', $uid,$total)
->update(array('coin' => new NotORM_Literal("coin - {$total}"),'consumption' => new NotORM_Literal("consumption + {$total}") ) );
if(!$ifok){//网络视频直播系统自动判断扣费是否成功,从而判断用户的余额是否足够
/* 余额不足 */
return 1001;
}
/* 更新直播 魅力值 累计魅力值 */
$istouid =DI()->notorm->users //主播收到礼物会会增加魅力值
->where('id = ?', $liveuid)
->update( array('votes' => new NotORM_Literal("votes + {$anthor_total}"),'votestotal' => new NotORM_Literal("votestotal + {$total}") ));
$stream2=explode('_',$stream);
$showid=$stream2[1];
$insert=array("type"=>$type,"action"=>$action,"uid"=>$uid,"touid"=>$liveuid,"giftid"=>$giftid,"giftcount"=>$giftcount,"totalcoin"=>$total,"showid"=>$showid,"mark"=>$giftinfo['mark'],"addtime"=>$addtime );
DI()->notorm->users_coinrecord->insert($insert); //添加消费记录,把用户的消费纪录下来
$userinfo2 =DI()->notorm->users
->select('consumption,coin')
->where('id = ?', $uid)
->fetchOne();
$level=getLevel($userinfo2['consumption']);
/* 更新主播热门 */
if($giftinfo['mark']==1){
DI()->notorm->users_live
->where('uid = ?', $liveuid)
->update( array('hotvotes' => new NotORM_Literal("hotvotes + {$total}") ));
}
DI()->redis->zIncrBy('user_'.$stream,$total,$uid);
/* 清除缓存 */
delCache("userinfo_".$uid);
delCache("userinfo_".$liveuid);
$votestotal=$this->getVotes($liveuid);
$gifttoken=md5(md5($action.$uid.$liveuid.$giftid.$giftcount.$total.$showid.$addtime.rand(100,999))); //生成一条送礼物的token,用来作为唯一的标识
$swf=$giftinfo['swf'] ? get_upload_path($giftinfo['swf']):'';
$result=array("uid"=>$uid,"giftid"=>$giftid,"type"=>$giftinfo['type'],"giftcount"=>$giftcount,"totalcoin"=>$total,"giftname"=>$giftinfo['giftname'],"gifticon"=>get_upload_path($giftinfo['gifticon']),"swftime"=>$giftinfo['swftime'],"swftype"=>$giftinfo['swftype'],"swf"=>$swf,"level"=>$level,"coin"=>$userinfo2['coin'],"votestotal"=>$votestotal,"gifttoken"=>$gifttoken);
return $result;
}
到现在为止,送礼物功能的实现已经完成一半了,接下来发送socket,就能使网络视频直播系统内的所有用户都收到这条socket,并且看到礼物的特效。
case 'SendGift':{
var gifToken = dataObj['msg'][0]['ct']; //获取送礼物的token
clientRedis.get(gifToken,function(error,res){ //利用礼物的token来获取送礼物的具体信息
if(!error&&res != null){
var resObj = evalJson(res);
dataObj['msg'][0]['ct'] = resObj;
io.sockets.in(socket.roomnum).emit('broadcastingListen',[JSON.stringify(dataObj)]); //将送礼物的信息发送到直播间的每个用户手机上,然后就可以看到礼物特效了
}
});
break;
}
在该功能的实现中我们常会遇到这样一些问题:
1.礼物信息错误
在出现该问题时我们需要查看一下数据库,是否在不经意间把礼物信息删除了。
2.用户方显示扣费,但礼物并未发出
出现该问题时我们需要看一下socket连接是否正常,以及socket中礼物的token获取是否正确
以上便是在网络视频直播系统中增添礼物功能的方法了,在开发直播app软件时一定不要忘记这个功能哦。更多与网络视频直播系统、直播软件开发的相关源码文章会在以后逐渐放出,敬请期待,需要的朋友请关注我。