/// <summary> /// 查询目标对话的历史消息 /// <remarks>不支持聊天室(暂态对话)</remarks> /// </summary> /// <param name="conversation">目标对话</param> /// <param name="beforeMessageId">从 beforeMessageId 开始向前查询(和 beforeTimeStampPoint 共同使用,为防止某毫秒时刻有重复消息)</param> /// <param name="afterMessageId"> 截止到某个 afterMessageId (不包含)</param> /// <param name="beforeTimeStampPoint">从 beforeTimeStampPoint 开始向前查询</param> /// <param name="afterTimeStampPoint">拉取截止到 afterTimeStampPoint 时间戳(不包含)</param> /// <param name="limit">拉取消息条数,默认值 20 条,可设置为 1 - 1000 之间的任意整数</param> /// <returns></returns> public Task <IEnumerable <IAVIMMessage> > QueryMessageAsync(AVIMConversation conversation, string beforeMessageId = null, string afterMessageId = null, DateTime?beforeTimeStampPoint = null, DateTime?afterTimeStampPoint = null, int limit = 20) { var logsCmd = new AVIMCommand() .Command("logs") .Argument("cid", conversation.ConversationId) .Argument("l", limit); if (beforeMessageId != null) { logsCmd = logsCmd.Argument("mid", beforeMessageId); } if (afterMessageId != null) { logsCmd = logsCmd.Argument("tmid", afterMessageId); } if (beforeTimeStampPoint != null) { logsCmd = logsCmd.Argument("t", beforeTimeStampPoint.Value.UnixTimeStampSeconds()); } if (afterTimeStampPoint != null) { logsCmd = logsCmd.Argument("tt", afterTimeStampPoint.Value.UnixTimeStampSeconds()); } return(this.LinkedRealtime.AVIMCommandRunner.RunCommandAsync(logsCmd).OnSuccess(t => { var rtn = new List <IAVIMMessage>(); var result = t.Result.Item2; var logs = result["logs"] as List <object>; if (logs != null) { foreach (var log in logs) { var logMap = log as IDictionary <string, object>; if (logMap != null) { var msgStr = logMap["data"].ToString(); var messageObj = AVRealtime.FreeStyleMessageClassingController.Instantiate(msgStr, logMap); messageObj.ConversationId = conversation.ConversationId; rtn.Add(messageObj); } } } return rtn.AsEnumerable(); })); }
/// <summary> /// Keeps the alive. /// </summary> public void KeepAlive() { try { var cmd = new AVIMCommand(); RunCommandAsync(cmd).ContinueWith(t => { if (t.IsCanceled || t.IsFaulted || t.Exception != null) { InvokeNetworkState(); } }); } catch (Exception ex) { InvokeNetworkState(); } }
internal Task <AVIMCommand> AttachSignature(AVIMCommand command, Task <AVIMSignature> SignatureTask) { var tcs = new TaskCompletionSource <AVIMCommand>(); if (SignatureTask == null) { tcs.SetResult(command); return(tcs.Task); } return(SignatureTask.OnSuccess(_ => { if (_.Result != null) { var signature = _.Result; command.Argument("t", signature.Timestamp); command.Argument("n", signature.Nonce); command.Argument("s", signature.SignatureContent); } return command; })); }
internal Task <AVIMCommand> AttachSignature(AVIMCommand command, Task <AVIMSignature> SignatureTask) { AVRealtime.PrintLog("begin to attach singature."); var tcs = new TaskCompletionSource <AVIMCommand>(); if (SignatureTask == null) { tcs.SetResult(command); return(tcs.Task); } return(SignatureTask.OnSuccess(_ => { if (_.Result != null) { var signature = _.Result; command.Argument("t", signature.Timestamp); command.Argument("n", signature.Nonce); command.Argument("s", signature.SignatureContent); AVRealtime.PrintLog("AttachSignature ended.t:" + signature.Timestamp + ";n:" + signature.Nonce + ";s:" + signature.SignatureContent); } return command; })); }
/// <summary> /// 查询目标对话的历史消息 /// <remarks>不支持聊天室(暂态对话)</remarks> /// </summary> /// <param name="conversation">目标对话</param> /// <param name="beforeMessageId">从 beforeMessageId 开始向前查询(和 beforeTimeStampPoint 共同使用,为防止某毫秒时刻有重复消息)</param> /// <param name="afterMessageId"> 截止到某个 afterMessageId (不包含)</param> /// <param name="beforeTimeStampPoint">从 beforeTimeStampPoint 开始向前查询</param> /// <param name="afterTimeStampPoint">拉取截止到 afterTimeStampPoint 时间戳(不包含)</param> /// <param name="direction">查询方向,默认是 1,如果是 1 表示从新消息往旧消息方向, 0 则相反,其他值无效</param> /// <param name="limit">拉取消息条数,默认值 20 条,可设置为 1 - 1000 之间的任意整数</param> /// <returns></returns> public Task <IEnumerable <T> > QueryMessageAsync <T>(AVIMConversation conversation, string beforeMessageId = null, string afterMessageId = null, DateTime?beforeTimeStampPoint = null, DateTime?afterTimeStampPoint = null, int direction = 1, int limit = 20) where T : IAVIMMessage { var maxLimit = 1000; var actualLimit = limit > maxLimit ? maxLimit : limit; var logsCmd = new AVIMCommand() .Command("logs") .Argument("cid", conversation.ConversationId) .Argument("l", actualLimit); if (beforeMessageId != null) { logsCmd = logsCmd.Argument("mid", beforeMessageId); } if (afterMessageId != null) { logsCmd = logsCmd.Argument("tmid", afterMessageId); } if (beforeTimeStampPoint != null && beforeTimeStampPoint.Value != DateTime.MinValue) { logsCmd = logsCmd.Argument("t", beforeTimeStampPoint.Value.ToUnixTimeStamp()); } if (afterTimeStampPoint != null && afterTimeStampPoint.Value != DateTime.MinValue) { logsCmd = logsCmd.Argument("tt", afterTimeStampPoint.Value.ToUnixTimeStamp()); } if (direction == 0) { logsCmd = logsCmd.Argument("direction", "NEW"); } var subMessageType = typeof(T); var subTypeInteger = subMessageType == typeof(AVIMTypedMessage) ? 0 : FreeStyleMessageClassInfo.GetTypedInteger(subMessageType.GetTypeInfo()); if (subTypeInteger != 0) { logsCmd = logsCmd.Argument("lctype", subTypeInteger); } return(this.RunCommandAsync(logsCmd).OnSuccess(t => { var rtn = new List <IAVIMMessage>(); var result = t.Result.Item2; var logs = result["logs"] as List <object>; if (logs != null) { foreach (var log in logs) { var logMap = log as IDictionary <string, object>; if (logMap != null) { var msgStr = logMap["data"].ToString(); var messageObj = AVRealtime.FreeStyleMessageClassingController.Instantiate(msgStr, logMap); messageObj.ConversationId = conversation.ConversationId; rtn.Add(messageObj); } } } conversation.OnMessageLoad(rtn); return rtn.AsEnumerable().OfType <T>(); })); }
/// <summary> /// Run command. /// </summary> /// <param name="command">Command.</param> public void RunCommand(AVIMCommand command) { command.PeerId(this.ClientId); this.LinkedRealtime.RunCommand(command); }
/// <summary> /// Run command async. /// </summary> /// <returns>The command async.</returns> /// <param name="command">Command.</param> public Task <Tuple <int, IDictionary <string, object> > > RunCommandAsync(AVIMCommand command) { command.PeerId(this.ClientId); return(this.LinkedRealtime.RunCommandAsync(command)); }
public static Task <AVLiveQuery <T> > SubscribeAsync <T>(this AVQuery <T> query, string sessionToken = "", CancellationToken cancellationToken = default(CancellationToken)) where T : AVObject { AVLiveQuery <T> rtn = null; if (sessionToken == "") { if (AVUser.CurrentUser != null) { sessionToken = AVUser.CurrentUser.SessionToken; } } return(CreateAsync(query, sessionToken, cancellationToken).OnSuccess(_ => { rtn = _.Result; if (Open) { return Task.FromResult(rtn); } if (AVRealtime.Instance == null) { throw new NullReferenceException("before subscribe live query, plaese call new AVRealtime(config) to initalize Realtime module."); } return AVRealtime.Instance.OpenAsync().OnSuccess(t => { var liveQueryLogInCmd = new AVIMCommand() .Option("login") .Argument("installationId", rtn.Id) .Argument("service", 1); return AVRealtime.AVIMCommandRunner.RunCommandAsync(liveQueryLogInCmd); }).Unwrap().OnSuccess(s => { Open = true; AVRealtime.Instance.NoticeReceived += (sender, e) => { if (e.CommandName == "data") { var ids = AVDecoder.Instance.DecodeList <string>(e.RawData["ids"]); var msg = e.RawData["msg"] as IEnumerable <object>; if (msg != null) { var receivedPayloads = from item in msg select item as Dictionary <string, object>; if (receivedPayloads != null) { var matchedPayloads = receivedPayloads.Where(item => { if (!item.ContainsKey("query_id")) { return false; } var query_id = item["query_id"].ToString(); return query_id == rtn.QueryId; }); foreach (var payload in matchedPayloads) { var scope = payload["op"].ToString(); var payloadMap = payload["object"] as Dictionary <string, object>; rtn.Emit(scope, payloadMap); } } } } }; return rtn; }); }).Unwrap()); }
/// <summary> /// /// </summary> /// <param name="command"></param> public void RunCommand(AVIMCommand command) { command.AppId(this.CurrentConfiguration.ApplicationId); this.AVIMCommandRunner.RunCommand(command); }
/// <summary> /// send websocket command to Realtime server. /// </summary> /// <param name="command"></param> /// <returns></returns> public Task <Tuple <int, IDictionary <string, object> > > RunCommandAsync(AVIMCommand command) { command.AppId(this.CurrentConfiguration.ApplicationId); return(this.AVIMCommandRunner.RunCommandAsync(command)); }