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

# 소셜 관계 변경 처리하기

> XBOX services 소셜 관계 변경 이벤트를 처리하고 사용자의 친구 목록이 업데이트될 때 핸들러를 추가하거나 제거하기 위한 XSAPI 코드입니다.

이 항목에서는 소셜 관계 변경을 처리하기 위한 예제 코드를 제공합니다. 예제에서 `xboxLiveContext`는 로그인한 사용자의 `XblContextHandle`이고, `state`는 타이틀의 상태를 보관하며, `LogToFile`은 타이틀에서 정의하는 로깅 함수입니다.

## 관계 변경 구독하기

관계 변경을 별도로 구독할 필요가 없습니다. XBOX Services API(XSAPI)는 컨텍스트에 대해 하나의 소셜 RTA(Real-Time Activity) 구독을 만들거나 재사용합니다. 관계 변경 핸들러와 받은 친구 요청 수 핸들러는 이 구독을 공유합니다. XSAPI는 두 유형의 핸들러를 모두 제거한 후에만 구독을 제거합니다.

> [!NOTE]
>
> `XblSocialSubscribeToSocialRelationshipChange` 및 `XblSocialUnsubscribeFromSocialRelationshipChange`는 더 이상 사용되지 않으며 RTA 구독을 관리하지 않습니다. 코드에서 이 두 함수에 대한 호출을 쌍으로 제거하세요. 코드에서 이미 `XblSocialSubscribeToSocialRelationshipChange`로 구독 핸들을 만든 경우 `XblSocialUnsubscribeFromSocialRelationshipChange`를 호출하여 해제하세요.

## 관계 변경 핸들러 추가하기

**Flat C API**

```cpp theme={null}
state.socialRelationshipChangedHandlerToken = XblSocialAddSocialRelationshipChangedHandler(
    xboxLiveContext,
    [](const XblSocialRelationshipChangeEventArgs* args, void* context)
    {
        UNREFERENCED_PARAMETER(context);
        LogToFile("Social relationship changed:");
        std::stringstream ss;
        for (size_t i = 0; i < args->xboxUserIdsCount; ++i)
        {
            if (i > 0) 
            {
                ss << ", ";
            }
            ss << args->xboxUserIds[i];
        }
        LogToFile("socialNotification = %u, affectedXuids = %s", args->socialNotification, ss.str().data());
    },
    nullptr
);
```

## 관계 변경 핸들러 제거하기

**Flat C API**

```cpp theme={null}
HRESULT hr = XblSocialRemoveSocialRelationshipChangedHandler(xboxLiveContext, state.socialRelationshipChangedHandlerToken);
state.socialRelationshipChangedHandlerToken = 0;
```

## 받은 친구 요청 수의 변경 처리하기

**Flat C API**

```cpp theme={null}
XblFunctionContext friendRequestCountToken{ 0 };
HRESULT hr = XblSocialAddFriendRequestCountChangedHandler(
    xboxLiveContext,
    [](const XblSocialFriendRequestCountChangedEventArgs* args, void* context)
    {
        UNREFERENCED_PARAMETER(context);
        // args is valid only during the callback.
        LogToFile("Incoming friend requests: %zu", args->incomingFriendRequestCount);
    },
    nullptr,
    &friendRequestCountToken
);

if (SUCCEEDED(hr))
{
    // Keep the token until you no longer need notifications.
    state.friendRequestCountToken = friendRequestCountToken;
}
```

더 이상 알림이 필요하지 않으면 핸들러를 제거합니다. 성공적으로 등록된 핸들러만 제거하세요.

```cpp theme={null}
if (state.friendRequestCountToken != 0)
{
    HRESULT hr = XblSocialRemoveFriendRequestCountChangedHandler(xboxLiveContext, state.friendRequestCountToken);
    if (SUCCEEDED(hr))
    {
        state.friendRequestCountToken = 0;
    }
}
```

## 참고 항목

[XblSocialAddSocialRelationshipChangedHandler](https://docs.xbox.com/ko/reference/live/xsapi-c/social_c/functions/xblsocialaddsocialrelationshipchangedhandler.md)
[XblSocialRemoveSocialRelationshipChangedHandler](https://docs.xbox.com/ko/reference/live/xsapi-c/social_c/functions/xblsocialremovesocialrelationshipchangedhandler.md)
[XblSocialAddFriendRequestCountChangedHandler](https://docs.xbox.com/ko/reference/live/xsapi-c/social_c/functions/xblsocialaddfriendrequestcountchangedhandler.md)
[XblSocialRemoveFriendRequestCountChangedHandler](https://docs.xbox.com/ko/reference/live/xsapi-c/social_c/functions/xblsocialremovefriendrequestcountchangedhandler.md)


## Related topics

- [People System(친구 목록) 예제 코드(목차)](https://docs.xbox.com/ko/services/xbox-services/community/people-system/how-to/live-pplsys-howto-nav.md)
- [방법](https://docs.xbox.com/ko/services/xbox-services/community/people-system/how-to/index.md)
- [PlayFab Party 개체 및 관계](https://docs.xbox.com/ko/services/playfab/multiplayer/networking/concepts-objects.md)
- [이벤트 기반 통계 변경 처리](https://docs.xbox.com/ko/services/xbox-services/player-data/stats-leaderboards/event-based/how-to/live-handling-eb-stat-change.md)
- [XblSocialAddSocialRelationshipChangedHandler](https://docs.xbox.com/ko/reference/live/xsapi-c/social_c/functions/xblsocialaddsocialrelationshipchangedhandler.md)
