/// <summary> /// 打开会话 /// </summary> /// <returns>The session.</returns> internal Task OpenSession(AVIMClient client) { var tcs = new TaskCompletionSource <bool>(); var sessionOpen = new SessionCommand { configBitmap = 1, Ua = "net-universal/1.0.6999.29889", N = null, T = 0, S = null, }; var cmd = commandFactory.NewRequest(client.ClientId, CommandType.Session, OpType.Open); cmd.sessionMessage = sessionOpen; SendRequest(cmd).ContinueWith(t => { AVRealtime.Context.Post(() => { if (t.IsFaulted) { AVRealtime.PrintLog("open session error"); tcs.SetException(t.Exception.InnerException); } else { var res = t.Result; var sessionOpened = res.sessionMessage; // TODO 判断会话打开结果 idToClient.Add(client.ClientId, client); tcs.SetResult(true); } }); }); return(tcs.Task); }
internal AVIMClient PreLogIn(string clientId, string tag = null, string deviceId = null) { var client = new AVIMClient(clientId, tag, this); if (this.OnOfflineMessageReceived != null) { client.OnOfflineMessageReceived += this.OnOfflineMessageReceived; } _clientId = clientId; _tag = tag; _deviceId = deviceId; if (_tag != null) { if (deviceId == null) { throw new ArgumentNullException(deviceId, "当 tag 不为空时,必须传入当前设备不变的唯一 id(deviceId)"); } } if (string.IsNullOrEmpty(clientId)) { throw new Exception("当前 ClientId 为空,无法登录服务器。"); } return(client); }
/// <summary> /// 从本地构建一个对话 /// </summary> /// <param name="convId">对话的 objectId</param> /// <param name="client"></param> /// <returns></returns> public static AVIMConversation CreateWithoutData(string convId, AVIMClient client) { return(new AVIMConversation() { ConversationId = convId, CurrentClient = client }); }
internal void AfterLogIn(AVIMClient client) { if (clients == null) { clients = new Dictionary <string, AVIMClient>(); } clients[client.ClientId] = client; }
/// <summary> /// 创建 Client /// </summary> /// <param name="clientId"></param> /// <param name="tag"></param> /// <param name="deviceId">设备唯一的 Id。如果是 iOS 设备,需要将 iOS 推送使用的 DeviceToken 作为 deviceId 传入</param> /// <param name="cancellationToken"></param> /// <returns></returns> public Task <AVIMClient> CreateClient( string clientId, string tag = null, string deviceId = null, CancellationToken cancellationToken = default(CancellationToken)) { _clientId = clientId; _tag = tag; if (_tag != null) { if (deviceId == null) { throw new ArgumentNullException(deviceId, "当 tag 不为空时,必须传入当前设备不变的唯一 id(deviceId)"); } } if (string.IsNullOrEmpty(clientId)) { throw new Exception("当前 ClientId 为空,无法登录服务器。"); } return(OpenAsync(cancellationToken).OnSuccess(t => { ToggleNotification(true); var cmd = new SessionCommand() .UA(VersionString) .Tag(tag) .Argument("deviceId", deviceId) .Option("open") .PeerId(clientId); return AttachSignature(cmd, this.SignatureFactory.CreateConnectSignature(clientId)).OnSuccess(_ => { return AVIMCommandRunner.RunCommandAsync(cmd); }).Unwrap(); }).Unwrap().OnSuccess(s => { if (s.Exception != null) { var imException = s.Exception.InnerException as AVIMException; } state = Status.Online; var response = s.Result.Item2; if (response.ContainsKey("st")) { _sesstionToken = response["st"] as string; } if (response.ContainsKey("stTtl")) { var stTtl = long.Parse(response["stTtl"].ToString()); _sesstionTokenExpire = DateTime.Now.UnixTimeStampSeconds() + stTtl; } var client = new AVIMClient(clientId, tag, this); return client; })); }
internal void AfterLogIn(AVIMClient client) { if (clients == null) { clients = new Dictionary <string, AVIMClient>(); } client.OnSessionClosed += (sender, e) => { string clientId = (sender as AVIMClient).ClientId; clients.Remove(clientId); if (clients.Count == 0) { LogOut(); } }; clients[client.ClientId] = client; }
/// <summary> /// Query messages. /// </summary> /// <returns>The message async.</returns> /// <param name="client">Client.</param> /// <param name="conversation">Conversation.</param> /// <param name="beforeMessageId">Before message identifier.</param> /// <param name="afterMessageId">After message identifier.</param> /// <param name="beforeTimeStampPoint">Before time stamp point.</param> /// <param name="afterTimeStampPoint">After time stamp point.</param> /// <param name="direction">Direction.</param> /// <param name="limit">Limit.</param> public static Task <IEnumerable <IAVIMMessage> > QueryMessageAsync(this AVIMClient client, AVIMConversation conversation, string beforeMessageId = null, string afterMessageId = null, DateTime?beforeTimeStampPoint = null, DateTime?afterTimeStampPoint = null, int direction = 1, int limit = 20) { return(client.QueryMessageAsync <IAVIMMessage>(conversation, beforeMessageId, afterMessageId, beforeTimeStampPoint, afterTimeStampPoint, direction, limit)); }
/// <summary> /// 获取 AVIMClient,如果不存在,则实例化 /// </summary> /// <returns>The client.</returns> /// <param name="clientId">Client identifier.</param> public static AVIMClient GetInstance(string clientId) { // TODO 判断 clientId 合法性 if (string.IsNullOrEmpty(clientId)) { throw new Exception("client id is null"); } lock (clients) { if (clients.TryGetValue(clientId, out var client)) { return(client); } var newClient = new AVIMClient(clientId); clients.Add(clientId, newClient); return(newClient); } }
/// <summary> /// Creates the client async. /// </summary> /// <returns>The client async.</returns> /// <param name="user">User.</param> /// <param name="tag">Tag.</param> /// <param name="deviceId">Device identifier.</param> /// <param name="secure">If set to <c>true</c> secure.</param> public Task <AVIMClient> CreateClientAsync(AVUser user = null, string tag = null, string deviceId = null, bool secure = true, CancellationToken cancellationToken = default(CancellationToken)) { AVIMClient client = null; AVRealtime.PrintLog("begin OpenAsync."); return(OpenAsync(secure, Subprotocol, true, cancellationToken).OnSuccess(openTask => { AVRealtime.PrintLog("OpenAsync OnSuccess. begin send open sesstion cmd."); var userTask = Task.FromResult(user); if (user == null) { userTask = AVUser.GetCurrentUserAsync(); } return userTask; }).Unwrap().OnSuccess(u => { var theUser = u.Result; return AVCloud.RequestRealtimeSignatureAsync(theUser); }).Unwrap().OnSuccess(signTask => { var signResult = signTask.Result; var clientId = signResult.ClientId; var nonce = signResult.Nonce; var singnature = signResult.Signature; var ts = signResult.Timestamp; client = PreLogIn(clientId, tag, deviceId); ToggleNotification(true); return this.OpenSessionAsync(clientId, tag, deviceId, nonce, ts, singnature, secure); }).Unwrap().OnSuccess(s => { ToggleHeartBeating(true); AfterLogIn(client); return client; })); }
internal Task <AVIMMessage> SendMessageAsync(AVIMClient client, AVIMConversation conversation, AVIMMessage message) { var tcs = new TaskCompletionSource <AVIMMessage>(); var sendMsg = new DirectCommand { // TODO 完善消息体 Cid = conversation.convId, Msg = message.ToString() }; var cmd = commandFactory.NewRequest(client.ClientId, CommandType.Direct); cmd.directMessage = sendMsg; SendRequest(cmd).ContinueWith(t => { if (t.IsFaulted) { tcs.SetException(t.Exception.InnerException); } else { tcs.SetResult(message); } }); return(tcs.Task); }
/// <summary> /// Creates the client async. /// </summary> /// <returns>The client async.</returns> /// <param name="user">User.</param> /// <param name="tag">Tag.</param> /// <param name="deviceId">Device identifier.</param> /// <param name="secure">If set to <c>true</c> secure.</param> public Task <AVIMClient> CreateClientAsync(AVUser user = null, string tag = null, string deviceId = null, bool secure = true) { var userTask = Task.FromResult(user); if (user == null) { userTask = AVUser.GetCurrentUserAsync(); } AVIMClient client = null; return(userTask.OnSuccess(u => { var theUser = u.Result; return AVCloud.RequestRealtimeSignatureAsync(theUser); }).Unwrap().OnSuccess(signTask => { var signResult = signTask.Result; var clientId = signResult.ClientId; var nonce = signResult.Nonce; var singnature = signResult.Signature; var ts = signResult.Timestamp; client = PreLogIn(clientId, tag, deviceId); ToggleNotification(true); return this.OpenSessionAsync(clientId, tag, deviceId, nonce, ts, singnature, secure); }).Unwrap().OnSuccess(s => { ToggleHeartBeating(_heartBeatingToggle); return client; })); }
/// <summary> /// Get the chat room query. /// </summary> /// <returns>The chat room query.</returns> /// <param name="client">Client.</param> public static AVIMConversationQuery GetChatRoomQuery(this AVIMClient client) { return(client.GetQuery().WhereEqualTo("tr", true)); }
/// <summary> /// Leave conversation async. /// </summary> /// <returns>The async.</returns> /// <param name="client">Client.</param> /// <param name="conversationId">Conversation identifier.</param> public static Task LeaveAsync(this AVIMClient client, string conversationId) { var conversation = client.GetConversation(conversationId); return(client.LeaveAsync(conversation)); }
/// <summary> /// Get conversation. /// </summary> /// <returns>The conversation.</returns> /// <param name="client">Client.</param> /// <param name="conversationId">Conversation identifier.</param> public static AVIMConversation GetConversation(this AVIMClient client, string conversationId) { return(AVIMConversation.CreateWithoutData(conversationId, client)); }
public static Task <AVIMConversation> CreateConversationAsync(this AVIMClient client, IEnumerable <string> members, string conversationName) { return(client.CreateConversationAsync(members: members, name: conversationName)); }
internal AVIMConversationQuery(AVIMClient _currentClient) : base() { CurrentClient = _currentClient; }
public static AVIMConversation CreateWithData(IEnumerable <KeyValuePair <string, object> > magicFields, AVIMClient client) { if (magicFields is AVObject) { return(new AVIMConversation(state: (AVObject)magicFields) { CurrentClient = client }); } return(new AVIMConversation(attributes: magicFields) { CurrentClient = client }); }