SDKBOX Cocos Creator GPG NearbyConnections PayloadCallback only called once

Hi,

I was testing GPG 2.6.0.1 Nearby Connections API in Cocos Creator and I have been using this sample as reference, but I noticed that the payload callback is only called once when calling SendUnreliableMessage (or SendReliableMessage) many times. Subsequent calls to SendUnreliableMessage does not trigger the payload callback whether its the discoverer or the advertiser.
I checked the code for sdkboxgpg.js and in the function nativeNotify the callback is removed after one execution if the id is > 1000, which is the case with Nearby Connections callbacks because they are added using addCallback which uses incremented callback id starting from 1000.
If I comment the line below that removes the callback for id > 1000, the payload callback is called every time as expected.

    // callbacks that are temporary one shot calls have to be removed.
    if ( id>=1000 ) {
        this._callbacks[id] = null;
    }

Can someone check this issue?

Thanks a lot.

Majiho

this call is responsive, so callback can only be used once.

My understanding is that the payload/message callback provided to AcceptConnectionRequest and SendConnectionRequest should be called for each message received with a SendUnreliableMessage (or Reliable) call as below.

//advertiser side
gameServices().NearbyConnections.AcceptConnectionRequest(
    remote_endpoint_id,
    payload,
    function(result) {
        //payload callback
    }
);
// discoverer side
gameServices().NearbyConnections.SendConnectionRequest(
    name, remote_endpoint_id, payload,
    function(result) {
         //connect_response_callback
    },
    function(result) {
         //payload callback
    }
);

I do not see any other way to specify payload callback.

If we look at the code for both function, it use addCallback.

    AcceptConnectionRequest : function(remote_endpoint_id, payload, callback) {
        _gpg.GPGNearbyConnectionsWrapper.AcceptConnectionRequest(remote_endpoint_id,
            payload,
            __callbackManager.addCallback(callback));
    },

    SendConnectionRequest : function(name, remote_endpoint_id, payload, connect_response_callback, message_callback) {
            _gpg.GPGNearbyConnectionsWrapper.SendConnectionRequest(name,
            remote_endpoint_id, payload,
            __callbackManager.addCallback(connect_response_callback),
            __callbackManager.addCallback(message_callback));
    },

addCallback then get the index which is incremented from the initial index of 1000.

        index= this.__nextIndex();
        this.addCallbackById(index, callback);

Then, when the payload is received, the callback is executed, but removed just after if index > 1000.

nativeNotify: function (id, str_json) {
    if ( this._callbacks[id] ) {
        this._callbacks[id](JSON.parse(str_json));
    }
    // callbacks that are temporary one shot calls have to be removed.
    if ( id>=1000 ) {
        this._callbacks[id] = null;
    }
},

Shouldn’t addCallbackId be used instead of addCallback, with an index < 1000?
If we look at SetOnAuthActionStarted signin notification function, it uses addCallbackId with index < 1000 and therefore the callback is kept for future executions.

        this.SetOnAuthActionStarted = function (authActionStartedCallback) {
            __callbackManager.addCallbackById(DefaultCallbacks.AUTH_ACTION_STARTED, authActionStartedCallback);
            return this;
        };

Is it not a case of doing the same for AcceptConnectionRequest and SendConnectionRequest like below?

    AcceptConnectionRequest : function(remote_endpoint_id, payload, callback) {
        _gpg.GPGNearbyConnectionsWrapper.AcceptConnectionRequest(remote_endpoint_id,
            payload,
            __callbackManager.addCallbackById(DefaultCallbacks.PAYLOAD_RECEIVED, callback));
    },

    SendConnectionRequest : function(name, remote_endpoint_id, payload, connect_response_callback, message_callback) {
            _gpg.GPGNearbyConnectionsWrapper.SendConnectionRequest(name,
            remote_endpoint_id, payload,
            __callbackManager.addCallback(connect_response_callback),
            __callbackManager.addCallbackById(DefaultCallbacks.PAYLOAD_RECEIVED, message_callback));
    },

var DefaultCallbacks = {
    DEFAULT_CALLBACKS_BEGIN : 1,
    AUTH_ACTION_STARTED : 1,
    ...
    MULTIPLAYER_INVITATION_EVENT : 10,

    // define callback id for payload received
    PAYLOAD_RECEIVED : 11

    DEFAULT_CALLBACKS_END : 11
};

I see what you mean. I think the program should be designed in a question-and-answer format.

We’ll fix it here. Thank you.

1 Like

Hi,

I was just wondering if there was any update on this? I’m hoping to release a multiplayer gaming feature in my existing game, which would be great.

I don’t see a problem with a question/answer format but the current sdkbox API is not allowing this flow.
As far as I can tell, the SDKBOX API is based on the Android API where the connections and callbacks are created only once at the beginning. Any messages exchanged between devices will trigger the payload callbacks.

Also, if we were to apply the changes as per my suggestion above, is it possible to avoid resetting the callbacks for nearby connection if player failed to sign in to gpg, because players do not need to be signed in to use the nearby connections API.

Thanks

we are handing this.you can fix it according to your own needs for temporary

1 Like