Directed messages
Vivox provides directed text by using a similar process to what is used for group text. You can add players to existing text sessions, and you can send text messages directly to any user who is signed in, independent of being in a channel with them.
Use directed text for short user-to-user messages, sometimes called whispers or private/direct messages, or to implement invite to channel capabilities.
Send directed messages
You can send a message to a player by using VivoxService.Instance.SendDirectTextMessageAsync(string playerId, string message)
with playerId being the playerId of the player that the message should be sent to, and message being the message that should be sent.
The following code snippet is an example implementation for sending a message to a player passed in from a managed channel roster, with the message being pulled from a dedicated message InputField.
void SendMessageAsync(string playerId)
{
if (string.IsNullOrEmpty(MessageInputField.text))
{
return;
}
VivoxService.Instance.SendDirectTextMessageAsync(playerId, MessageInputField.text);
MessageInputField.text = string.Empty;
}
Receive directed messages
In order to receive messages from other users, the VivoxService.Instance.DirectedMessageReceived
event must be subscribed to.
Directed VivoxMessages are nearly identical to channel messages, with the caveat that the VivoxMessage.ChannelName
will be set to null, and the VivoxMessage.FromSelf
will be set to false.
The following code snippet is an example line of subscribing to the event, along with an example of pulling all available information out of the VivoxMessage on receipt.
//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 also allows for users to access the history of a direct message conversation between two players using VivoxService.Instance.GetDirectTextMessageHistoryAsync(string playerId, int requestSize = 10, ChatHistoryQueryOptions chatHistoryQueryOptions = null)
. The playerId being the ID of the player to request the history of, requestSize being the number of messages to return at maximum (10 is the default), and chatHistoryQueryOptions being an optional parameter which can do things like specifying a word or phrase contained in the messages, or times to query before or after. More specific information on ChatHistoryQueryOptions can be found on the Chat History query options page.
The messages returned in IReadOnlyCollection
are listed in order from newest message to oldest message.
The following code snippet is an example of pulling the most recent 25 messages from a set LobbyChannelName, then logging the sender's display name and the message in order from oldest to newest, without using the optional ChatHistoryQueryOptions
.
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 allows for users to edit the text of messages they have sent into a channel using VivoxService.Instance.EditDirectTextMessageAsync(string messageId, string newMessage)
with the messageId being the ID of the message to change, and newMessage being the updated text of the message.
When a message is successfully edited, logged in players associated with the direct message will receive a VivoxService.Instance.DirectedMessageEdited
Action containing the VivoxMessage that was edited, with the updated MessageText.
public async void UpdateDirectedMessageAsync(VivoxMessage messageToUpdate, string updatedMessageText)
{
await VivoxService.Instance.EditDirectTextMessageAsync(messageToUpdate.MessageId, updatedMessageText);
}
Deleting direct messages
Vivox allows for users to delete direct messages they have sent using VivoxService.Instance.DeleteDirectTextMessageAsync(string messageId)
with messageId being the ID of the message to delete.
When a message is successfully deleted, logged in players associated with the directed message will receive a VivoxService.Instance.DirectedMessageDeleted
Action containing the VivoxMessage that was deleted.
public async void DeleteDirectMessageAsync(VivoxMessage messageToDelete)
{
await VivoxService.Instance.DeleteDirectTextMessageAsync(messageToDelete.MessageId)
}