定向消息
Send direct messages between users.
阅读时间3 分钟最后更新于 7 个月前
VivoxMessages 可以发送给已登录服务的个人玩家。此功能通常称为定向消息 (DM)。
发送定向消息
您可以使用 向玩家发送消息,其中 playerId 是应将消息发送到的玩家的 playerId,而 message 是应发送的消息。
VivoxService.Instance.SendDirectTextMessageAsync(string playerId, string message)以下代码片段的示例实现了向从托管频道名单传入的玩家发送一条消息,此消息是从专用消息 InputField 中提取的。
void SendMessageAsync(string playerId){ if (string.IsNullOrEmpty(MessageInputField.text)) { return; } VivoxService.Instance.SendChannelTextMessageAsync(playerId, MessageInputField.text); MessageInputField.text = string.Empty;}
接收定向消息
为了接收来自其他用户的消息,必须订阅 事件。
VivoxService.Instance.DirectedMessageReceived定向 VivoxMessages 与频道消息几乎相同,但需要注意的是 将设置为 null, 将设置为 false。
VivoxMessage.ChannelNameVivoxMessage.FromSelf以下代码片段是订阅以上事件的代码行示例,以及在收到 VivoxMessage 时从中提取所有可用信息的示例。
//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;}
文本私信历史记录
Vivox 还允许用户使用 访问两个玩家之间私信对话的历史记录。其中,playerId 是要请求相应历史记录的玩家的 ID,requestSize 是最多要返回的消息数(默认值为 10),chatHistoryQueryOptions 是一个可选参数,可以执行多种操作,例如指定消息中包含的单词或短语,或前后查询时间。如需了解有关 ChatHistoryQueryOptions 的更多具体信息,请参阅聊天历史记录查询选项页面。
VivoxService.Instance.GetDirectTextMessageHistoryAsync(string playerId, int requestSize = 10, ChatHistoryQueryOptions chatHistoryQueryOptions = null)IReadOnlyCollection以下代码片段的示例说明如何从设置的 LobbyChannelName 中提取最近 25 条消息,然后按从旧到新的顺序记录发送方的显示名称和消息,而无需使用可选的 。
ChatHistoryQueryOptionsvoid 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}"); }}
编辑私信
Vivox 允许用户使用 编辑他们发送到频道的消息文本,其中 messageId 是要更改的消息的 ID,newMessage 是更新的消息文本。
VivoxService.Instance.EditDirectTextMessageAsync(string messageId, string newMessage)当成功编辑消息后,与私信关联的登录玩家将收到一个 操作,其中包含已编辑的 VivoxMessage 以及更新的 MessageText。
VivoxService.Instance.DirectedMessageEditedpublic async void UpdateDirectedMessageAsync(VivoxMessage messageToUpdate, string updatedMessageText){ await VivoxService.Instance.EditDirectTextMessageAsync(messageToUpdate.MessageId, updatedMessageText);}
删除私信
Vivox 允许用户使用 删除他们发送的私信,其中 messageId 是要删除的消息的 ID。
VivoxService.Instance.DeleteDirectTextMessageAsync(string messageId)当成功删除消息后,与定向消息关联的登录玩家将收到一个 操作,其中包含已删除的 VivoxMessage。
VivoxService.Instance.DirectedMessageDeletedpublic async void DeleteDirectMessageAsync(VivoxMessage messageToDelete){ await VivoxService.Instance.DeleteDirectTextMessageAsync(messageToDelete.MessageId)}