Пример #1
0
        public async Task AckAndRead()
        {
            TaskCompletionSource <object> tcs = new TaskCompletionSource <object>();

            m2.OnMessage = async(conv, msg) => {
                await conv.Read();
            };
            m1.OnMessageDelivered = (conv, msgId) => {
                WriteLine($"{msgId} is delivered.");
            };
            m1.OnMessageRead = (conv, msgId) => {
                WriteLine($"{msgId} is read.");
                tcs.SetResult(null);
            };
            LCIMTextMessage        textMessage = new LCIMTextMessage("hello");
            LCIMMessageSendOptions options     = new LCIMMessageSendOptions {
                Receipt = true
            };
            await conversation.Send(textMessage, options);

            await tcs.Task;
        }
Пример #2
0
        internal async Task <LCIMMessage> Send(string convId,
                                               LCIMMessage message,
                                               LCIMMessageSendOptions options)
        {
            DirectCommand direct = new DirectCommand {
                FromPeerId = Client.Id,
                Cid        = convId,
            };

            if (message is LCIMTypedMessage typedMessage)
            {
                direct.Msg = JsonConvert.SerializeObject(typedMessage.Encode());
            }
            else if (message is LCIMBinaryMessage binaryMessage)
            {
                direct.BinaryMsg = ByteString.CopyFrom(binaryMessage.Data);
            }
            else
            {
                throw new ArgumentException("Message MUST be LCIMTypedMessage or LCIMBinaryMessage.");
            }
            // 暂态消息
            if (options.Transient)
            {
                direct.Transient = options.Transient;
            }
            // 消息接收回执
            if (options.Receipt)
            {
                direct.R = options.Receipt;
            }
            // 遗愿消息
            if (options.Will)
            {
                direct.Will = options.Will;
            }
            // 推送数据
            if (options.PushData != null)
            {
                direct.PushData = JsonConvert.SerializeObject(options.PushData);
            }
            // 提醒所有人
            if (message.MentionAll)
            {
                direct.MentionAll = message.MentionAll;
            }
            // 提醒用户列表
            if (message.MentionIdList != null &&
                message.MentionIdList.Count > 0)
            {
                direct.MentionPids.AddRange(message.MentionIdList);
            }
            GenericCommand command = NewCommand(CommandType.Direct);

            command.DirectMessage = direct;
            // 优先级
            if (command.Priority > 0)
            {
                command.Priority = (int)options.Priority;
            }
            GenericCommand response = await Connection.SendRequest(command);

            // 消息发送应答
            AckCommand ack = response.AckMessage;

            message.Id            = ack.Uid;
            message.SentTimestamp = ack.T;
            message.FromClientId  = Client.Id;
            return(message);
        }