本文档主要介绍如何使用 RTC Room Engine
SDK 实现 主播连线功能。
前提条件
在使用 RTC RoomEngine
SDK前,您需要先调用 登录 SDK,以便后续功能正常使用。 使用指引
发起连线请求
您首先需要通过 getLiveConnectionManager
接口获取 TUILiveConnectionManager
插件。
然后再使用 TUILiveConnectionManager
插件的 requestConnection
接口实现该功能,传入三个参数:邀请连线的主播所在房间的房间Id、超时时长和扩展信息。
以邀请直播间Id为 live_100002 的主播连线为例:
import RTCRoomEngine
let roomIdList = ["live_100002"]
let timeout = 30
let extensionInfo = ""
let connectionManager = TUIRoomEngine.sharedInstance().getLiveConnectionManager()
connectionManager.requestConnection(roomIdList: roomIdList,
timeout: TimeInterval(timeout),
extensionInfo: extensionInfo) { connectionResults in
} onError: { code, message in
}
List<String> roomIdList = Collections.singletonList("live_100002");
int timeout = 30;
String extensionInfo = "";
TUILiveConnectionManager connectionManager = TUIRoomEngine.sharedInstance().getLiveConnectionManager();
connectionManager.requestConnection(roomIdList, timeout, extensionInfo, new TUILiveConnectionManager.ConnectionRequestCallback() {
@Override
public void onSuccess(Map<String, TUILiveConnectionManager.ConnectionCode> resultMap) {
}
@Override
public void onError(TUICommonDefine.Error error, String message) {
}
});
若您通过 addObserver
接口成为了 TUILiveConnectionManager
SDK的观察者,则当有人申请和您连线时,您会收到 onConnectionRequestReceived
回调,您可通过 acceptConnection
/ rejectConnection
接口接受或拒绝该请求。
func onConnectionRequestReceived(inviter: TUIConnectionUser,
inviteeList: [TUIConnectionUser],
extensionInfo: String) {
connectionManager.acceptConnection(inviter.roomId) {
} onError: { code, message in
}
connectionManager.rejectConnection(inviter.roomId) {
} onError: { code, message in
}
}
public void onConnectionRequestReceived(TUILiveConnectionManager.ConnectionUser inviter,
List<TUILiveConnectionManager.ConnectionUser> inviteeList,
String extensionInfo) {
connectionManager.acceptConnection(inviter.roomId, new TUIRoomDefine.ActionCallback() {
@Override
public void onSuccess() {
}
@Override
public void onError(TUICommonDefine.Error error, String message) {
}
});
connectionManager.rejectConnection(inviter.roomId, new TUIRoomDefine.ActionCallback() {
@Override
public void onSuccess() {
}
@Override
public void onError(TUICommonDefine.Error error, String message) {
}
});
}
放弃连线请求
您首先需要通过 getLiveConnectionManager
接口获取 TUILiveConnectionManager
插件。
然后再使用 TUILiveConnectionManager
插件的 cancelConnectionRequest
接口实现该功能,传入参数:放弃连线邀请的主播所在房间的房间Id。
以放弃和直播间Id为 live_100002 的主播连线请求为例:
let roomIdList = ["live_100002"]
let connectionManager = TUIRoomEngine.sharedInstance().getLiveConnectionManager()
connectionManager.cancelConnectionRequest(roomIdList: roomIdList) {
} onError: { code, message in
}
List<String> roomIdList = Collections.singletonList("live_100002");
TUILiveConnectionManager connectionManager = TUIRoomEngine.sharedInstance().getLiveConnectionManager();
connectionManager.cancelConnectionRequest(roomIdList, new TUIRoomDefine.ActionCallback() {
@Override
public void onSuccess() {
}
@Override
public void onError(TUICommonDefine.Error error, String message) {
}
});
中断连线
您首先需要通过 getLiveConnectionManager
接口获取 TUILiveConnectionManager
插件。
然后再使用 TUILiveConnectionManager
插件的 disconnect
接口中断当前正在进行的连线。
import RTCRoomEngine
let connectionManager = TUIRoomEngine.sharedInstance().getLiveConnectionManager()
connectionManager.disconnect {
} onError: { code, message in
}
TUILiveConnectionManager connectionManager = TUIRoomEngine.sharedInstance().getLiveConnectionManager();
connectionManager.disconnect(new TUIRoomDefine.ActionCallback() {
@Override
public void onSuccess() {
}
@Override
public void onError(TUICommonDefine.Error error, String message) {
}
});
监听回调
您可通过调用 addObserver
成为 TUILiveConnectionManager
的观察者,监听连线相关的回调。
以 CoHostController
成为观察者为例:
import RTCRoomEngine
class CoHostController: NSObject, TUILiveConnectionObserver {
override init() {
super.init()
TUIRoomEngine.sharedInstance().getLiveConnectionManager().addObserver(self)
}
deinit {
TUIRoomEngine.sharedInstance().getLiveConnectionManager().removeObserver(self)
}
func onConnectionUserListChanged(connectedList: [TUIConnectionUser],
joinedList: [TUIConnectionUser],
leavedList: [TUIConnectionUser]) {
}
func onConnectionRequestReceived(inviter: TUIConnectionUser,
inviteeList: [TUIConnectionUser],
extensionInfo: String) {
}
func onConnectionRequestCancelled(inviter: TUIConnectionUser) {
}
func onConnectionRequestAccept(invitee: TUIConnectionUser) {
}
func onConnectionRequestReject(invitee: TUIConnectionUser) {
}
func onConnectionRequestTimeout(inviter: TUIConnectionUser, invitee: TUIConnectionUser) {
}
}
您可通过调用 addObserver
成为 TUILiveConnectionManager
的观察者,监听连线相关的回调。
以 CoHostObserver
成为观察者为例:
class CoHostObserver extends TUILiveConnectionManager.Observer {
CoHostObserver() {
TUIRoomEngine.sharedInstance().getLiveConnectionManager().addObserver(this);
}
@Override
public void onConnectionUserListChanged(List<TUILiveConnectionManager.ConnectionUser> connectedList,
List<TUILiveConnectionManager.ConnectionUser> joinedList,
List<TUILiveConnectionManager.ConnectionUser> leavedList) {
}
@Override
public void onConnectionRequestReceived(TUILiveConnectionManager.ConnectionUser inviter,
List<TUILiveConnectionManager.ConnectionUser> inviteeList,
String extensionInfo) {
}
@Override
public void onConnectionRequestCancelled(TUILiveConnectionManager.ConnectionUser inviter) {
}
@Override
public void onConnectionRequestAccept(TUILiveConnectionManager.ConnectionUser invitee) {
}
@Override
public void onConnectionRequestReject(TUILiveConnectionManager.ConnectionUser invitee) {
}
@Override
public void onConnectionRequestTimeout(TUILiveConnectionManager.ConnectionUser inviter,
TUILiveConnectionManager.ConnectionUser invitee) {
}
}
本页内容是否解决了您的问题?