> ## 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.

# 실시간 메시지 SignalR Hub

> HubConnection 설정, MsgPack 인코딩, KeepAlive 간격을 포함하여 PlayFab Lobby 및 Matchmaking 클라이언트를 실시간 메시지 SignalR Hub에 연결합니다.

<Note>
  여기에 문서화된 REST 및 SignalR API는 클라이언트 SDK보다 더 복잡합니다. 해당 SDK가 요구 사항을 충족하지 않는 경우가 아니라면 [Lobby C++ SDK](/services/playfab/multiplayer/lobby/playfabmultiplayerreference-cpp/pflobby/pflobby_members) 또는 [Matchmaking C++ SDK](/services/playfab/multiplayer/lobby/playfabmultiplayerreference-cpp/pfmatchmaking/pfmatchmaking_members)를 대신 사용하는 것을 고려하세요.
</Note>

[Lobby 및 Matchmaking 서비스](/services/playfab/multiplayer/lobby/lobby-and-matchmaking)의 실시간 메시지 기능은 [SignalR 서비스](https://learn.microsoft.com/en-us/aspnet/core/signalr)를 통해 작동하며, 이는 [SignalR Hub](https://learn.microsoft.com/en-us/aspnet/core/signalr/introduction#hubs)를 노출합니다. 이 SignalR Hub는 게임 클라이언트가 [구독한 리소스](/services/playfab/multiplayer/real-time-messages/subscribing-to-resources)에 대한 메시지를 수신하는 데 사용할 수 있습니다.

## SignalR Hub에 연결하기

실시간 메시지를 수신하는 첫 번째 단계는 [SignalR 클라이언트](https://learn.microsoft.com/en-us/aspnet/core/signalr/client-features)를 생성하는 것입니다. 클라이언트는 다음 API를 사용하여 [HubConnection을 인스턴스화](https://learn.microsoft.com/en-us/aspnet/core/signalr/dotnet-client#connect-to-a-hub)함으로써 SignalR Hub에 대한 연결을 만들 수 있습니다: [SignalR Negotiate REST API](https://learn.microsoft.com/en-us/rest/api/playfab/multiplayer/pub-sub/negotiate). 이 API는 [SignalR 문서](https://github.com/dotnet/aspnetcore/blob/main/src/SignalR/docs/specs/TransportProtocols.md#post-endpoint-basenegotiate-request)에 설명된 Negotiate API를 구현합니다.

<Warning>
  SignalR Hub에 연결할 때 [MessagePack (MsgPack) 인코딩](https://github.com/dotnet/aspnetcore/blob/main/src/SignalR/docs/specs/HubProtocol.md#messagepack-msgpack-encoding)을 사용하는 것이 강력히 권장되며, 향후에는 이것이 유일하게 지원되는 인코딩으로 강제될 예정입니다.
</Warning>

<Info>
  클라이언트는 `HubConnection`을 구축할 때 `KeepAliveInterval`을 반드시 **5초**로 설정해야 합니다. 백엔드 서비스는 **10초** 이내에 keep-alive ping을 수신하지 않은 연결을 시간 초과하여 닫습니다. SignalR 기본 `KeepAliveInterval`인 15초는 너무 느려서 연결 끊김을 유발합니다. `csharp var connection = new HubConnectionBuilder() .WithUrl(hubUrl, options => { /* auth headers */ }) .WithKeepAliveInterval(TimeSpan.FromSeconds(5)) .Build(); ` SignalR 클라이언트 구성에 대한 자세한 내용은 [SignalR 구성 문서](https://learn.microsoft.com/en-us/aspnet/core/signalr/configuration#configure-client-options)를 참조하세요.
</Info>

<Note>
  1. SignalR 클라이언트 구현은 URL에서 이 경로의 `/negotiate` 부분을 생략할 것으로 예상합니다(예: HubConnection은 `...playfabapi.com/pubsub`으로 끝나는 URL에 대해 생성되어야 합니다). 1. 이 API를 호출하는 데 필요한 인증 헤더는 [이 메서드에 표시된 것처럼](https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.signalr.client.hubconnectionbuilderhttpextensions.withurl#microsoft-aspnetcore-signalr-client-hubconnectionbuilderhttpextensions-withurl\(microsoft-aspnetcore-signalr-client-ihubconnectionbuilder-system-string-system-action\(\(microsoft-aspnetcore-http-connections-client-httpconnectionoptions\)\)\)) `HttpConnectionOptions`를 통해 SignalR 클라이언트에 전달할 수 있습니다.
</Note>

## 메시지 수신을 위한 클라이언트 메서드 구현

메시지를 수신하기 위해 호출될 [클라이언트 메서드 구현에 대한](https://learn.microsoft.com/en-us/aspnet/core/signalr/dotnet-client#call-client-methods-from-hub) SignalR 클라이언트 문서를 참조하세요. 클라이언트는 아래 [클라이언트 메서드](#client-methods)에 나열된 모든 메서드를 구현해야 합니다.

## 서버 메서드를 호출하여 세션 시작 및 종료하기

세션 관리를 위한 [서버 메서드 호출에 대한](https://learn.microsoft.com/en-us/aspnet/core/signalr/dotnet-client#call-hub-methods-from-client) SignalR 클라이언트 문서를 참조하세요. 예를 들어, 허브에 연결되면 [StartOrRecoverSession](/services/playfab/multiplayer/real-time-messages/server-methods/start-or-recover-session)을 호출하여 [리소스를 구독](/services/playfab/multiplayer/real-time-messages/subscribing-to-resources)하는 데 사용할 수 있는 `connection handle`을 받아야 합니다. 마찬가지로, 세션이 완료되면 [EndSession](/services/playfab/multiplayer/real-time-messages/server-methods/end-session)을 호출하세요.

## 리소스 구독하기

[StartOrRecoverSession](/services/playfab/multiplayer/real-time-messages/server-methods/start-or-recover-session)으로 세션이 시작되면, 반환된 `connection handle`을 사용하여 [리소스를 구독](/services/playfab/multiplayer/real-time-messages/subscribing-to-resources)하고 메시지 수신을 시작할 수 있습니다.

## API

### 클라이언트 메서드

| 이름                                                                                                                                      | 설명                                               |
| --------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------ |
| [ReceiveMessage](/services/playfab/multiplayer/real-time-messages/client-methods/receive-message)                                       | 메시지를 수신합니다.                                      |
| [ReceiveSubscriptionChangeMessage](/services/playfab/multiplayer/real-time-messages/client-methods/receive-subscription-change-message) | 현재 구독을 추적하기 위해 SubscriptionChangeMessage를 수신합니다. |

### 서버 메서드

| 이름                                                                                                                | 설명               |
| ----------------------------------------------------------------------------------------------------------------- | ---------------- |
| [StartOrRecoverSession](/services/playfab/multiplayer/real-time-messages/server-methods/start-or-recover-session) | 세션을 시작하거나 복구합니다. |
| [EndSession](/services/playfab/multiplayer/real-time-messages/server-methods/end-session)                         | 세션을 종료합니다.       |

#### 공유 세션 메서드

| 이름                                                                                                                    | 설명                                                                            |
| --------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------- |
| [AddEntityToSession](/services/playfab/multiplayer/real-time-messages/server-methods/add-entity-to-session)           | 여러 엔터티가 같은 장치에 로그인한 시나리오용. 공유 세션에서 여러 로컬 엔터티의 메시지를 수신하기 위해 세션에 추가 엔터티를 추가합니다. |
| [RemoveEntityFromSession](/services/playfab/multiplayer/real-time-messages/server-methods/remove-entity-from-session) | 세션에서 엔터티를 제거합니다.                                                              |

## 참고 항목

* [리소스 구독하기](/services/playfab/multiplayer/real-time-messages/subscribing-to-resources)
* [Lobby 및 Matchmaking API용 실시간 메시지](/services/playfab/multiplayer/real-time-messages/overview)


## Related topics

- [Message 실시간 메시지 타입](/ko/services/playfab/multiplayer/real-time-messages/types/message.md)
- [실시간 메시지를 위한 리소스 구독](/ko/services/playfab/multiplayer/real-time-messages/subscribing-to-resources.md)
- [SubscriptionChangeMessage 실시간 메시지 타입](/ko/services/playfab/multiplayer/real-time-messages/types/subscription-change-message.md)
- [EndSessionRequest 실시간 메시지 타입](/ko/services/playfab/multiplayer/real-time-messages/types/end-session-request.md)
- [EndSessionResponse 실시간 메시지 타입](/ko/services/playfab/multiplayer/real-time-messages/types/end-session-response.md)
