Java对接腾讯多人音视频房间回调接口示例

发布时间:2023年12月19日

在前面我们已经对接好了腾讯多人音视频房间相关内容:Java对接腾讯多人音视频房间示例
为了完善业务逻辑,我们还需要对接它的一些回调接口 官方文档地址
主要就下面这些

在这里插入图片描述

这里因为比较简单直接上代码
里面有些工具类和上一章一样这里就没贴,需要的看下我上一章Java对接腾讯多人音视频房间示例


import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

/**
 * @Description: 腾讯会议回调
 * @Author: Sakura
 * @Date: 2023/12/18 14:43
 */
@Log
@RestController
@RequestMapping("/txcloud/callback")
public class TxCallbackController {

    @Value("${tencent.roomctl.SDKAppId}")
    private Long SDKAppId;

    @Autowired
    TxCallbackService txCallbackService;

    @Autowired
    CallbackListenerStrategyContext callbackListenerStrategyContext;

//    // 实例参数
//    {
//        "address": "https://www.zscloud.work:29000/api/txcloud/callback/listener",
//        "actions": ["createRoom", "destroyRoom", "updateRoomInfo", "enterRoom", "exitRoom", "updateUserRole", "takeSeat", "leaveSeat"]
//    }

    /**
     * @Description: 配置回调地址
     * @Author: Sakura
     * @Date: 2023/12/18 14:52
     */
    @PostMapping("/create")
    public Result createCallback(@RequestBody CreateCallbackRequest createCallbackRequest) {
        return txCallbackService.createCallback(createCallbackRequest);
    }

    /** 
     * @Description: 修改回调地址配置
     * @Author: Sakura
     * @Date: 2023/12/19 15:55
    */
    @PostMapping("/update")
    public Result updateCallback(@RequestBody CreateCallbackRequest createCallbackRequest) {
        return txCallbackService.updateCallback(createCallbackRequest);
    }

    /**
     * @Description: 回调监听
     * @Author: Sakura
     * @Date: 2023/12/19 11:00
     */
    @PostMapping("/listener")
    public CallbackResponse callbackListener(@RequestParam Long sdkappid, @RequestParam String command,
                                   @RequestParam String contenttype, @RequestParam String clientip,
                                   @RequestParam String optplatform,
                                   @RequestBody CallbackRoomInfoRequest callbackRoomInfoRequest) {
        log.info("回调监听:" + "sdkappid:" + sdkappid + "  command:" + command + "  " + callbackRoomInfoRequest.toString());
        // 目前只能校验SDKAppId是否一致
        if (!SDKAppId.equals(sdkappid)) {
            return CallbackResponse.ERROR();
        }

        return callbackListenerStrategyContext.callbackListener(command, callbackRoomInfoRequest);
    }

}


/** 
 * @Description: 多人视频管理
 * @Author: Sakura
 * @Date: 2023/12/14 17:46
*/
public interface TxCallbackService {

    Result createCallback(CreateCallbackRequest createCallbackRequest);

    Result updateCallback(CreateCallbackRequest createCallbackRequest);

}


import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/** 
 * @Description: 回调管理
 * @Author: Sakura
 * @Date: 2023/12/18 14:52
*/
@Log
@Service
public class TxCallbackServiceImpl implements TxCallbackService {

    @Autowired
    RoomctlUtil roomctlUtil;

    /**
     * @Description: 预约会议
     * @Author: Sakura
     * @Date: 2023/12/14 17:46
     */
    @Override
    @Transactional
    public Result createCallback(CreateCallbackRequest createCallbackRequest) {
        String result = roomctlUtil.createCallback(createCallbackRequest.getAddress(), createCallbackRequest.getActions());
        if (!"ok".equals(result)) {
            return Result.ERROR(result);
        }

        return Result.SUCCESS();
    }

    @Override
    @Transactional
    public Result updateCallback(CreateCallbackRequest createCallbackRequest) {
        String result = roomctlUtil.updateCallback(createCallbackRequest.getAddress(), createCallbackRequest.getActions());
        if (!"ok".equals(result)) {
            return Result.ERROR(result);
        }

        return Result.SUCCESS();
    }

}

下面是请求参数的封装类


import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.List;

/** 
 * @Description: 创建回调参数
 * @Author: Sakura
 * @Date: 2023/12/18 14:50
*/
@Data
@EqualsAndHashCode(callSuper = false)
public class CreateCallbackRequest extends PageRequest {

    private static final long serialVersionUID = 1L;

    // 回调的地址,需要以 http/https 开头,建议使用更为安全的 https
    private String address;

    // 需要触发回调的情景,列表请参考 回调命令字
    private List<String> actions;

}


import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.io.Serializable;

/** 
 * @Description: 回调房间详细信息 
 * @Author: Sakura
 * @Date: 2023/12/19 13:43
*/
@Data
@EqualsAndHashCode(callSuper = false)
public class CallbackRoomInfoRequest implements Serializable {

    private static final long serialVersionUID = 1L;

    // 操作的用户 ID
    @JsonProperty("UserId")
    private String UserId;

    // 角色类型:0为房主,1为管理员,2为普通成员
    @JsonProperty("UserRole")
    private Integer UserRole;

    // 操作的房间 ID
    @JsonProperty("RoomId")
    private String RoomId;

    // 当前房间成员数量
    @JsonProperty("MemberCount")
    private Integer MemberCount;

    // 用户退出的原因
    @JsonProperty("ExitReason")
    private String ExitReason;

    // 当前的麦位数量
    @JsonProperty("SeatCount")
    private Integer SeatCount;

    // 房间详情
    @JsonProperty("RoomInfo")
    private RoomInfoRequest RoomInfo;

    // 事件触发时间戳
    @JsonProperty("EventTime")
    private Long EventTime;
}


import lombok.Data;
import lombok.EqualsAndHashCode;

import java.io.Serializable;

/**
 * @Description: 回调房间信息
 * @Author: Sakura
 * @Date: 2023/12/19 13:41
 */
@Data
@EqualsAndHashCode(callSuper = false)
public class RoomInfoRequest implements Serializable {

    private static final long serialVersionUID = 1L;

    // 最大麦位数量,默认为套餐包上限,(直播最大16个 默认16)
    @JsonProperty("MaxSeatCount")
    private Integer MaxSeatCount;

    // 房间名称。默认为房间 ID
    @JsonProperty("RoomName")
    private String RoomName;

    // 房间类型:1为会议,2为直播\n 默认为:会议1
    @JsonProperty("RoomType")
    private Integer RoomType;

    // 麦控模式:1为自由发言模式,2为举手发言模式,3为就座后发言(抢麦位)\n默认为:自由发言模式1
    @JsonProperty("SpeechMode")
    private Integer SpeechMode;

    // 全体禁画 默认 false
    @JsonProperty("DisableVideo")
    private Boolean DisableVideo;

    // 全体静音 默认 false
    @JsonProperty("DisableAudio")
    private Boolean DisableAudio;

    // 禁止所有成员发送文本消息 默认 false
    @JsonProperty("DisableMessage")
    private Boolean DisableMessage;

}

再就是回调这块,因为回调的都是一个方法,通过command来区分,所以这里用策略模式来处理


import lombok.SneakyThrows;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;

/**
 * @author Sakura
 * @date 2023/12/19 11:16
 */
@Service
public class CallbackListenerStrategyContext {
    // 策略采用lambda的方法存储
    Map<String, Function<CallbackRoomInfoRequest, CallbackResponse>> strategyContextMap = new HashMap<>();

    @Resource
    CallbackListenerService callbackListenerService;

    @PostConstruct
    @SneakyThrows
    public void setStrategyMap() {
        // 配置回调方法
        strategyContextMap.put("createRoom",(callbackRoomInfoRequest)-> callbackListenerService.createRoom(callbackRoomInfoRequest));
        strategyContextMap.put("destroyRoom",(callbackRoomInfoRequest)-> callbackListenerService.destroyRoom(callbackRoomInfoRequest));
        strategyContextMap.put("updateRoomInfo",(callbackRoomInfoRequest)-> callbackListenerService.updateRoomInfo(callbackRoomInfoRequest));
        strategyContextMap.put("enterRoom",(callbackRoomInfoRequest)-> callbackListenerService.enterRoom(callbackRoomInfoRequest));
        strategyContextMap.put("exitRoom",(callbackRoomInfoRequest)-> callbackListenerService.exitRoom(callbackRoomInfoRequest));
        strategyContextMap.put("takeSeat",(callbackRoomInfoRequest)-> callbackListenerService.takeSeat(callbackRoomInfoRequest));
        strategyContextMap.put("leaveSeat",(callbackRoomInfoRequest)-> callbackListenerService.leaveSeat(callbackRoomInfoRequest));

    }

    public CallbackResponse callbackListener(String command, CallbackRoomInfoRequest callbackRoomInfoRequest){
        // 根据command获取对应的方法返回策略
        Function<CallbackRoomInfoRequest, CallbackResponse> callbackListenerFunc = strategyContextMap.get(command);
        return callbackListenerFunc.apply(callbackRoomInfoRequest);
    }
}



import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author Sakura
 * @date 2023/12/19 11:42
 */
@Service
@Log
public class CallbackListenerService {

    // 此处用来统一处理多人视频房间回调++++++++++++++++++++++++++++++++++++++++++++

    // 创建房间回调
    public CallbackResponse createRoom(CallbackRoomInfoRequest callbackRoomInfoRequest){
        log.info("创建房间回调" + callbackRoomInfoRequest.toString());
    
        return CallbackResponse.success();
    }

    // 销毁房间回调
    public CallbackResponse destroyRoom(CallbackRoomInfoRequest callbackRoomInfoRequest){
        log.info("销毁房间回调" + callbackRoomInfoRequest.toString());
        
        return CallbackResponse.success();
    }

    // 更新房间回调
    public CallbackResponse updateRoomInfo(CallbackRoomInfoRequest callbackRoomInfoRequest){
        log.info("更新房间回调" + callbackRoomInfoRequest.toString());
       
        return CallbackResponse.success();
    }

    // 用户进房回调
    public CallbackResponse enterRoom(CallbackRoomInfoRequest callbackRoomInfoRequest){
        log.info("用户进房回调" + callbackRoomInfoRequest.toString());
        
        return CallbackResponse.success();
    }

    // 用户退房回调
    public CallbackResponse exitRoom(CallbackRoomInfoRequest callbackRoomInfoRequest){
        log.info("用户退房回调" + callbackRoomInfoRequest.toString());
        
        return CallbackResponse.success();
    }

    // 用户上麦回调
    public CallbackResponse takeSeat(CallbackRoomInfoRequest callbackRoomInfoRequest){
        log.info("用户上麦回调" + callbackRoomInfoRequest.toString());
        
        return CallbackResponse.success();
    }

    // 用户下麦回调
    public CallbackResponse leaveSeat(CallbackRoomInfoRequest callbackRoomInfoRequest){
        log.info("用户下麦回调" + callbackRoomInfoRequest.toString());
        
        return CallbackResponse.success();
    }
}

返回对象


import lombok.Data;
import lombok.EqualsAndHashCode;

import java.io.Serializable;

/** 
 * @Description: 回调返回信息
 * @Author: Sakura
 * @Date: 2023/12/19 13:41
*/
@Data
@EqualsAndHashCode(callSuper = false)
public class CallbackResponse implements Serializable {

    private static final long serialVersionUID = 1L;

    // 当前房间成员数量
    private Integer ErrorCode;

    // 操作的房间 ID
    private String ErrorMessage;

    public static CallbackResponse success() {
        CallbackResponse callbackResponse = new CallbackResponse();
        callbackResponse.setErrorCode(0);
        callbackResponse.setErrorMessage("Success");
        return callbackResponse;
    }

    public static CallbackResponse ERROR() {
        CallbackResponse callbackResponse = new CallbackResponse();
        callbackResponse.setErrorCode(1);
        callbackResponse.setErrorMessage("Fail");
        return callbackResponse;
    }

}

我们需要先调用配置回调地址的接口把我们的接口配置上去

{
    "address": "https://www.sakura.com/txcloud/callback/listener",
    "actions": ["createRoom", "destroyRoom", "updateRoomInfo", "enterRoom", "exitRoom", "updateUserRole", "takeSeat", "leaveSeat"]
}

在这里插入图片描述

注意address换成自己的域名,actions可以照着写

这样就能收到回调信息了

文章来源:https://blog.csdn.net/cl617287/article/details/135091404
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。