> ## Documentation Index
> Fetch the complete documentation index at: https://devdocs.xbox.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Programando o serviço RTA

> Exemplos de código em C simples que mostram como registrar e cancelar o registro de manipuladores da XSAPI no serviço Real-Time Activity (RTA) do XBOX para receber alterações de estatísticas de jogadores.

Este tópico demonstra como chamar o serviço Real-Time Activity (RTA) usando os exemplos de código em C simples a seguir.

* [Registrando um manipulador para obter eventos de alteração de estatísticas do serviço RTA](#registering-a-handler-to-get-statistic-changes-events-from-the-Real-Time-Activity-service)
* [Cancelando o registro no serviço RTA](#unregistering-from-the-Real-Time-Activity-service)

<a id="registering-a-handler-to-get-statistic-changes-events-from-the-Real-Time-Activity-service" />

## Registrando um manipulador para obter eventos de alteração de estatísticas do serviço RTA

Você define suas estatísticas e as configura para o RTA no [Partner Center](https://partner.microsoft.com/dashboard). Para obter mais informações, consulte:

* [Configurando estatísticas em destaque e placares gerenciados pelo título no Partner Center](/pt-BR/services/xbox-services/player-data/stats-leaderboards/title-managed/config/live-tm-leaderboards-portal)

* [Registrando-se para notificações de alteração de estatísticas de jogadores](/pt-BR/services/xbox-services/fundamentals/rta/concepts/live-register-for-stat-notifications)

<Note>Se você desenvolve estatísticas baseadas em eventos, entre em contato com seu gerente de conta de desenvolvedor (DAM) para obter informações sobre a configuração de estatísticas baseadas em eventos no portal do [Partner Center](https://partner.microsoft.com/dashboard). Para obter mais informações, consulte [Configure XBOX Live stats and stat rules in Partner Center)](https://developer.microsoft.com/en-us/games/xbox/partner/windows-configure-stats).</Note>

No seu título, você precisa configurar as estatísticas que deseja que o RTA acompanhe. Os manipuladores do título são invocados sempre que uma das estatísticas configuradas muda, conforme mostrado no exemplo de código a seguir.

#### C simples

```cpp theme={null}
// Add a statistic changed handler.
void* context{ nullptr };
XblFunctionContext statisticChangedFunctionContext = XblUserStatisticsAddStatisticChangedHandler(
    xboxLiveContext,
    [](XblStatisticChangeEventArgs eventArgs, void* context)
    {
        // Handle statistic change. 
        LogToScreen("Statistic changed callback: stat changed (%s = %s)",
            eventArgs.latestStatistic.statisticName,
            eventArgs.latestStatistic.value);
    },
    context
    );

// Configure the statistics that you want RTA to track. Titles only receive real-time updates for tracked statistics.
// Note that you can update the set of tracked statistics independently from the handlers.
std::vector<const char*> statisticNames{ "TotalPuzzlesSolved" };
HRESULT hr = XblUserStatisticsTrackStatistics(
    xblContextHandle,
    &xboxUserId,
    1,
    scid,
    statisticNames.data(),
    statisticNames.size()
);
```

<a id="unregistering-from-the-Real-Time-Activity-service" />

## Cancelando o registro no serviço RTA

Quando as atualizações não forem mais necessárias para uma determinada estatística (ou conjunto de estatísticas), seu título deverá simplesmente parar de acompanhar essa estatística.
Se as atualizações de estatísticas não forem mais necessárias de forma alguma, remover todos os manipuladores registrados remove automaticamente as assinaturas do RTA relacionadas, conforme mostrado no exemplo de código a seguir.

#### C simples

```cpp theme={null}
// Stop receiving updates for a particular statistic.   
std::vector<const char*> statisticNames{ "TotalPuzzlesSolved" }; 
HRESULT hr = XblUserStatisticsStopTrackingStatistics(
    xblContextHandle,
    &xboxUserId,
    1,
    scid,
    statisticNames.data(),
    statisticNames.size()
);

// Alternatively, stop receiving updates for statistics changes altogether.
XblUserStatisticsRemoveStatisticChangedHandler(
    xblContextHandle,
    statisticChangedFunctionContext
);
```

<Info>Se um cliente usar o RTA para sessões multiplayer e for desconectado por trinta segundos, o Multiplayer Session Directory (MPSD) detectará que a sessão do RTA foi fechada e removerá o jogador da sessão. Em seguida, a XSAPI restabelecerá automaticamente a conexão do RTA. No entanto, é responsabilidade do título regravar suas sessões do MPSD depois que a assinatura do RTA for restabelecida.</Info>

## Confira também

[XblStatisticChangeEventArgs](/pt-BR/reference/live/xsapi-c/user_statistics_c/structs/xblstatisticchangeeventargs)

[XblUserStatisticsAddStatisticChangedHandler](/pt-BR/reference/live/xsapi-c/user_statistics_c/functions/xbluserstatisticsaddstatisticchangedhandler)

[XblUserStatisticsRemoveStatisticChangedHandler](/pt-BR/reference/live/xsapi-c/user_statistics_c/functions/xbluserstatisticsremovestatisticchangedhandler)


## Related topics

- [Código de exemplo do serviço Real-Time Activity (RTA)](/pt-BR/services/xbox-services/fundamentals/rta/how-to/live-rta-howto-nav.md)
- [Instruções](/pt-BR/services/xbox-services/fundamentals/rta/how-to/index.md)
- [Práticas recomendadas para o serviço RTA](/pt-BR/services/xbox-services/fundamentals/rta/concepts/live-rta-best-practices.md)
- [Serviço Real-Time Activity (RTA)](/pt-BR/services/xbox-services/fundamentals/rta/live-rta-nav.md)
- [Conceitos do serviço Real-Time Activity (RTA)](/pt-BR/services/xbox-services/fundamentals/rta/concepts/live-rta-concepts-nav.md)
