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

在前面我们已经对接好了腾讯多人音视频房间相关内容:Java对接腾讯多人音视频房间示例

为了完善业务逻辑,我们还需要对接它的一些回调接口 官方文档地址

主要就下面这些

这里因为比较简单直接上代码

里面有些工具类和上一章一样这里就没贴,需要的看下我上一章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.sakura.com/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);
    }

}
java 复制代码
/** 
 * @Description: 多人视频管理
 * @Author: Sakura
 * @Date: 2023/12/14 17:46
*/
public interface TxCallbackService {

    Result createCallback(CreateCallbackRequest createCallbackRequest);

    Result updateCallback(CreateCallbackRequest createCallbackRequest);

}
java 复制代码
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/18 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();
    }

}

下面是请求参数的封装类

java 复制代码
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 implements Serializable {

    private static final long serialVersionUID = 1L;

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

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

}
java 复制代码
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;
}
java 复制代码
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来区分,所以这里用策略模式来处理

java 复制代码
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);
    }
}
java 复制代码
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();
    }
}

返回对象

java 复制代码
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;

    // 异常消息
    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;
    }

}

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

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

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

这样就能收到回调信息了

相关推荐
钱多多_qdd4 分钟前
spring cache源码解析(四)——从@EnableCaching开始来阅读源码
java·spring boot·spring
waicsdn_haha6 分钟前
Java/JDK下载、安装及环境配置超详细教程【Windows10、macOS和Linux图文详解】
java·运维·服务器·开发语言·windows·后端·jdk
_WndProc8 分钟前
C++ 日志输出
开发语言·c++·算法
Q_192849990617 分钟前
基于Spring Boot的摄影器材租赁回收系统
java·spring boot·后端
qq_4335545417 分钟前
C++ 面向对象编程:+号运算符重载,左移运算符重载
开发语言·c++
Code_流苏19 分钟前
VSCode搭建Java开发环境 2024保姆级安装教程(Java环境搭建+VSCode安装+运行测试+背景图设置)
java·ide·vscode·搭建·java开发环境
数据小爬虫@36 分钟前
如何高效利用Python爬虫按关键字搜索苏宁商品
开发语言·爬虫·python
ZJ_.38 分钟前
WPSJS:让 WPS 办公与 JavaScript 完美联动
开发语言·前端·javascript·vscode·ecmascript·wps
Narutolxy44 分钟前
深入探讨 Go 中的高级表单验证与翻译:Gin 与 Validator 的实践之道20241223
开发语言·golang·gin
Hello.Reader1 小时前
全面解析 Golang Gin 框架
开发语言·golang·gin