当前位置:网站首页>华为联机对战服务玩家掉线重连案例总结
华为联机对战服务玩家掉线重连案例总结
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(() => {
// 初始化失败
});
边栏推荐
猜你喜欢
全网显示 IP 归属地,是怎么实现的?
Open the encrypted SQLite method with sqlcipher
STM32 and motor development (upper system)
stm32和電機開發(上比特系統)
flink 提交程序
Database dictionary Navicat automatic generation version
4.随机变量
Flutter环境配置保姆级教程,让doctor一绿到底
The nanny level tutorial of flutter environment configuration makes the doctor green to the end
拆解美图SaaS:开着飞机换引擎
随机推荐
[tutorial] how to make the Helpviewer help document of VisualStudio run independently
Pywin32 opens the specified window
集成学习概览
js promise. all
UVM learning - build a simple UVM verification platform
Shell programming 01_ Shell foundation
Flink实时计算topN热榜
Disassembling Meitu SaaS: driving the plane to change the engine
JSP webshell免殺——JSP的基礎
01安装虚拟机
(5) Gear control setting of APA scene construction
2. Hacking lab script off [detailed writeup]
Lunix reallocates root and home space memory
全网显示 IP 归属地,是怎么实现的?
flink 提交程序
What are the popular frameworks for swoole in 2022?
UWA报告使用小技巧,你get了吗?(第四弹)
使用sqlcipher打开加密的sqlite方法
转换YV12到RGB565图像转换,附YUV转RGB测试
Is this code PHP MySQL redundant?