# 获取对话列表

> How to retrieve a list of conversations for a player.

可以获取用户的对话列表。对话是指用户发送消息或私信 (DM) 的频道。可以使用此 API 填充频道或 DM 列表，以便用户查找其活动聊天。

> **Note:**
>
> 返回的对话列表按最近的活动进行排序。结果中先显示包含较新消息的对话。

要获取对话列表，请登录到应用程序，然后使用 [`await VivoxService.Instance.GetConversationsAsync();`](https://docs.unity3d.com/Packages/com.unity.services.vivox@latest/index.html?subfolder=/api/Unity.Services.Vivox.IVivoxService.html#Unity_Services_Vivox_IVivoxService_GetConversationsAsync_Unity_Services_Vivox_ConversationQueryOptions_) 发出请求。此方法返回 `VivoxConversation` 对象的集合，每个对象表示一个 DM 或一个频道对话。

以下代码的示例说明如何获取用户的对话列表。

```text
// The use of `ConversationQueryOptions` is optional and the method can be called without providing it.
var options = new ConversationQueryOptions()
{
    CutoffTime = DateTime.Now, // Point in time to fetch conversations from. Conversations joined after this timestamp will not be present in the query results. This timestamp gets converted to UTC internally.
    PageCursor = 1, // Parameter if you’re fetching anything but the first page.
    PageSize = 10 // The number of results returned per page. The default is 10.
};
var conversations = await VivoxService.Instance.GetConversationsAsync(options);
```

获取用户的对话列表后，可以解析得到的 `VivoxConversation` 集合，以确定哪些是 DM，哪些是频道对话：

```text
foreach (VivoxConversation conversation in conversations)
{
    switch (conversation.ConversationType)
    {
        case ConversationType.ChannelConversation:
            // Code for handling channel conversations.
            // You must be in a channel matching conversation.Name in order to fetch the message history of type ConversationType.ChannelConversation.
            break;
        case ConversationType.DirectedMessageConversation:
            // Code for handling DM conversations, for instance...
            var directMessages = await VivoxService.Instance.GetDirectTextMessageHistoryAsync(conversation.Name);
            // Populate UI with directMessages with another user.
            break;
        default:
            break;
    }
}
```

> **Note:**
>
> 根据返回的 `VivoxConversation` 的 [`VivoxConversation.ConversationType`](https://docs.unity3d.com/Packages/com.unity.services.vivox@latest/index.html?subfolder=/api/Unity.Services.Vivox.VivoxConversation.html#Unity_Services_Vivox_VivoxConversation_ConversationType)，`VivoxConversation.Name` 将是另一个用户的 PlayerId，或者是频道的唯一频道名称。在这两种情况下，`VivoxConversation.Name` 是两种对话之一的唯一标识符。
