当前位置:网站首页>华为联机对战服务玩家掉线重连案例总结
华为联机对战服务玩家掉线重连案例总结
2022-07-02 07:15:00 【华为开发者论坛】
华为联机对战服务断线重连解决方案
游戏过程中,经常会发生网络波动等异常情况,导致玩家掉线,此时如果网络恢复仍希望玩家加入到原游戏中,可以通过下面的方式进行重连。允许重连时间可以在AppGallery Connec控制台进行设置。
网络异常导致掉线场景
网络异常导致玩家客户端与联机对战服务端连接不上,在一定周期后服务器会将该玩家设置会掉线状态,如果游戏允许玩家在短时间内网络恢复后重新接入网络则需要使用掉线重连接口实现该场景。
具体是玩家进入房间后,游戏通过room.onDisconnect(playerInfo)方法监听玩家掉线事件,该方法会返回玩家信息。判断如果是玩家自己掉线,则需要触发room.reconnect()方法重连,重连过程如果网络始终未恢复则一直尝试重连,如果网络恢复则根据结果处理,可能超过允许重连时间则跳转到其他游戏页面。
room.onDisconnect((playerInfo) => {
// 当前玩家断线
if(playerInfo.playerId === room.playerId){
// 重连逻辑
reConnect();
}else{
//其他玩家掉线处理
}
}
reConnect() {
// 调用重连方法进行重连
room.reconnect().then(() => {
//重连成功
}).catch((e) => {
if (!e.code) {
// 网络不通继续重试
this.reConnect();
return;
}
if (e.code != 0) {
// 超过允许重连时间退出到其他页面
}
});
}
关闭客户端导致掉线
玩家关闭客户端重新打开后仍希望重连上一局游戏,此时可以在初始化接口返回中判断玩家是否仍然在有效房间内,如果仍然在说明其仍然在允许重连时间内,可以使用加入房间接口重新加入。如果此时不希望加入上一局游戏,则必须先调用接口离开该房间才能重新正常创建房间。
client.init().then(() => {
// 初始化成功
if(client.lastRoomId){
// 当前玩家仍在上一房间内,可根据lastRoomId重新加入房间
// 如果不想加入上一个房间内,必须通过client.leaveRoom离开房间,否则新建房间或匹配房间时会报错:玩家已在房间内
}
}).catch(() => {
// 初始化失败
});
边栏推荐
- Basic usage of mock server
- UVM learning - object attribute of UVM phase
- [unity3d] nested use layout group to make scroll view with dynamic sub object height
- 01-spooldir
- axis设备的rtsp setup头中的url不能带参
- MySQL lethal serial question 4 -- are you familiar with MySQL logs?
- Shell programming 01_ Shell foundation
- 快速做出原型
- KS009基于SSH实现宠物管理系统
- Flink calculates topn hot list in real time
猜你喜欢
随机推荐
AI技术产业热点分析
记录 AttributeError: ‘NoneType‘ object has no attribute ‘nextcall‘
"Matching" is true love, a new attitude for young people to make friends
正则及常用公式
1287_ Implementation analysis of prvtaskistasksuspended() interface in FreeRTOS
PCL 点云转深度图像
从.bag文件中读取并保存.jpg图片和.pcd点云
Introduction to MySQL 8 DBA foundation tutorial
Beautiful and intelligent, Haval H6 supreme+ makes Yuanxiao travel safer
Sus system availability scale
UWA报告使用小技巧,你get了吗?(第四弹)
PCL Eigen介绍及简单使用
What are the popular frameworks for swoole in 2022?
UVM - configuration mechanism
Importing tables from sqoop
01 install virtual machine
shell编程01_Shell基础
[SUCTF2018]followme
STM32 and motor development (upper system)
[jetbrain rider] an exception occurred in the construction project: the imported project "d:\visualstudio2017\ide\msbuild\15.0\bin\roslyn\microsoft.csh" was not found








