当前位置:网站首页>华为联机对战服务玩家掉线重连案例总结
华为联机对战服务玩家掉线重连案例总结
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(() => {
// 初始化失败
});
边栏推荐
- nodejs+express+mysql简单博客搭建
- Windows环境MySQL8忘记密码文件解决方案
- 转换YV12到RGB565图像转换,附YUV转RGB测试
- (五)APA场景搭建之挡位控制设置
- 1287_ Implementation analysis of prvtaskistasksuspended() interface in FreeRTOS
- [pit avoidance guide] pit encountered by unity3d project when accessing Tencent bugly tool
- 使用sqlcipher打开加密的sqlite方法
- SQOOP 1.4.6 INSTALL
- 传输优化抽象
- STM32 and motor development (upper system)
猜你喜欢
随机推荐
1287_ Implementation analysis of prvtaskistasksuspended() interface in FreeRTOS
13.信号量临界区保护
Excuse me, is it cost-effective to insure love life patron saint 2.0 increased lifelong life insurance? What are the advantages of this product?
1287_FreeRTOS中prvTaskIsTaskSuspended()接口实现分析
js promise.all
HDU1234 开门人和关门人(水题)
MongoDB 学习整理(条件操作符,$type 操作符,limit()方法,skip() 方法 和 sort() 方法)
UWA report uses tips. Did you get it? (the fourth bullet)
全网显示 IP 归属地,是怎么实现的?
Is this code PHP MySQL redundant?
[tutorial] how to make the Helpviewer help document of VisualStudio run independently
In the face of uncertainty, the role of supply chain
4.随机变量
Thanos Receiver
SUS系统可用性量表
MySQL environment configuration
Kustomize user manual
13. Semaphore critical zone protection
JSP webshell免殺——JSP的基礎
Operator-1 first acquaintance with operator









