this document primarily introduces how to use RTC Room Engine SDK to implement Anchor PK Feature.
Prerequisites
Before using the RTC RoomEngine SDK, you need to log in to the SDK first so that subsequent features can be used normally. User Guide
Note:
When using the PK Function, ensure you have started broadcasting.
PK Request
First, you need to obtain the TUILiveBattleManager plug-in through the getLiveBattleManager API.
Then reuse the requestBattle API of the TUILiveBattleManager plug-in to implement the feature, inputting three parameters: PK configuration message, the user Id of the inviting PK anchor, and timeout duration.
PK configuration message is a structure of TUIBattleConfig. When configuring, you generally just need to set the PK duration duration.
Take inviting an anchor with user Id 100001 for PK as an example:
import RTCRoomEngine
let battleManager = TUIRoomEngine.sharedInstance().getLiveBattleManager()
let config = TUIBattleConfig()
config.duration = 30
let userIds = ["100001"]
let timeout = 30
battleManager.requestBattle(config: config,
userIdList: userIds,
timeout: TimeInterval(timeout)) { battleInfo, battleResults in
} onError: { code, message in
}
TUILiveBattleManager battleManager = TUIRoomEngine.sharedInstance().getLiveBattleManager();
TUILiveBattleManager.BattleConfig config = new TUILiveBattleManager.BattleConfig();
config.duration = 30;
List<String> userIds = Collections.singletonList("100001");
int timeout = 30;
battleManager.requestBattle(config, userIds, timeout, new TUILiveBattleManager.BattleRequestCallback() {
@Override
public void onSuccess(TUILiveBattleManager.BattleInfo battleInfo, Map<String, TUILiveBattleManager.BattleCode> resultMap) {
}
@Override
public void onError(TUICommonDefine.Error error, String message) {
}
});
If you become an observer of the TUILiveBattleManager plug-in through the addObserver API, you will receive the onBattleRequestReceived callback when someone applies to connect with you. You can accept or reject the request via the acceptBattle / rejectBattle APIs.
func onBattleRequestReceived(battleInfo: TUIBattleInfo,
inviter: TUIBattleUser,
invitee: TUIBattleUser) {
let battleManager = TUIRoomEngine.sharedInstance().getLiveBattleManager()
battleManager.acceptBattle(battleId: battleInfo.battleId) {
} onError: { code, message in
}
battleManager.rejectBattle(battleId: battleInfo.battleId) {
} onError: { code, message in
}
}
public void onBattleRequestReceived(TUILiveBattleManager.BattleInfo battleInfo, TUILiveBattleManager.BattleUser inviter, TUILiveBattleManager.BattleUser invitee) {
TUILiveBattleManager battleManager = TUIRoomEngine.sharedInstance().getLiveBattleManager();
battleManager.acceptBattle(battleInfo.battleId, new TUIRoomDefine.ActionCallback() {
@Override
public void onSuccess() {
}
@Override
public void onError(TUICommonDefine.Error error, String message) {
}
});
battleManager.rejectBattle(battleInfo.battleId, new TUIRoomDefine.ActionCallback() {
@Override
public void onSuccess() {
}
@Override
public void onError(TUICommonDefine.Error error, String message) {
}
});
}
Give Up PK Request
First, you need to obtain the TUILiveBattleManager plug-in through the getLiveBattleManager API.
Then reuse the cancelBattleRequest API of the TUILiveBattleManager plug-in to implement the feature, inputting two parameters: the battleId of battleInfo after initiating a PK Request successfully and the user Id of the anchor who cancels the PK invitation. Take giving up the PK request with the anchor whose user Id is 100001 as an example:
let battleManager = TUIRoomEngine.sharedInstance().getLiveBattleManager()
let battleId = ""
let userIds = ["100001"]
battleManager.cancelBattleRequest(battleId: battleId, userIdList: userIds) {
} onError: { code, message in
}
TUILiveBattleManager battleManager = TUIRoomEngine.sharedInstance().getLiveBattleManager();
String battleId = "";
List<String> userIds = Collections.singletonList("100001");
battleManager.cancelBattleRequest(battleId, userIds, new TUIRoomDefine.ActionCallback() {
@Override
public void onSuccess() {
}
@Override
public void onError(TUICommonDefine.Error error, String message) {
}
});
Exit PK
First, you need to obtain the TUILiveBattleManager plug-in through the getLiveBattleManager API.
Then use the exitBattle API of the TUILiveBattleManager plug-in. Input parameter: the battleId in battleInfo after thePK Request is initiated successfully, to exit the ongoing PK. import RTCRoomEngine
let battleManager = TUIRoomEngine.sharedInstance().getLiveBattleManager()
let battleId = ""
battleManager.exitBattle(battleId: battleId) {
} onError: { code, message in
}
TUILiveBattleManager battleManager = TUIRoomEngine.sharedInstance().getLiveBattleManager();
String battleId = "";
battleManager.exitBattle(battleId, new TUIRoomDefine.ActionCallback() {
@Override
public void onSuccess() {
}
@Override
public void onError(TUICommonDefine.Error error, String message) {
}
});
Listening Callback
You can become an observer of TUILiveBattleManager by calling addObserver to listen for PK-related callbacks.
Take AnchorBattleController becoming an observer as an example:
import RTCRoomEngine
class AnchorBattleController: NSObject, TUILiveBattleObserver {
override init() {
super.init()
TUIRoomEngine.sharedInstance().getLiveBattleManager().addObserver(self)
}
deinit {
TUIRoomEngine.sharedInstance().getLiveBattleManager().removeObserver(self)
}
func onBattleStarted(battleInfo: TUIBattleInfo) {
}
func onBattleEnded(battleInfo: TUIBattleInfo, reason: TUIBattleStoppedReason) {
}
func onUserJoinBattle(battleId: String, battleUser: TUIBattleUser) {
}
func onUserExitBattle(battleId: String, battleUser: TUIBattleUser) {
}
func onBattleScoreChanged(battleId: String, battleUserList: [TUIBattleUser]) {
}
func onBattleRequestReceived(battleInfo: TUIBattleInfo, inviter: TUIBattleUser, invitee: TUIBattleUser) {
}
func onBattleRequestCancelled(battleInfo: TUIBattleInfo, inviter: TUIBattleUser, invitee: TUIBattleUser) {
}
You can become an observer of TUILiveConnectionManager by calling addObserver to listen to connection-related callbacks.
Take CoHostObserver becoming an observer as an example:
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) {
}
}