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
};