成都网站建设开发公司,内江市建设培训中心网站,合肥城乡建设局官网,公司简介范本文字在前面我们已经对接好了腾讯多人音视频房间相关内容#xff1a;Java对接腾讯多人音视频房间示例 为了完善业务逻辑#xff0c;我们还需要对接它的一些回调接口 官方文档地址 主要就下面这些 这里因为比较简单直接上代码 里面有些工具类和上一章一样这里就没贴#xff0c;需要…在前面我们已经对接好了腾讯多人音视频房间相关内容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;AutowiredTxCallbackService txCallbackService;AutowiredCallbackListenerStrategyContext 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 {AutowiredRoomctlUtil roomctlUtil;/*** Description: 预约会议* Author: Sakura* Date: 2023/12/14 17:46*/OverrideTransactionalpublic Result createCallback(CreateCallbackRequest createCallbackRequest) {String result roomctlUtil.createCallback(createCallbackRequest.getAddress(), createCallbackRequest.getActions());if (!ok.equals(result)) {return Result.ERROR(result);}return Result.SUCCESS();}OverrideTransactionalpublic 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 开头建议使用更为安全的 httpsprivate String address;// 需要触发回调的情景列表请参考 回调命令字private ListString 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;// 操作的用户 IDJsonProperty(UserId)private String UserId;// 角色类型0为房主1为管理员2为普通成员JsonProperty(UserRole)private Integer UserRole;// 操作的房间 IDJsonProperty(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个 默认16JsonProperty(MaxSeatCount)private Integer MaxSeatCount;// 房间名称。默认为房间 IDJsonProperty(RoomName)private String RoomName;// 房间类型1为会议2为直播\n 默认为会议1JsonProperty(RoomType)private Integer RoomType;// 麦控模式1为自由发言模式2为举手发言模式3为就座后发言抢麦位\n默认为自由发言模式1JsonProperty(SpeechMode)private Integer SpeechMode;// 全体禁画 默认 falseJsonProperty(DisableVideo)private Boolean DisableVideo;// 全体静音 默认 falseJsonProperty(DisableAudio)private Boolean DisableAudio;// 禁止所有成员发送文本消息 默认 falseJsonProperty(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的方法存储MapString, FunctionCallbackRoomInfoRequest, CallbackResponse strategyContextMap new HashMap();ResourceCallbackListenerService callbackListenerService;PostConstructSneakyThrowspublic 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获取对应的方法返回策略FunctionCallbackRoomInfoRequest, 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;// 操作的房间 IDprivate 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可以照着写
这样就能收到回调信息了