Huawei Online Battle Service Disconnection and Reconnection Solution
During the game, abnormal situations such as network fluctuations often occur, causing players to disconnect. At this time, if the network is restored and the player still wants to join the original game, the following methods can be used to reconnect. Allow reconnection time can be set in the AppGallery Connec console.
Network Abnormal Causes Dropped Scenarios
The network abnormality causes the player client to be unable to connect to the online battle server. After a certain period of time, the server will set the player to be disconnected. If the game allows the player to reconnect to the network after a short period of network recovery, you need to use disconnection The reconnect interface implements this scenario.
Specifically, after the player enters the room, the game monitors the player disconnect event through the room.onDisconnect(playerInfo) method, which returns the player information. It is judged that if the player is disconnected by himself, the room.reconnect() method needs to be triggered to reconnect. If the network has not been restored during the reconnection process, it will always try to reconnect. If the network is restored, it will be processed according to the results. Go to other game pages.
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) {
// 超过允许重连时间退出到其他页面
}
});
}
Closing the client leads to disconnection
After the player closes the client and reopens it, he still wants to reconnect to the previous game. At this time, he can judge whether the player is still in the valid room in the return of the initialization interface. If it is still in the allowed reconnection time, he can use the join room The interface is rejoined. If you don't want to join the previous game at this time, you must call the interface to leave the room before you can create the room again.
client.init().then(() => {
// 初始化成功
if(client.lastRoomId){
// 当前玩家仍在上一房间内,可根据lastRoomId重新加入房间
// 如果不想加入上一个房间内,必须通过client.leaveRoom离开房间,否则新建房间或匹配房间时会报错:玩家已在房间内
}
}).catch(() => {
// 初始化失败
});
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。