如标题所示,我已经能够连接到谷歌游戏服务,在两台设备之间交换数据,一切都运行良好,除了一件事:断开回调。
我试图拦截 onPeersDisconnected 和 onP2PDisconnected 都没有成功。onP2PDisconnected 方法在与 Internet 断开连接的设备中被调用,但在仍然在线的设备中没有被调用(因此无法告诉玩家另一个已断开连接)。
比赛开始后,似乎永远不会通知第二台设备意外断开连接。如果用户正确关闭游戏,则调用 onPeersLeft 方法。
两个设备之间的 ping 真的有必要克服这个“错误”吗?难道我做错了什么?
这是我使用的代码:
void startQuickGame() {
// quick-start a game with 1 randomly selected opponent
final int MIN_OPPONENTS = 1, MAX_OPPONENTS = 1;
Bundle autoMatchCriteria = RoomConfig.createAutoMatchCriteria(MIN_OPPONENTS,
MAX_OPPONENTS, 0);
RoomConfig.Builder rtmConfigBuilder = RoomConfig.builder(this);
rtmConfigBuilder.setMessageReceivedListener(this);
rtmConfigBuilder.setRoomStatusUpdateListener(this);
rtmConfigBuilder.setAutoMatchCriteria(autoMatchCriteria);
mListener.switchToScreen(R.id.screen_wait);
keepScreenOn();
resetGameVars();
getGamesClient().createRoom(rtmConfigBuilder.build());
}
这里是简单的听众:
@Override
public void onPeersDisconnected(Room room, List<String> peers) {
Log.d(TAG, "onPeersDisconnected");
updateRoom(room);
}
void updateRoom(Room room) {
Log.d(TAG, "UpdateRoom: "+room.getParticipants().size());
mParticipants = room.getParticipants();
}
@Override
public void onP2PDisconnected(String participantId) {
Log.d(TAG, "onP2PDisconnected");
}
public int getPartecipantsInRooom(){
if(mRoom != null)
return mRoom.getParticipants().size();
else
return -123456;
}
请注意,在两个设备之一断开连接后调用 getPartecipantsInRooom() 始终返回 2,并且永远不会调用 updateRoom。