1
\$\begingroup\$

I'm looking for some idea of how to handle Reliable Messages in Google Play Services with libGDX. So far I've been using it like this: I was waiting for message with OnRealTimeMessageReceivedListener, setting an outside byte[] array to the message received, changing a flag that suggests that new message arrived to true, getting that message in my libGDX class using methods from interface, setting newMessage flag again to false, handling message, and so again waiting for new one

private OnRealTimeMessageReceivedListener mMessageRecievedHandler = new OnRealTimeMessageReceivedListener() {
        @Override
        public void onRealTimeMessageReceived(@NonNull RealTimeMessage realTimeMessage) {

            //Handle received message here


            byte[] message = realTimeMessage.getMessageData();

            /*byte tbye[] = message;
            ByteBuffer bb = ByteBuffer.wrap(tbye);
            if(bb.getFloat() == 1884) {
                Log.d("MESSAGETEST", "Message received in anL : " + debugIntL);
                debugIntL++;
            } */

            byteArray = message;

            newMessageFlag = true;
        }
    };

@Override
    public boolean getNewMessageFlag() {
        return newMessageFlag;
    }


@Override
    public byte[] getMessage() {
        newMessageFlag = false;
        return byteArray;
    }

Libgdx class:

@Override
    public void render (float delta) {
    ...
    checkForNewMessage();
    ...
   }

private void checkForNewMessage() {

    if(game.customHandler.getNewMessageFlag()) {
        handleMessage(); // handling message here
    }

}

private void handleMessage() {

        byte[] message = game.customHandler.getMessage();

        ByteBuffer bb = ByteBuffer.wrap(message);

        float typeOfMessage = bb.getFloat();



        if(typeOfMessage == NEW_UNIT_MESSAGE) {

            Gdx.app.log("MESSAGETEST", "Message received in gdx : " + debugInt);
            debugInt++;
            ...
           }
       }

But the result is, when uncommenting the code in OnRealTimeMessageReceivedListener and adding similar log to my handleMessage(); that some messages are skipped between receiving it in listener and checking for new in my libGDX class, I don't know why, maybe because some render calls take longer to do everything inside and it skips through one check call when there's significant number of those in small amount of time enter image description here

On the image you can see that first log is from OnRealTimeMessageReceivedListener, which receives every message, but the second one is from handleMessage, and at 251 there it wasn't called, so handleMessage just skipped this message and went to the next one.

So here's my question, what's good way to handle those messages so that I won't skip any?

\$\endgroup\$
0
\$\begingroup\$

so if anyone has the same problem i think i found the solution, there's been no problems with that so far, i'll edit this answer if it turns out to have some problems.

Anyway, the solution is as follows, I completely abandoned the idea of checking for new message with flags, and then handling it, instead in main class with OnRealTimeMessageReceivedListener i created private List<byte[]> byteArrayListand changed listener to:

private OnRealTimeMessageReceivedListener mMessageRecievedHandler = new OnRealTimeMessageReceivedListener() {
        @Override
        public void onRealTimeMessageReceived(@NonNull RealTimeMessage realTimeMessage) {

            //Handle received message here


            byte[] message = realTimeMessage.getMessageData();

            byteArrayList.add(message);

        }
    };


@Override
public List<byte[]> getArrayOfMessages() {
    return byteArrayList;
}

Then in the libGDX class instead of checkForNewMessage() in render method i just put the handleMessage()there which looks like this

private void handleMessage() {

        if(!game.customHandler.getArrayOfMessages().isEmpty() && game.customHandler.getArrayOfMessages() != null) {

            byte[] message = game.customHandler.getArrayOfMessages().get(0);
            game.customHandler.getArrayOfMessages().remove(0);

            ByteBuffer bb = ByteBuffer.wrap(message);

             // Rest of handling message...

This way it won't skip anything, because every message is inside a ArrayList, and handleMessage() handles every message that is inside of it one by one.

\$\endgroup\$
  • \$\begingroup\$ Looks like a nice improvement. Thanks for sharing. \$\endgroup\$ – Shad Nov 15 '18 at 4:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.