AlexeyGorovoy AlexeyGorovoy - 1 year ago 82
Android Question

Real-time messaging not working when players get connected via invitation

I'm developing a simple game for two players with Real-time multiplayer provided by Google Play Services and libGDX framework. Basically I'm using code snippets from google's tutorial.

When players get connected via auto-match function all works perfectly, but when I try to accept invitation something is going wrong. All the necessary callbacks are called with status code STATUS_OK, but the player who accepted the invitation can't send a reliable message to other. While the player who sent this invitation can send messages, and they arrive on the second device.

Are there any known issues about GPGS + libGDX? Maybe threading matters (in which thread I call methods like

etc) in this case?

I'm pretty sure all my code is similar to the link above, because I even tried to implement a new app from scratch with GPGS+libGDX, when failed to add this functionality to my existent game.

Also I have tested both my apps on multiple devices, and always got the same:

  • auto-match - everything just fine;

  • invite - works in "one direction" only: invitee can't send messages, but receives them fine, no errors in logs or callbacks.

Example output from the perspective of the player who accepted invitation:

08-11 18:35:52.219 14173-14173/com.teremok.taptapparty D/PartyRoomUpdateListener: onJoinedRoom - success
08-11 18:35:52.219 14173-14173/com.teremok.taptapparty D/PartyRoomUpdateListener: Room participants:
08-11 18:35:52.219 14173-14173/com.teremok.taptapparty D/PartyRoomUpdateListener: HateCrub - p_CMCMr4mRp_K63QEQAQ
08-11 18:35:52.219 14173-14173/com.teremok.taptapparty D/PartyRoomUpdateListener: AlexeyGorovoy - p_CMCMr4mRp_K63QEQAg
08-11 18:35:52.219 14173-14173/com.teremok.taptapparty D/PartyRoomStatusUpdateListener: onPeerJoined - [p_CMCMr4mRp_K63QEQAQ]
08-11 18:35:53.979 14173-14173/com.teremok.taptapparty D/PartyRoomStatusUpdateListener: onP2PConnected - p_CMCMr4mRp_K63QEQAQ
08-11 18:35:54.419 14173-14173/com.teremok.taptapparty D/PartyRoomStatusUpdateListener: onConnectedToRoom
08-11 18:35:54.429 14173-14173/com.teremok.taptapparty D/PartyRoomStatusUpdateListener: onPeersConnected - [p_CMCMr4mRp_K63QEQAQ]
08-11 18:35:54.439 14173-14173/com.teremok.taptapparty D/PartyRoomUpdateListener: onRoomConnected - success
08-11 18:35:54.439 14173-14173/com.teremok.taptapparty D/AndroidMultiplayer: sending message: hello#AlexeyGorovoy#
08-11 18:35:54.439 14173-14173/com.teremok.taptapparty D/AndroidMultiplayer: message sent failed - network error (STATUS_REAL_TIME_MESSAGE_SEND_FAILED)
08-11 18:35:54.779 14173-14173/com.teremok.taptapparty D/PartyMessageReceiver: onMessageReceived: hello#HateCrub#

In this snippet you can see that room is connected, other peer is connected etc, but then this player can't send a message, but successfully receives one.

Answer Source

And after many days I found my silly mistake. I thought that client can get his own participantId as the first entry in participants list.

It's the incorrect way of getting current player's participant id (though works in auto-match):

String myPartId = room.getParticipants().get(0) // I was doing so :(

Here is the correct way:

    String myPlayerId = Games.Players.getCurrentPlayer(gameHelper.getApiClient()).getPlayerId();
    String myPartId = room.getParticipantId(myPlayerId);

I hope this will help someone to avoid such mistakes.