当前位置:网站首页>华为联机对战服务玩家掉线重连案例总结
华为联机对战服务玩家掉线重连案例总结
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(() => {
// 初始化失败
});
边栏推荐
- PCL之滤波
- JSP webshell免殺——JSP的基礎
- HDU1236 排名(结构体排序)
- [pit avoidance guide] pit encountered by unity3d project when accessing Tencent bugly tool
- js promise.all
- 01-spooldir
- UVM learning - object attribute of UVM phase
- Considerations for Apache deploying static web page projects
- "Matching" is true love, a new attitude for young people to make friends
- VSCode工具使用
猜你喜欢

Dialogue Wu Gang: why do I believe in the rise of "big country brands"?

使用Windbg静态分析dump文件(实战经验总结)

从.bag文件中读取并保存.jpg图片和.pcd点云

UVM learning - object attribute of UVM phase

拆解美图SaaS:开着飞机换引擎

Flink submitter

1287_ Implementation analysis of prvtaskistasksuspended() interface in FreeRTOS

The nanny level tutorial of flutter environment configuration makes the doctor green to the end

Sus system availability scale

js数组常用方法
随机推荐
Beautiful and intelligent, Haval H6 supreme+ makes Yuanxiao travel safer
KS009基于SSH实现宠物管理系统
Flink submitter
从MediaRecord录像中读取H264参数
Win11 arm系统配置.net core环境变量
Flutter环境配置保姆级教程,让doctor一绿到底
学习open62541 --- [66] UA_String的生成方法
Flink实时计算topN热榜
快速做出原型
PCL Eigen介绍及简单使用
session-cookie与token
Pywin32打开指定窗口
2022-06-17
Solutions to a series of problems in sqoop job creation
MySQL lethal serial question 3 -- are you familiar with MySQL locks?
[pit avoidance guide] pit encountered using ugui: the text component cannot indent the first line by two spaces
What are the popular frameworks for swoole in 2022?
axis设备的rtsp setup头中的url不能带参
MySQL keyword
lunix重新分配root 和 home 空间内存