gpt4 book ai didi

android - Google Game Services sPeers Disconnected/on P2P Disconnected not being called

转载 作者:太空狗 更新时间:2023-10-29 13:28:44 25 4
gpt4 key购买 nike

如标题所示,我已经能够连接到 Google 游戏服务,在两台设备之间交换数据并且一切正常,除了一件事:断开连接回调 .

我试图拦截 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。

最佳答案

为了确保这可能对您不起作用,对于我的应用程序,我使用它来通知我其他参与者何时离开房间,并立即调用它:

@Override
public void onPeerLeft(Room room, final List<String> participantIds) {


this.mRoomCurrent = room;
this.mRoomId = this.mRoomCurrent.getRoomId();
this.mParticipants = this.mRoomCurrent.getParticipants();

int connected = 0;
for (Participant p : room.getParticipants()) {
if(p.getStatus() == Participant.STATUS_JOINED) {
connected += 1;
}
}

final int fconnected = connected;
for (String s : listIgnoreTheseIDs) {
//checkint to see if we care anymore about this ID.. if out of game already.. nope
if(s.equals(participantIds.get(0))){
return;
}
}
Gdx.app.postRunnable(new Runnable() {
@Override
public void run() {
mGHInterface.onPeerLeft(fconnected, participantIds.size());
}
});
}

不知道为什么有两个项目,但像你一样,我意识到 onPeersDisconnected() 并不那么可靠,但 onPeerLeft() 通常会在 1 秒内返回到其他设备。

关于android - Google Game Services sPeers Disconnected/on P2P Disconnected not being called,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18516773/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com