Before you can send a messages from Client Game to Server Game function, you need to:
Fireball uses an async method for delivering messages from client to server and backward. Any message requires having a Name
that is unique for each type of message. Some names (such as authenticate
, session
etc.) are registered by Fireball and can't be used for custom messaging. Any message must be a child class from BaseMessage
that contains a bunch of required fields that are automatically filled up from player's Fireball session on message creation.
BaseRequest
class child:public class SpinRequest : BaseRequest
{
private const string NAME = "spin";
public long Amount;
public SpinRequest(long betAmount, FireballSession session, string customActionID = null)
: base(NAME, session, customActionID)
{
Amount = betAmount;
}
}
BaseResponse
class child:public class SpinResult : BaseResponse
{
private const string NAME = "spin-result";
public List<int> Symbols;
public long WinAmount;
public long Balance;
public bool IsWon;
public SpinResult()
{
Name = NAME;
}
}
fireball.SendRequest
method, where you can specify custom request and response messages types:var spinRequest = new SpinRequest(100, fireball.CurrentSession);
fireball.SendRequest<SpinRequest, SpinResult>(spinRequest, (response) =>
{
// spin result response
Debug.Log($"Is Won: {response.IsWon}");
Debug.Log($"Win Amount: {response.Currency} {response.WinAmount}");
},
(error) =>
{
// handle error
Debug.LogError($"Error: {error.Reason}");
});
For a different reason (message lost, internal server error etc.) but the response could not come. You can better handle this case by specifying message timeout (in seconds) and the number of retrying attempts for any request. By default, the message will send one time and wait for a response as long as it is needed.
var timeoutSec = 10.0f;
var retryAttempts = 3;
var spinRequest = new SpinRequest(100, fireball.CurrentSession);
fireball.SendRequest<SpinRequest, SpinResult>(spinRequest,
(response) =>
{
// spin result response
Debug.Log($"Is Won: {response.IsWon}");
Debug.Log($"Win Amount: {response.Currency} {response.WinAmount}");
},
(error) =>
{
if (error.IsTimeout)
{
// handle timeout
Debug.LogError($"Error: {error.Reason}");
}
else
{
// handle error
Debug.LogError($"Error: {error.Reason}");
}
},
timeoutSec, retryAttempts);
More about errors handling you can find here