频道消息
Send, receive and manage text messages in Vivox channels.
阅读时间3 分钟最后更新于 13 天前
VivoxMessages 可以发送到使用 TextOnly 或 TextAndAudio 的 ChatCapability 启用了文本的特定 Vivox 频道。
发送频道消息
您可以使用VivoxService.Instance.SendChannelTextMessageAsync(string channelName, string message)channelNamemessagepublic async void SendMessageAsync(){ if (string.IsNullOrEmpty(MessageInputField.text)) { return; } VivoxService.Instance.SendChannelTextMessageAsync(LobbyChannelName, MessageInputField.text); MessageInputField.text = string.Empty;}
接收频道消息
为了从启用了文本的频道接收消息,必须订阅VivoxService.Instance.ChannelMessageReceivedVivoxMessage.ChannelNameVivoxMessage.FromSelf//Subscribe to this event at some point in the Vivox initialization process,//preferably before joining any channels.. . .VivoxService.Instance.ChannelMessageReceived += OnChannelMessageReceived;. . .void OnChannelMessageReceived(VivoxMessage message){ var channelName = message.ChannelName; var senderName = message.SenderDisplayName; var senderId = message.SenderPlayerId; var messageText = message.MessageText; var timeReceived = message.ReceivedTime; var language = message.Language; var fromSelf = message.FromSelf; var messageId = message.MessageId;}
频道文本消息历史记录
Vivox 允许用户使用VivoxService.Instance.GetChannelTextMessageHistoryAsync(string channelName, int requestSize = 10, ChatHistoryQueryOptions chatHistoryQueryOptions = null)channelNamerequestSizechatHistoryQueryOptionsChatHistoryQueryOptionIReadOnlyCollection
以下代码片段的示例说明如何从设置的 LobbyChannelName 中提取最近 25 条消息,然后按从旧到新的顺序记录发送方的显示名称和消息,而无需使用可选的 ChatHistoryQueryOptions。
public async void FetchHistoryAsync(){ var historyMessages = await VivoxService.Instance.GetChannelTextMessageHistoryAsync(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 允许用户使用VivoxService.Instance.EditChannelTextMessageAsync(string channelName, string messageId, string newMessage)VivoxService.Instance.ChannelMessageEditedpublic async void UpdateChannelMessageAsync(VivoxMessage messageToUpdate, string updatedMessageText){ await VivoxService.Instance.EditChannelTextMessageAsync(messageToUpdate.ChannelName, messageToUpdate.MessageId, updatedMessageText);}
删除频道消息
Vivox 允许用户使用VivoxService.Instance.DeleteChannelTextMessageAsync(string channelName, string messageId)VivoxService.Instance.ChannelMessageDeletedpublic async void DeleteChannelMessageAsync(VivoxMessage messageToDelete){ await VivoxService.Instance.DeleteChannelTextMessageAsync(messageToDelete.ChannelName, messageToDelete.MessageId)}