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

# 大厅邀请

> 发送和接受 PlayFab 大厅邀请,使用邀请侦听器,并通过大厅成员和所有者特权控制谁可以邀请他人。

本文概述大厅邀请以及如何在 Lobby 和 Matchmaking SDK 中使用它们。

<Note>
  只有玩家(即 title\_player\_account PlayFab 实体)才能发送或接收邀请。游戏服务器(即 game\_server PlayFab 实体)不能。有关游戏服务器如何与大厅交互的更多信息,请参阅[游戏服务器与大厅](/services/playfab/multiplayer/lobby/lobby-server-overview)。
</Note>

## 邀请类型

有两种类型的邀请你的游戏可能会用到。

1. [游戏内邀请](#joining-a-lobby-by-in-game-invites)
2. [平台邀请](#joining-a-lobby-by-platform-invites)

### 通过游戏内邀请加入大厅

* 大厅成员可以通过大厅服务直接邀请另一位玩家加入该大厅。
* 大厅所有者可以选择通过大厅属性 **RestrictInvitesToLobbyOwner** 限制只有所有者可以发送邀请
* 此邀请会将大厅的连接字符串共享给被邀请玩家。
* 被邀请玩家通过 **PFLobbyInviteReceivedStateChange** 接收邀请,并可使用附加的连接字符串加入大厅。
* 这些邀请可跨平台工作,但仅在游戏内有效。

### 通过平台邀请加入大厅

* 大厅成员可以通过平台特定的邀请机制直接将大厅的连接字符串共享给其他玩家。
* 这些邀请不能跨平台工作,但接收者无需已经运行游戏即可收到。
* 一旦被邀请玩家通过平台机制收到连接字符串,他们就可以使用附加的连接字符串加入大厅。

## 使用 Lobby 和 Matchmaking SDK 发送和接收邀请示例

在邀请接收者上使用 **PFMultiplayerStartListeningForLobbyInvites** 来启用接收游戏内邀请。

邀请侦听器成功建立后,其状态将变为 **Listening**。

```cpp theme={null}
HRESULT AllowInvitations(
    const PFEntityKey* entity)
{
    return PFMultiplayerStartListeningForLobbyInvites(g_pfmHandle, entity);
}

void HandleInvitationListenerStatusChange(
    const PFLobbyInvitationListenerStatusChangedStateChange& invitationListenerStateChange)
{
    PFLobbyInvitationListenerStatus status;
    HRESULT hr = PFMultiplayerGetLobbyInvitationListenerStatus(
        g_pfmHandle,
        &invitationListenerStateChange.listeningEntity,
        &status);
    assert(SUCCEEDED(hr));

    switch (status)
    {
        case PFLobbyInvitationListenerStatus::Listening:
        {
            Log("%s is listening for invitations", invitationListenerStateChange.listeningEntity.id);
            break;
        }
        case PFLobbyInvitationListenerStatus::NotAuthorized:
        {
            Log("Invitation listener not authorized!"); // this is likely an issue with the listener's entity token.
            break;
        }
        default:
    }
}
```

使用 **PFLobbySendInvite** 通过大厅服务向另一位 PlayFab 用户发送邀请。

接收者将收到一个 **PFLobbyInviteReceivedStateChange**。

```cpp theme={null}
HRESULT SendInvite(PFLobbyHandle lobby, const PFEntityKey* sender, const PFEntityKey* receiver)
{
    return PFLobbySendInvite(lobby, sender, receiver, nullptr);
}

void HandleInvitationNotification(const PFLobbyInviteReceivedStateChange& invite)
{
    Log("%s invited to lobby by %s", invite.listeningEntity.id, invite.invitingEntity.id);
    // pass invite.connectionString to PFMultiplayerJoinLobby
}
```

## 另请参阅

* [大厅与匹配](/services/playfab/multiplayer/lobby/lobby-and-matchmaking)
* [查找大厅](/services/playfab/multiplayer/lobby/find-lobbies)
* [加入大厅](/services/playfab/multiplayer/lobby/join-lobbies)
* [大厅属性](/services/playfab/multiplayer/lobby/lobby-properties)


## Related topics

- [同时使用大厅和匹配](/zh-CN/services/playfab/multiplayer/lobby/lobby-and-matchmaking.md)
- [PFMultiplayerStopListeningForLobbyInvites](/zh-CN/services/playfab/multiplayer/lobby/playfabmultiplayerreference-cpp/pflobby/functions/pfmultiplayerstoplisteningforlobbyinvites.md)
- [PFMultiplayerStartListeningForLobbyInvites](/zh-CN/services/playfab/multiplayer/lobby/playfabmultiplayerreference-cpp/pflobby/functions/pfmultiplayerstartlisteningforlobbyinvites.md)
- [加入大厅](/zh-CN/services/playfab/multiplayer/lobby/join-lobbies.md)
- [PFLobbyGetRestrictInvitesToLobbyOwner](/zh-CN/services/playfab/multiplayer/lobby/playfabmultiplayerreference-cpp/pflobby/functions/pflobbygetrestrictinvitestolobbyowner.md)
