RTC Room Engine
SDK to implement the host PK feature.RTC RoomEngine
SDK, you need to call the SDK login to ensure the subsequent features work properly.TUILiveBattleManager
plugin through the getLiveBattleManager
API.TUILiveBattleManager
plugin's requestBattle
API to implement this feature, passing in three parameters: PK configuration information, the user ID of the host to invite for PK, and the timeout duration.duration
.import RTCRoomEnginelet battleManager = TUIRoomEngine.sharedInstance().getLiveBattleManager()let config = TUIBattleConfig()config.duration = 30 // Please replace it with your PK duration in seconds.let userIds = ["100001"] // Please replace it with the user ID of the host you want to PK with.let timeout = 30 // Please replace it with your application timeout in seconds. If set to 0, the SDK will not perform timeout detection or trigger timeout callbacks.battleManager.requestBattle(config: config,userIdList: userIds,timeout: TimeInterval(timeout)) { battleInfo, battleResults in// PK request initiated successfully, battleId:battleInfo.battleId} onError: { code, message in// PK request initiation failed}
TUILiveBattleManager battleManager = TUIRoomEngine.sharedInstance().getLiveBattleManager();TUILiveBattleManager.BattleConfig config = new TUILiveBattleManager.BattleConfig();config.duration = 30; // Please replace it with your PK duration in secondsList<String> userIds = Collections.singletonList("100001"); // Please replace it with the user ID of the host you want to PK withint timeout = 30; // Please replace it with your application timeout in seconds. If set to 0, the SDK will not perform timeout detection or trigger timeout callbacksbattleManager.requestBattle(config, userIds, timeout, new TUILiveBattleManager.BattleRequestCallback() {@Overridepublic void onSuccess(TUILiveBattleManager.BattleInfo battleInfo, Map<String, TUILiveBattleManager.BattleCode> resultMap) {// PK request initiated successfully, battleId:battleInfo.battleId}@Overridepublic void onError(TUICommonDefine.Error error, String message) {// PK request initiation failed}});
addObserver
API for the TUILiveBattleManager
plugin, you will receive the onBattleRequestReceived
callback when someone requests to connect with you. You can accept or reject the request using the acceptBattle
/ rejectBattle
APIs.func onBattleRequestReceived(battleInfo: TUIBattleInfo,inviter: TUIBattleUser,invitee: TUIBattleUser) {// accept PK requestlet battleManager = TUIRoomEngine.sharedInstance().getLiveBattleManager()battleManager.acceptBattle(battleId: battleInfo.battleId) {// PK request accepted successfully} onError: { code, message in// PK request acceptance failed}// Reject the PK requestbattleManager.rejectBattle(battleId: battleInfo.battleId) {// PK request rejected successfully} onError: { code, message in// PK request rejection failed}}
public void onBattleRequestReceived(TUILiveBattleManager.BattleInfo battleInfo, TUILiveBattleManager.BattleUser inviter, TUILiveBattleManager.BattleUser invitee) {// accept PK requestTUILiveBattleManager battleManager = TUIRoomEngine.sharedInstance().getLiveBattleManager();battleManager.acceptBattle(battleInfo.battleId, new TUIRoomDefine.ActionCallback() {@Overridepublic void onSuccess() {// PK request accepted successfully}@Overridepublic void onError(TUICommonDefine.Error error, String message) {// PK request acceptance failed}});// Reject the PK requestbattleManager.rejectBattle(battleInfo.battleId, new TUIRoomDefine.ActionCallback() {@Overridepublic void onSuccess() {// PK request rejected successfully}@Overridepublic void onError(TUICommonDefine.Error error, String message) {// PK request rejection failed}});}
TUILiveBattleManager
plugin through the getLiveBattleManager
API.TUILiveBattleManager
plugin's cancelBattleRequest
API to implement this feature, passing in two parameters: the battleId from battleInfo after initiating a PK request successfully and the user ID of the host to abandon the PK invitation.let battleManager = TUIRoomEngine.sharedInstance().getLiveBattleManager()let battleId = "" // Please replace it with the battleId value from battleInfo after successfully calling the requestBattle API for PK.let userIds = ["100001"] // Please replace it with the user ID of the host you invited for PK.battleManager.cancelBattleRequest(battleId: battleId, userIdList: userIds) {// PK request abandoned successfully} onError: { code, message in// PK request abandonment failed}
TUILiveBattleManager battleManager = TUIRoomEngine.sharedInstance().getLiveBattleManager();String battleId = ""; // Please replace it with the battleId value from battleInfo after successfully calling the requestBattle API for PK.List<String> userIds = Collections.singletonList("100001"); // Please replace it with the user ID of the host to be invited for PKbattleManager.cancelBattleRequest(battleId, userIds, new TUIRoomDefine.ActionCallback() {@Overridepublic void onSuccess() {// PK request abandoned successfully}@Overridepublic void onError(TUICommonDefine.Error error, String message) {// PK request abandonment failed}});
TUILiveBattleManager
plugin through the getLiveBattleManager
API.TUILiveBattleManager
plugin's exitBattle
API, passing in the battleId from battleInfo after successfully initiating a PK request to exit the ongoing PK.import RTCRoomEnginelet battleManager = TUIRoomEngine.sharedInstance().getLiveBattleManager()let battleId = "" // Please replace it with the battleId value from battleInfo after successfully calling the requestBattle API for PK.battleManager.exitBattle(battleId: battleId) {// PK exited successfully} onError: { code, message in// PK exit failed}
TUILiveBattleManager battleManager = TUIRoomEngine.sharedInstance().getLiveBattleManager();String battleId = ""; // Please replace it with the battleId value from battleInfo after successfully calling the requestBattle API for PK.battleManager.exitBattle(battleId, new TUIRoomDefine.ActionCallback() {@Overridepublic void onSuccess() {// PK exited successfully}@Overridepublic void onError(TUICommonDefine.Error error, String message) {// PK exit failed}});
addObserver
to become an observer of TUILiveBattleManager
and listen for PK-related callbacks.AnchorBattleController
as an example to become an observer:import RTCRoomEngineclass AnchorBattleController: NSObject, TUILiveBattleObserver {override init() {super.init()TUIRoomEngine.sharedInstance().getLiveBattleManager().addObserver(self)}deinit {TUIRoomEngine.sharedInstance().getLiveBattleManager().removeObserver(self)}// Triggered when PK startsfunc onBattleStarted(battleInfo: TUIBattleInfo) {// PK started, battleId:battleInfo.battleId}// Triggered when PK endsfunc onBattleEnded(battleInfo: TUIBattleInfo, reason: TUIBattleStoppedReason) {// PK ended, battleId:battleInfo.battleId}// Triggered when a new host joins the PKfunc onUserJoinBattle(battleId: String, battleUser: TUIBattleUser) {// battleUser.userName joined the PK}// Triggered when a host in the PK leaves the PKfunc onUserExitBattle(battleId: String, battleUser: TUIBattleUser) {// battleUser.userName left the PK}// Triggered when PK score changesfunc onBattleScoreChanged(battleId: String, battleUserList: [TUIBattleUser]) {// PK score changed, the first user's score is: battleUserList.first?.score}// Triggered when a PK invitation is receivedfunc onBattleRequestReceived(battleInfo: TUIBattleInfo, inviter: TUIBattleUser, invitee: TUIBattleUser) {// Received a PK invitation from inviter.userName}// Triggered when a PK invitation is canceledfunc onBattleRequestCancelled(battleInfo: TUIBattleInfo, inviter: TUIBattleUser, invitee: TUIBattleUser) {// PK invitation from inviter.userName was canceled}// Triggered when a PK invitation times outfunc onBattleRequestTimeout(battleInfo: TUIBattleInfo, inviter: TUIBattleUser, invitee: TUIBattleUser) {// PK invitation from inviter.userName timed out}// Triggered when a sent PK invitation is acceptedfunc onBattleRequestAccept(battleInfo: TUIBattleInfo, inviter: TUIBattleUser, invitee: TUIBattleUser) {// PK invitation to invitee.userName was accepted}// Triggered when a sent PK invitation is rejectedfunc onBattleRequestReject(battleInfo: TUIBattleInfo, inviter: TUIBattleUser, invitee: TUIBattleUser) {// The PK invitation to invitee.userName was rejected}}
addObserver
to become an observer of TUILiveBattleManager
and listen for PK-related callbacks.AnchorBattleObserver
as an example to become an observer:class AnchorBattleObserver extends TUILiveBattleManager.Observer {AnchorBattleObserver() {TUIRoomEngine.sharedInstance().getLiveBattleManager().addObserver(this);}// Triggered when PK starts@Overridepublic void onBattleStarted(TUILiveBattleManager.BattleInfo battleInfo) {// PK started, battleId:battleInfo.battleId}// Triggered when PK ends@Overridepublic void onBattleEnded(TUILiveBattleManager.BattleInfo battleInfo, TUILiveBattleManager.BattleStoppedReason reason) {// PK ended, battleId:battleInfo.battleId}// Triggered when a new host joins the PK@Overridepublic void onUserJoinBattle(String battleId, TUILiveBattleManager.BattleUser battleUser) {// battleUser.userName joined the PK}// Triggered when a host in the PK leaves the PK@Overridepublic void onUserExitBattle(String battleId, TUILiveBattleManager.BattleUser battleUser) {// battleUser.userName left the PK}// Triggered when PK score changes@Overridepublic void onBattleScoreChanged(String battleId, List<TUILiveBattleManager.BattleUser> battleUserList) {// PK score changed, the first user's score is: battleUserList.first?.score}// Triggered when a PK invitation is received@Overridepublic void onBattleRequestReceived(TUILiveBattleManager.BattleInfo battleInfo, TUILiveBattleManager.BattleUser inviter, TUILiveBattleManager.BattleUser invitee) {// Received a PK invitation from inviter.userName}// Triggered when a PK invitation is canceled@Overridepublic void onBattleRequestCancelled(TUILiveBattleManager.BattleInfo battleInfo, TUILiveBattleManager.BattleUser inviter, TUILiveBattleManager.BattleUser invitee) {// PK invitation from inviter.userName was canceled}// Triggered when a PK invitation times out@Overridepublic void onBattleRequestTimeout(TUILiveBattleManager.BattleInfo battleInfo, TUILiveBattleManager.BattleUser inviter, TUILiveBattleManager.BattleUser invitee) {// PK invitation from inviter.userName timed out}// Triggered when a sent PK invitation is accepted@Overridepublic void onBattleRequestAccept(TUILiveBattleManager.BattleInfo battleInfo, TUILiveBattleManager.BattleUser inviter, TUILiveBattleManager.BattleUser invitee) {// PK invitation to invitee.userName was accepted}// Triggered when a sent PK invitation is rejected@Overridepublic void onBattleRequestReject(TUILiveBattleManager.BattleInfo battleInfo, TUILiveBattleManager.BattleUser inviter, TUILiveBattleManager.BattleUser invitee) {// The PK invitation to invitee.userName was rejected}}
Was this page helpful?