Ejemplo n.º 1
0
        /// <summary>
        /// 向目标对话发送消息
        /// </summary>
        /// <param name="conversation">目标对话</param>
        /// <param name="message">消息体</param>
        /// <param name="options">消息的发送选项,包含了一些特殊的标记<see cref="AVIMSendOptions"/></param>
        /// <returns></returns>
        public Task <IAVIMMessage> SendMessageAsync(
            AVIMConversation conversation,
            IAVIMMessage message,
            AVIMSendOptions options)
        {
            if (this.LinkedRealtime.State != AVRealtime.Status.Online)
            {
                throw new Exception("未能连接到服务器,无法发送消息。");
            }

            var messageBody = message.Serialize();

            message.ConversationId = conversation.ConversationId;
            message.FromClientId   = this.ClientId;

            var cmd = new MessageCommand()
                      .Message(messageBody)
                      .ConvId(conversation.ConversationId)
                      .Receipt(options.Receipt)
                      .Transient(options.Transient)
                      .Priority(options.Priority)
                      .Will(options.Will)
                      .MentionAll(message.MentionAll);

            if (message is AVIMMessage)
            {
                cmd = ((AVIMMessage)message).BeforeSend(cmd);
            }

            if (options.PushData != null)
            {
                cmd = cmd.PushData(options.PushData);
            }

            if (message.MentionList != null)
            {
                cmd = cmd.Mention(message.MentionList);
            }

            var directCmd = cmd.PeerId(this.ClientId);

            return(this.RunCommandAsync(directCmd).OnSuccess(t =>
            {
                var response = t.Result.Item2;

                message.Id = response["uid"].ToString();
                message.ServerTimestamp = long.Parse(response["t"].ToString());

                return message;
            }));
        }