# 다이렉트 메시징

> Send direct messages between users.

VivoxMessage는 서비스에 로그인한 개별 플레이어에게 전송될 수 있습니다. 이 기능을 흔히 DM이라고 합니다.

## 다이렉트 메시지 보내기##send-directed-messages

[`VivoxService.Instance.SendDirectTextMessageAsync(string playerId, string message)`](https://docs.unity3d.com/Packages/com.unity.services.vivox@latest/index.html?subfolder=/api/Unity.Services.Vivox.IVivoxService.html#Unity_Services_Vivox_IVivoxService_SendDirectTextMessageAsync_System_String_System_String_Unity_Services_Vivox_MessageOptions_)를 사용하여 플레이어에게 메시지를 보낼 수 있습니다. playerId는 메시지를 보내야 하는 플레이어의 playerId이고 message는 보내야 하는 메시지입니다.

다음 코드 스니핏은 관리되는 채널 플레이어 목록에서 전달된 플레이어에게 메시지를 보내는 구현 예제입니다. 메시지는 전용 메시지 InputField에서 가져옵니다.

```cs
void SendMessageAsync(string playerId)
{
    if (string.IsNullOrEmpty(MessageInputField.text))
    {
        return;
    }

    VivoxService.Instance.SendChannelTextMessageAsync(playerId, MessageInputField.text);
    MessageInputField.text = string.Empty;
}
```

## 다이렉트 메시지 받기##receive-directed-messages

다른 사용자로부터 메시지를 받으려면 [`VivoxService.Instance.DirectedMessageReceived`](https://docs.unity3d.com/Packages/com.unity.services.vivox@latest/index.html?subfolder=/api/Unity.Services.Vivox.IVivoxService.html#Unity_Services_Vivox_IVivoxService_DirectedMessageReceived) 이벤트를 구독해야 합니다.

다이렉트 VivoxMessages는 [채널 메시지](./channel-messages.md)와 거의 동일하며, `VivoxMessage.ChannelName`은 null로 설정되고 `VivoxMessage.FromSelf`는 false로 설정됩니다.

다음 코드 스니핏은 수신 시 VivoxMessage에서 사용 가능한 모든 정보를 가져오는 예제와 함께 이벤트를 구독하는 예제 라인입니다.

```cs

//Subscribe to this event at some point in the Vivox initialization process,
//preferably before joining any channels.
. . .
VivoxService.Instance.DirectedMessageReceived += OnDirectedMessageReceived;
. . .

void OnDirectedMessageReceived(VivoxMessage message)
{
    var senderName = message.SenderDisplayName;
    var senderId = message.SenderPlayerId;
    var messageText = message.MessageText;
    var timeReceived = message.ReceivedTime;
    var language = message.Language;
    var messageId = message.MessageId;
}
```

## 다이렉트 텍스트 메시지 이력##direct-text-message-history

또한 Vivox를 사용하면 사용자는 [`VivoxService.Instance.GetDirectTextMessageHistoryAsync(string playerId, int requestSize = 10, ChatHistoryQueryOptions chatHistoryQueryOptions = null)`](https://docs.unity3d.com/Packages/com.unity.services.vivox@latest/index.html?subfolder=/api/Unity.Services.Vivox.IVivoxService.html#Unity_Services_Vivox_IVivoxService_GetDirectTextMessageHistoryAsync_System_String_System_Int32_Unity_Services_Vivox_ChatHistoryQueryOptions_)를 사용하여 두 플레이어 간의 다이렉트 메시지 대화 이력에 액세스할 수 있습니다. playerId는 이력을 요청할 플레이어의 ID이고, requestSize는 최대 반환할 메시지 수(기본값은 10)이며, chatHistoryQueryOptions는 메시지에 포함된 단어나 문구를 지정하거나 전후에 쿼리할 시간을 지정하는 등의 작업을 수행할 수 있는 선택적 파라미터입니다. ChatHistoryQueryOptions에 대한 보다 구체적인 정보는 [채팅 이력 쿼리 옵션 페이지](../../text-chat-guide/chat-history-query-options)에서 확인할 수 있습니다.

`IReadOnlyCollection`에서 반환된 메시지는 최신 메시지부터 오래된 메시지 순서대로 나열됩니다.

다음 코드 스니핏은 LobbyChannelName 컬렉션에서 가장 최근 메시지 25개를 가져온 다음 추가로 `ChatHistoryQueryOptions`를 사용하지 않고 발송자의 표시 이름과 메시지를 가장 오래된 항목부터 가장 최신 항목 순서대로 기록하는 예제입니다.

```cs
void FetchHistoryAsync()
{
    var historyMessages = await VivoxService.Instance.GetDirectTextMessageHistoryAsync(LobbyChannelName, 10);
    //Reversing the messages so they display from oldest to newest
    var reversedMessages = historyMessages.Reverse();
    foreach(VivoxMessage message in reversedMessages)
    {
        Debug.Log($"{message.SenderDisplayName}: {message.MessageText}");
    }
}
```

## 다이렉트 메시지 편집##editing-direct-messages

Vivox를 통해 사용자는 [`VivoxService.Instance.EditDirectTextMessageAsync(string messageId, string newMessage)`](https://docs.unity3d.com/Packages/com.unity.services.vivox@latest/index.html?subfolder=/api/Unity.Services.Vivox.IVivoxService.html#Unity_Services_Vivox_IVivoxService_EditDirectTextMessageAsync_System_String_System_String_)를 사용하여 채널에 보낸 메시지의 텍스트를 편집할 수 있습니다. messageId는 변경할 메시지의 ID이고 newMessage는 메시지의 업데이트된 텍스트입니다.

메시지가 편집되면 다이렉트 메시지와 연관된 로그인한 플레이어는 업데이트된 MessageText와 함께 편집된 VivoxMessage가 포함된 [`VivoxService.Instance.DirectedMessageEdited`](https://docs.unity3d.com/Packages/com.unity.services.vivox@latest/index.html?subfolder=/api/Unity.Services.Vivox.IVivoxService.html#Unity_Services_Vivox_IVivoxService_DirectedMessageEdited) 액션을 수신하게 됩니다.

```cs
public async void UpdateDirectedMessageAsync(VivoxMessage messageToUpdate, string updatedMessageText)
{
    await VivoxService.Instance.EditDirectTextMessageAsync(messageToUpdate.MessageId, updatedMessageText);
}
```

## 다이렉트 메시지 삭제##deleting-direct-messages

Vivox를 통해 사용자가 [`VivoxService.Instance.DeleteDirectTextMessageAsync(string messageId)`](https://docs.unity3d.com/Packages/com.unity.services.vivox@latest/index.html?subfolder=/api/Unity.Services.Vivox.IVivoxService.html#Unity_Services_Vivox_IVivoxService_DeleteDirectTextMessageAsync_System_String_)를 사용하여 보낸 다이렉트 메시지를 삭제할 수 있습니다. messageId는 삭제할 메시지의 ID입니다.

메시지가 삭제되면 다이렉트 메시지와 연관된 로그인한 플레이어는 삭제된 VivoxMessage가 포함된 [`VivoxService.Instance.DirectedMessageDeleted`](https://docs.unity3d.com/Packages/com.unity.services.vivox@latest/index.html?subfolder=/api/Unity.Services.Vivox.IVivoxService.html#Unity_Services_Vivox_IVivoxService_DirectedMessageDeleted) 액션을 수신하게 됩니다.

```cs
public async void DeleteDirectMessageAsync(VivoxMessage messageToDelete)
{
    await VivoxService.Instance.DeleteDirectTextMessageAsync(messageToDelete.MessageId)
}
```
