Before you can use it, you need to:
[FunctionsStartup(typeof(Startup))]
public class Function : ICloudEventFunction<MessagePublishedData>
{
...
public async Task HandleAsync(CloudEvent cloudEvent, MessagePublishedData data, CancellationToken cancellationToken)
{
string messageJson = data.Message?.TextData;
ParseResult result = await _fireball.ParseMessage(messageJson);
MessageResult messageResult = await _game.HandleMessage(result);
}
}
public async Task<MessageResult> HandleMessage(ParseResult parseResult)
{
if (parseResult.IsSuccess)
{
switch (parseResult.MessageName)
{
case FireballConstants.MessagesNames.AUTHENTICATE:
return await Auth(parseResult.ToMessage<AuthMessage>());
default:
return MessageResult.ErrorResult($"Undefined message name: {parseResult.MessageName}");
}
}
return MessageResult.ErrorResult("Error Parse message...");
}
...
public async Task<MessageResult> Auth(AuthMessage message)
{
_logger.Log($"authenticate: {message.ToJson()}");
// send
return await _fireball.Authenticate(message);
}
public async Task<MessageResult> HandleMessage(ParseResult parseResult)
{
if (parseResult.IsSuccess)
{
switch (parseResult.MessageName)
{
...
case FireballConstants.MessagesNames.SESSION:
return await OnAuthSuccess(parseResult.ToMessage<SessionMessage>());
...
}
}
...
}
public async Task<MessageResult> OnAuthSuccess(SessionMessage message)
{
if (message.GameSession == null)
{
// create new game session
}
return await _fireball.SendSessionToClient(message);
}
More information about creating and managing game sessions check here
public async Task<MessageResult> HandleMessage(ParseResult parseResult)
{
if (parseResult.IsSuccess)
{
switch (parseResult.MessageName)
{
...
case FireballConstants.MessagesNames.AUTHENTICATE_REJECT:
return await OnAuthReject(parseResult.ToMessage<ErrorMessage>());
...
}
}
...
}
public async Task<MessageResult> OnAuthReject(ErrorMessage error)
{
return await _fireball.SendErrorToClient(error, ErrorCode.Authentication);
}