书接上回《浅尝不辄止系列之试试腾讯云的 TUIRoom(上)》
前提
上篇主要聊了一下集成 TURRoom 的前端部分。
涉及到的代码不是很多,主要是思路,因为我觉得思路和画面感对程序员来说比编码更重要,想清楚自己要做的东西,在脑海里是个什么样子的,结合相关文档资料,只要能基本确定可以实现,就可以放手去干了,这样做绝对会事半功倍!
集成服务端 SDK
其实服务端的部分比较简单,只需要引入腾讯的服务端开发包,然后根据需求,稍加封装就可以了
package install TecentCloudSDK
复制代码
public class MyTrtc
{
private IResponse resp;
public MyTrtc(IResponse Resp)
{
resp = Resp;
}
//tecentCloud子账号信用凭证
public static readonly Credential cred = new Credential
{
SecretId = "自己的id",
SecretKey = "自己的秘钥"
};
/// <summary>
/// 查询房间内的人数
/// </summary>
/// <param name="CommId"></param>
/// <param name="startTime"></param>
/// <param name="endTime"></param>
/// <returns></returns>
//public static Response DescribeCallDetail(string CommId, ulong startTime,ulong endTime)
public static async Task<Response> DescribeCallDetail(MemberModel model)
{
Response resp = new Response();
try
{
ClientProfile clientProfile = new ClientProfile();
HttpProfile httpProfile = new HttpProfile();
httpProfile.Endpoint = ("trtc.tencentcloudapi.com");
clientProfile.HttpProfile = httpProfile;
TrtcClient client = new TrtcClient(cred, "ap-guangzhou", clientProfile);
DescribeCallDetailRequest req = new DescribeCallDetailRequest();
req.CommId = model.CommId;
req.StartTime = model.StartTime;
req.EndTime = model.EndTime;
req.PageNumber = (Convert.ToInt32(model.pageindex) - 1).ToString();
req.PageSize = model.pagesize;
req.SdkAppId = Common.ConfigurationHelper.GetSectionValue("trtcId");
DescribeCallDetailResponse dresp = await client.DescribeCallDetail(req);
resp.code = 1;
resp.data = new { total = dresp.Total, items = dresp.UserList };
}
catch(Exception ex)
{
resp.code = -1;
resp.message = $"查询房间人数失败,{ex.Message},{ex.StackTrace}";
}
return resp;
}
}
复制代码
因为封住的部分,主要就是根据实际需求灌装代码了,我就不一一列举了,这里就列一个查询房间人数的逻辑,其余的,还有“查询房间列表”,“解散房间”,“踢人”,“推流”,“终止推流”这几个方法,大家感兴趣的,可以看官方手册,按需接入就可以了。
/// <summary>
/// 查询房间列表
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> DescribeRoomInformation(RequestModels.Trtc.RoomModel model)
{
return Json(await MyTrtc.DescribeRoomInformation(model));
}
/// <summary>
/// 解散房间
/// </summary>
/// <param name="roomId"></param>
/// <returns></returns>
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> DismissRoom(RequestModels.Trtc.RoomModel model)
{
return Json(await MyTrtc.DismissRoom(model.RoomId));
}
/// <summary>
/// 房间内用户列表
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> DescribeCallDetail(RequestModels.Trtc.MemberModel model)
{
return Json(await MyTrtc.DescribeCallDetail(model));
}
/// <summary>
/// 移除房间内用户
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> RemoveUser(RequestModels.Trtc.RoomModel model)
{
return Json(await MyTrtc.RemoveUser(model));
}
/// <summary>
/// 直播推流
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
[HttpPost]
public async Task<IActionResult> PushLive(RequestModels.Trtc.PushStreamModel model)
{
return Json(await MyTrtc.PushLive(model));
}
/// <summary>
/// 结束推流
/// </summary>
/// <param name="roomId"></param>
/// <returns></returns>
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> EndPushLive(ulong roomId)
{
return Json(await MyTrtc.EndPushLive(roomId));
}
复制代码
服务端的集成基本就是这样。集成好后,就可以进行旁路直播的基本管理了。
好了,基本就是这样了,一篇拆两篇,希望官方大大原谅~~。
评论