快速入门
业务流程简介
主要业务模块及流程如下图,开发者可根据该图了解不同功能接口调用时机:

实现多人游戏
通过模拟一个简单的 Unity 象棋游戏项目,在不同游戏页面中调用对应 SDK 接口 ,让开发者快速了解接入流程。
该项目中主要游戏页面及流程如下:
游戏启动页面 -> 登录页面 -> 游戏大厅页 -> 房间列表及房间内页面 -> 象棋对战页面
游戏启动与登录页面
在游戏启动页面,调用 SDK 初始化接口,示例如下:
- Unity
- Android Java
- Android Kotlin
- iOS Swift
- iOS Objective-C
using TapSDK.Core;
// 核心配置
TapTapSdkOptions coreOptions = new TapTapSdkOptions
{
// 客户端 ID,开发者后台获取
clientId = clientId,
// 客户端令牌,开发者后台获取
clientToken = clientToken,
// 地区,CN 为国内,Overseas 为海外
region = TapTapRegionType.CN
};
// TapSDK 初始化
TapTapSDK.Init(coreOptions);
import com.taptap.sdk.core.TapTapRegion;
import com.taptap.sdk.core.TapTapSdk;
import com.taptap.sdk.core.TapTapSdkOptions;
// 开发者中心对应 Client ID
String clientId = "";
// 开发者中心对应 Client Token
String clientToken = "";
TapTapSdkOptions tapSdkOptions = new TapTapSdkOptions(
clientId, // 游戏 Client ID
clientToken, // 游戏 Client Token
TapTapRegion.CN // 游戏可玩区域
);
// 初始化 TapSDK
TapTapSdk.init(context, tapSdkOptions);
import com.taptap.sdk.core.TapTapSdk
import com.taptap.sdk.core.TapTapSdkOptions
import com.taptap.sdk.core.TapTapRegion
TapTapSdk.init(
context = context,
sdkOptions = TapTapSdkOptions(
clientId = clientId, // 客户端 ID
clientToken = clientToken, // 客户端令牌
region = TapTapRegion.CN
)
)
import TapTapSDK
let options = TapTapSdkOptions()
options.clientId = "your_client_id" // 客户端 ID
options.clientToken = "your_client_token" // 客户端令牌
options.region = .CN
TapTapSDK.initWith(options)
#import <TapTapSDK/TapTapSDK.h>
TapTapSdkOptions *options = [[TapTapSdkOptions alloc] init];
options.clientId = @"your_client_id"; // 客户端 ID
options.clientToken = @"your_client_token"; // 客户端令牌
options.region = TapTapRegionCN;
[TapTapSDK initWithOptions:options];
在游戏登录页面,当玩家点击登录按钮后,调用 SDK 登录接口,获取用户信息:
- Unity
- Android Java
- Android Kotlin
- iOS Swift
- iOS Objective-C
using TapSDK.Login;
try
{
// 定义授权范围
List<string> scopes = new List<string>
{
TapTapLogin.TAP_LOGIN_SCOPE_PUBLIC_PROFILE
};
// 发起 Tap 登录
var userInfo = await TapTapLogin.Instance.LoginWithScopes(scopes.ToArray());
Debug.Log($"登录成功,当前用户 ID:{userInfo.unionId}");
// TODO: 进入游戏大厅页面
}
catch (TaskCanceledException)
{
Debug.Log("用户取消登录");
}
catch (Exception exception)
{
Debug.Log($"登录失败,出现异常:{exception}");
}
import com.taptap.sdk.login.TapTapLogin;
import com.taptap.sdk.login.callback.TapTapLoginCallback;
import com.taptap.sdk.login.model.TapUserInfo;
import com.taptap.sdk.kit.internal.exception.TapError;
// 定义授权范围
String[] scopes = new String[]{TapTapLogin.SCOPE_PUBLIC_PROFILE};
// 发起 Tap 登录
TapTapLogin.loginWithScopes(activity, scopes, new TapTapLoginCallback() {
@Override
public void onSuccess(TapUserInfo userInfo) {
Log.d("Login", "登录成功,当前用户 ID:" + userInfo.unionId);
// TODO: 进入游戏大厅页面
}
@Override
public void onCancel() {
Log.d("Login", "用户取消登录");
}
@Override
public void onError(TapError error) {
Log.e("Login", "登录失败,出现异常:" + error.message);
}
});
import com.taptap.sdk.login.TapTapLogin
import com.taptap.sdk.login.callback.TapTapLoginCallback
import com.taptap.sdk.login.model.TapUserInfo
import com.taptap.sdk.kit.internal.exception.TapError
import android.util.Log
// 定义授权范围
val scopes = arrayOf(TapTapLogin.SCOPE_PUBLIC_PROFILE)
// 发起 Tap 登录
TapTapLogin.loginWithScopes(activity, scopes, object : TapTapLoginCallback {
override fun onSuccess(userInfo: TapUserInfo) {
Log.d("Login", "登录成功,当前用户 ID:${userInfo.unionId}")
// TODO: 进入游戏大厅页面
}
override fun onCancel() {
Log.d("Login", "用户取消登录")
}
override fun onError(error: TapError) {
Log.e("Login", "登录失败,出现异常:${error.message}")
}
})
import TapTapSDK
// 定义授权范围
let scopes = [TapTapLogin.scopePublicProfile]
// 发起 Tap 登录
TapTapLogin.login(with: scopes) { userInfo, error in
if let error = error {
print("登录失败,出现异常:\(error.localizedDescription)")
} else if let userInfo = userInfo {
print("登录成功,当前用户 ID:\(userInfo.unionId)")
// TODO: 进入游戏大厅页面
}
}
#import <TapTapSDK/TapTapSDK.h>
// 定义授权范围
NSArray *scopes = @[TapTapLogin.scopePublicProfile];
// 发起 Tap 登录
[TapTapLogin loginWithScopes:scopes callback:^(TapUserInfo *userInfo, NSError *error) {
if (error) {
NSLog(@"登录失败,出现异常:%@", error.localizedDescription);
} else {
NSLog(@"登录成功,当前用户 ID:%@", userInfo.unionId);
// TODO: 进入游戏大厅页面
}
}];
游戏大厅页
进入大厅后,先注册全局 SDK 回调,然后调用建立连接接口,示例如下:
- Unity
- Android Java
- Android Kotlin
- iOS Swift
- iOS Objective-C
using TapSDK.OnlineBattle;
/// 大厅页面,实现 SDK 回调接口
public class LobbyPanel : MonoBehaviour , ITapOnlineBattleCallback
{
/// 注册回调
public async void RegisterCallback()
{
TapTapOnlineBattle.RegisterOnlineBattleCallback(this);
}
/// 建立连接
public async void Connect()
{
try
{
string playerId = await TapTapOnlineBattle.Connect();
LogMessage("Connect success " + playerId);
}
catch (TapException e)
{
LogMessage($"Connect error {e.Code} {e.Message}");
}
}
public void OnPlayerEntered(string roomId, TapSDK.OnlineBattle.PlayerInfo playerInfo)
{
// 有玩家进入房间,根据 playerInfo 渲染房间内玩家 UI
}
public void OnPlayerLeft(string roomId, string roomOwnerId, string playerId)
{
// 有玩家离开房间,根据 playerID 移除房间内玩家 UI
}
public void OnRoomPropertiesChanged(string id, string name, string customProperties)
{
// 房主修改了房间属性,根据数据修改房间内页面 UI
}
public void OnCustomMessageReceived(string playerId, string msg)
{
// 收到玩家操作,根据 msg 中玩家操作数据渲染对战页面棋 盘 UI
}
public void OnPlayerCustomStatusChanged(string playerId, int status)
{
// 玩家更改了对战就绪状态,更新房间内 UI
}
}
import com.taptap.sdk.battle.TapTapBattle;
import com.taptap.sdk.battle.BattleNotificationCallback;
import com.taptap.sdk.battle.ConnectListener;
import com.taptap.sdk.battle.model.SignInResponse;
import com.taptap.sdk.battle.model.ErrorResponse;
import com.taptap.sdk.battle.model.EnterRoomNotification;
import com.taptap.sdk.battle.model.LeaveRoomNotification;
import com.taptap.sdk.battle.model.RoomPropertiesNotification;
import com.taptap.sdk.battle.model.CustomMessageNotification;
import com.taptap.sdk.battle.model.PlayerCustomStatusNotification;
// 大厅页面,实现 SDK 回调接口
public class LobbyActivity extends AppCompatActivity implements BattleNotificationCallback {
// 注册回调
public void registerCallback() {
TapTapBattle.registerOnlineBattleCallback(this);
}
// 建立连接
public void connect() {
TapTapBattle.connect(new ConnectListener() {
@Override
public void onSuccess(SignInResponse response) {
Log.d("Battle", "Connect success " + response.getPlayerId());
}
@Override
public void onFailure(ErrorResponse error) {
Log.e("Battle", "Connect error " + error.getCode() + " " + error.getMsg());
}
});
}
@Override
public void onPlayerEntered(EnterRoomNotification notification) {
// 有玩家进入房间,根据 notification.playerInfo 渲染房间内玩家 UI
}
@Override
public void onPlayerLeft(LeaveRoomNotification notification) {
// 有玩家离开房间,根据 notification.playerId 移除房间内玩家 UI
}
@Override
public void onRoomPropertiesChanged(RoomPropertiesNotification notification) {
// 房主修改了房间属性,根据 notification.customProperties 修改房间内页面 UI
}
@Override
public void onCustomMessageReceived(CustomMessageNotification notification) {
// 收到玩家操作,根据 notification.msg 中玩家操作数据渲染对战页面棋盘 UI
}
@Override
public void onPlayerCustomStatusChanged(PlayerCustomStatusNotification notification) {
// 玩家更改了对战就绪状态,更新房间内 UI
}
}
import com.taptap.sdk.battle.TapTapBattle
import com.taptap.sdk.battle.BattleNotificationCallback
import com.taptap.sdk.battle.ConnectListener
import com.taptap.sdk.battle.model.SignInResponse
import com.taptap.sdk.battle.model.ErrorResponse
import com.taptap.sdk.battle.model.EnterRoomNotification
import com.taptap.sdk.battle.model.LeaveRoomNotification
import com.taptap.sdk.battle.model.RoomPropertiesNotification
import com.taptap.sdk.battle.model.CustomMessageNotification
import com.taptap.sdk.battle.model.PlayerCustomStatusNotification
import android.util.Log
// 大厅页面,实现 SDK 回调接口
class LobbyActivity : AppCompatActivity(), BattleNotificationCallback {
// 注册回调
fun registerCallback() {
TapTapBattle.registerOnlineBattleCallback(this)
}
// 建立连接
fun connect() {
TapTapBattle.connect(object : ConnectListener {
override fun onSuccess(response: SignInResponse) {
Log.d("Battle", "Connect success ${response.playerId}")
}
override fun onFailure(error: ErrorResponse) {
Log.e("Battle", "Connect error ${error.code} ${error.msg}")
}
})
}
override fun onPlayerEntered(notification: EnterRoomNotification) {
// 有玩家进入房间,根据 notification.playerInfo 渲染房间内玩家 UI
}
override fun onPlayerLeft(notification: LeaveRoomNotification) {
// 有玩家离开房间,根据 notification.playerId 移除房间内玩家 UI
}
override fun onRoomPropertiesChanged(notification: RoomPropertiesNotification) {
// 房主修改了房间属性,根据 notification.customProperties 修改房间内页面 UI
}
override fun onCustomMessageReceived(notification: CustomMessageNotification) {
// 收到玩家操作,根据 notification.msg 中玩家操作数据渲染对战页面棋盘 UI
}
override fun onPlayerCustomStatusChanged(notification: PlayerCustomStatusNotification) {
// 玩家更改了对战就绪状态,更新房间内 UI
}
}
import TapTapBattleSDK
// ConnectListener 实现
class MyConnectListener: ConnectListener {
func onSuccess(response: SignInResponse) {
print("Connect success \(response.playerId)")
}
func onFailure(error: ErrorResponse) {
print("Connect error \(error.code) \(error.msg)")
}
}
// 大厅页面,实现 SDK 回调接口
class LobbyViewController: UIViewController, BattleNotificationCallback {
// 注册回调
func registerCallback() {
TapTapBattle.registerBattleNotificationListener(self)
}
// 建立连接
func connect() {
TapTapBattle.connect(listener: MyConnectListener())
}
func onPlayerEntered(notification: EnterRoomNotification) {
// 有玩家进入房间,根据 notification.playerInfo 渲染房间内玩家 UI
}
func onPlayerLeft(notification: LeaveRoomNotification) {
// 有玩家离开房间,根据 notification.playerId 移除房间内玩家 UI
}
func onRoomPropertiesChanged(notification: RoomPropertiesNotification) {
// 房主修改了房间属性,根据 notification.customProperties 修改房间内页面 UI
}
func onCustomMessageReceived(notification: CustomMessageNotification) {
// 收到玩家操作,根据 notification.msg 中玩家操作数据渲染 对战页面棋盘 UI
}
func onPlayerCustomStatusChanged(notification: PlayerCustomStatusNotification) {
// 玩家更改了对战就绪状态,更新房间内 UI
}
}
#import <TapTapBattleSDK/TapTapBattleSDK.h>
@interface LobbyViewController : UIViewController <BattleNotificationCallback>
@end
@implementation LobbyViewController
// 注册回调
- (void)registerCallback {
[TapTapBattle registerBattleNotificationListener:self];
}
// 建立连接
- (void)connect {
MyConnectListener *listener = [[MyConnectListener alloc] init];
[TapTapBattle connectWithListener:listener];
}
// SDK 回调实现(所有方法均可选)
// 玩家进入房间
- (void)onPlayerEntered:(EnterRoomNotification *)notification {
// 有玩家进入房间,根据 playerInfo 渲染房间内玩家 UI
}
// 玩家离开 房间
- (void)onPlayerLeft:(LeaveRoomNotification *)notification {
// 有玩家离开房间,根据 notification.playerId 移除房间内玩家 UI
}
// 房间属性变更
- (void)onRoomPropertiesChanged:(RoomPropertiesNotification *)notification {
// 房主修改了房间属性,根据数据修改房间内页面 UI
}
// 收到玩家操作
- (void)onCustomMessageReceived:(CustomMessageNotification *)notification {
// 收到玩家操作,根据 notification.msg 中玩家操作数据渲染对战页面棋盘 UI
}
// 玩家更新自定义状态
- (void)onPlayerCustomStatusChanged:(PlayerCustomStatusNotification *)notification {
// 玩家更改了对战就绪状态,更新房间内 UI
}
@end
根据玩家选择,调用匹配房间或搜索房间列表来加入房间:
- Unity
- Android Java
- Android Kotlin
- iOS Swift
- iOS Objective-C
using TapSDK.OnlineBattle;
/// 匹配房间
public async void MatchRoom(){
Dictionary<string, string> matchParams; // 匹配参数
// TODO: 设置具体匹配参数
var config = new MatchRoomConfig()
{
roomConfig = new RoomConfig()
{
maxPlayerCount = roomMaxPlayerCount, // 最大玩家数量
type = roomType, // 房间类型
matchParams = matchParams, // 匹配参数
}
playerConfig = new PlayerConfig()
{
customStatus = playerCustomStatus, // 玩家状态,自定义: 0 未就绪 1 已就绪
customProperties = playerProperty, // 玩家属性,设置昵称、头像等
},
};
try
{
RoomInfo data = await TapTapOnlineBattle.MatchRoom(config);
LogMessage(" 匹配房间成功 " + JsonConvert.SerializeObject(data));
// TODO: 跳转到房间内页面
}
catch (TapException e)
{
LogMessage($"匹配房间失败 error {e.Code} {e.Message}");
}
}
// 获取房间列表数据
public async void GetRoomList()
{
try
{
RoomListData data = await TapTapOnlineBattle.GetRoomList(
queryRoomType, // 房间类型
);
LogMessage(" 查询房间列表成功 " + JsonConvert.SerializeObject(data));
// TODO: 跳转到房间列表页面,展示可选房间数据
}
catch (TapException e)
{
LogMessage($"查询房间列表 error {e.Code} {e.Message}");
}
}
// 根据房间 ID 加入房间,当玩家在房间列表选择房间后,调用该方法加入房间
public async void JoinRoom()
{
/// 当前玩家信息配置
var playerConfig = new PlayerConfig()
{
customStatus = joinPlayerStatus, // 玩家状态,自定义: 0 未就绪 1 已就绪
customProperties = joinPlayerProperty, // 玩家信息,例如头像、昵称等
};
try
{
RoomInfo data = await TapTapOnlineBattle.JoinRoom(
joinRoomId, // 房间 ID
playerConfig
);
LogMessage(" 加入房 间成功 " + JsonConvert.SerializeObject(data));
// TODO: 跳转到房间内页面, 根据 RoomInfo 的 players 字段获取所有玩家信息
}
catch (TapException e)
{
LogMessage($"加入房间 error {e.Code} {e.Message}");
}
}
import com.taptap.sdk.battle.TapTapBattle;
import com.taptap.sdk.battle.MatchRoomListener;
import com.taptap.sdk.battle.GetRoomListListener;
import com.taptap.sdk.battle.JoinRoomListener;
import com.taptap.sdk.battle.model.MatchRoomConfig;
import com.taptap.sdk.battle.model.RoomConfig;
import com.taptap.sdk.battle.model.PlayerConfig;
import com.taptap.sdk.battle.model.MatchRoomResponse;
import com.taptap.sdk.battle.model.GetRoomListRequest;
import com.taptap.sdk.battle.model.GetRoomListResponse;
import com.taptap.sdk.battle.model.JoinRoomRequest;
import com.taptap.sdk.battle.model.JoinRoomResponse;
import com.taptap.sdk.battle.model.ErrorResponse;
import java.util.Map;
import java.util.HashMap;
// 匹配房间
public void matchRoom() {
Map<String, String> matchParams = new HashMap<>(); // 匹配参数
// TODO: 设置具体匹配参数
RoomConfig roomConfig = new RoomConfig(
roomMaxPlayerCount, // 最大玩家数量
roomType, // 房间类型
matchParams, // 匹配参数
"", // 房间名称(选填)
"" // 自定义房间属性(选填)
);
PlayerConfig playerConfig = new PlayerConfig(
playerCustomStatus, // 玩家状态,自定义: 0 未就绪 1 已就绪
playerProperty // 玩家属性,设置昵称、头像等
);
MatchRoomConfig config = new MatchRoomConfig(roomConfig, playerConfig);
TapTapBattle.matchRoom(config, new MatchRoomListener() {
@Override
public void onSuccess(MatchRoomResponse response) {
Log.d("Battle", "匹配房间成功 " + response.getRoomInfo().toString());
// TODO: 跳转到房间内页面
}
@Override
public void onFailure(ErrorResponse error) {
Log.e("Battle", "匹配房间失败 error " + error.getCode() + " " + error.getMsg());
}
});
}
// 获取房间列表数据
public void getRoomList() {
GetRoomListRequest request = new GetRoomListRequest(queryRoomType, 0, 20);
TapTapBattle.getRoomList(request, new GetRoomListListener() {
@Override
public void onSuccess(GetRoomListResponse response) {
Log.d("Battle", "查询房间列表成功 " + response.toString());
// TODO: 跳转到房间列表页面,展示可选房间数据
}
@Override
public void onFailure(ErrorResponse error) {
Log.e("Battle", "查询房间列表 error " + error.getCode() + " " + error.getMsg());
}
});
}
// 根据房间 ID 加入房间,当玩家在房间列表选择房间后,调用该方法加入房间
public void joinRoom() {
// 当前玩家信息配置
PlayerConfig playerConfig = new PlayerConfig(
joinPlayerStatus, // 玩家状态,自定义: 0 未就绪 1 已就绪
joinPlayerProperty // 玩家信息,例如头像、昵称等
);
JoinRoomRequest joinRequest = new JoinRoomRequest(joinRoomId, playerConfig);
TapTapBattle.joinRoom(joinRequest, new JoinRoomListener() {
@Override
public void onSuccess(JoinRoomResponse response) {
Log.d("Battle", "加入房间成功 " + response.getRoomInfo().toString());
// TODO: 跳转到房间内页面, 根据 RoomInfo 的 players 字段获取所有玩家信息
}
@Override
public void onFailure(ErrorResponse error) {
Log.e("Battle", "加入房间 error " + error.getCode() + " " + error.getMsg());
}
});
}
import com.taptap.sdk.battle.TapTapBattle
import com.taptap.sdk.battle.MatchRoomListener
import com.taptap.sdk.battle.GetRoomListListener
import com.taptap.sdk.battle.JoinRoomListener
import com.taptap.sdk.battle.model.MatchRoomConfig
import com.taptap.sdk.battle.model.RoomConfig
import com.taptap.sdk.battle.model.PlayerConfig
import com.taptap.sdk.battle.model.MatchRoomResponse
import com.taptap.sdk.battle.model.GetRoomListRequest
import com.taptap.sdk.battle.model.GetRoomListResponse
import com.taptap.sdk.battle.model.JoinRoomRequest
import com.taptap.sdk.battle.model.JoinRoomResponse
import com.taptap.sdk.battle.model.ErrorResponse
import android.util.Log
// 匹配房间
fun matchRoom() {
val matchParams: MutableMap<String, String> = mutableMapOf() // 匹配参数
// TODO: 设置具体匹配参数
val roomConfig = RoomConfig(
maxPlayerCount = roomMaxPlayerCount, // 最大玩家数量
type = roomType, // 房间类型
matchParams = matchParams // 匹配参数
)
val playerConfig = PlayerConfig(
customStatus = playerCustomStatus, // 玩家状态,自定义: 0 未就绪 1 已就绪
customProperties = playerProperty // 玩家属性,设置昵称、头像等
)
val config = MatchRoomConfig(roomCfg = roomConfig, playerCfg = playerConfig)
TapTapBattle.matchRoom(config, object : MatchRoomListener {
override fun onSuccess(response: MatchRoomResponse) {
Log.d("Battle", "匹配房间成功 ${response.roomInfo}")
// TODO: 跳转到房间内页面
}
override fun onFailure(error: ErrorResponse) {
Log.e("Battle", "匹配房间失败 error ${error.code} ${error.msg}")
}
})
}
// 获取房间列表数据
fun getRoomList() {
val request = GetRoomListRequest(roomType = queryRoomType, offset = 0, limit = 20)
TapTapBattle.getRoomList(request, object : GetRoomListListener {
override fun onSuccess(response: GetRoomListResponse) {
Log.d("Battle", "查询房间列表成功 $response")
// TODO: 跳转到房间列表页面,展示可选房间数据
}
override fun onFailure(error: ErrorResponse) {
Log.e("Battle", "查询房间列表 error ${error.code} ${error.msg}")
}
})
}
// 根据房间 ID 加入房间,当玩家在房间列表选择房间后,调用该方法加入房间
fun joinRoom() {
// 当前玩家信息配置
val playerConfig = PlayerConfig(
customStatus = joinPlayerStatus, // 玩家状态,自定义: 0 未就绪 1 已就绪
customProperties = joinPlayerProperty // 玩家信息,例如头像、昵称等
)
val joinRequest = JoinRoomRequest(roomId = joinRoomId, playerCfg = playerConfig)
TapTapBattle.joinRoom(joinRequest, object : JoinRoomListener {
override fun onSuccess(response: JoinRoomResponse) {
Log.d("Battle", "加入房间成功 ${response.roomInfo}")
// TODO: 跳转到房间内页面, 根据 RoomInfo 的 players 字段获取所有玩家信息
}
override fun onFailure(error: ErrorResponse) {
Log.e("Battle", "加入房间 error ${error.code} ${error.msg}")
}
})
}
import TapTapBattleSDK
// 匹配房间
class MyMatchRoomListener: MatchRoomListener {
func onSuccess(response: MatchRoomResponse) {
print("匹配房间成功 \(response.roomInfo)")
// TODO: 跳转到房间内页面
}
func onFailure(error: ErrorResponse) {
print("匹配房间失败 error \(error.code) \(error.msg)")
}
}
func matchRoom() {
var matchParams: [String: String] = [:] // 匹配参数
// TODO: 设置具体匹配参数
let roomConfig = RoomConfig(
maxPlayerCount: roomMaxPlayerCount, // 最大玩家数量
type: roomType, // 房间类型
matchParams: matchParams // 匹配参数
)
let playerConfig = PlayerConfig(
customStatus: playerCustomStatus, // 玩家状态,自定义: 0 未就绪 1 已就绪
customProperties: playerProperty // 玩家属性,设置昵称、头像等
)
let request = MatchRoomRequest(roomCfg: roomConfig, playerCfg: playerConfig)
TapTapBattle.matchRoom(request: request, listener: MyMatchRoomListener())
}
// 获取房间列表数据
class MyGetRoomListListener: GetRoomListListener {
func onSuccess(response: GetRoomListResponse) {
print("查询房间列表成功 \(response)")
// TODO: 跳转到房间列表页面,展示可选房间数据
}
func onFailure(error: ErrorResponse) {
print("查询房间列表 error \(error.code) \(error.msg)")
}
}
func getRoomList() {
let request = GetRoomListRequest(roomType: queryRoomType, offset: 0, limit: 20)
TapTapBattle.getRoomList(request: request, listener: MyGetRoomListListener())
}
// 根据房间 ID 加入房间,当玩家在房间列表选择房间后,调用该方法加入房间
class MyJoinRoomListener: JoinRoomListener {
func onSuccess(response: JoinRoomResponse) {
print("加入房间成功 \(response.roomInfo)")
// TODO: 跳转到房间内页面, 根据 RoomInfo 的 players 字段获取所有玩家信息
}
func onFailure(error: ErrorResponse) {
print("加入房间 error \(error.code) \(error.msg)")
}
}
func joinRoom() {
// 当前玩家信息配置
let playerConfig = PlayerConfig(
customStatus: joinPlayerStatus, // 玩家状态,自定义: 0 未就绪 1 已就绪
customProperties: joinPlayerProperty // 玩家信息,例如头像、昵称等
)
let joinRequest = JoinRoomRequest(roomId: joinRoomId, playerCfg: playerConfig)
TapTapBattle.joinRoom(request: joinRequest, listener: MyJoinRoomListener())
}
#import <TapTapBattleSDK/TapTapBattleSDK.h>
// 匹配房间
- (void)matchRoom {
RoomConfig *roomConfig = [[RoomConfig alloc] initWithMaxPlayerCount:roomMaxPlayerCount
type:roomType
matchParams:matchParams
name:@""
customProperties:@""];
PlayerConfig *playerConfig = [[PlayerConfig alloc] initWithCustomStatus:playerCustomStatus
customProperties:playerProperty];
MatchRoomRequest *request = [[MatchRoomRequest alloc] initWithRoomCfg:roomConfig playerCfg:playerConfig];
[TapTapBattle matchRoomWithRequest:request listener:[[MyMatchRoomListener alloc] init]];
}
// 获取房间列表数据
- (void)getRoomList {
GetRoomListRequest *request = [[GetRoomListRequest alloc] initWithRoomType:queryRoomType offset:0 limit:20];
[TapTapBattle getRoomListWithRequest:request listener:[[MyGetRoomListListener alloc] init]];
}
// 根据房间 ID 加入房间
- (void)joinRoom {
PlayerConfig *playerConfig = [[PlayerConfig alloc] initWithCustomStatus:joinPlayerStatus
customProperties:joinPlayerProperty];
JoinRoomRequest *request = [[JoinRoomRequest alloc] initWithRoomId:joinRoomId playerCfg:playerConfig];
[TapTapBattle joinRoomWithRequest:request listener:[[MyJoinRoomListener alloc] init]];
}