using System; using System.Collections.Generic; using System.Threading.Tasks; using System.Linq; using LeanCloud.Realtime.Internal.WebSocket; using LeanCloud.Realtime.Protocol; using Google.Protobuf; using Newtonsoft.Json; namespace LeanCloud.Realtime { public class LCIMClient { private string clientId; private LCWebSocketClient client; private Dictionary conversationDict; /// /// 当前用户被加入某个对话的黑名单 /// public Action OnBlocked { get; set; } /// /// 当前客户端被服务端强行下线 /// public Action OnClosed { get; set; } /// /// 客户端连接断开 /// public Action OnDisconnected { get; set; } /// /// 客户端连接恢复正常 /// public Action OnReconnect { get; set; } /// /// 当前用户被添加至某个对话 /// public Action OnInvited { get; set; } /// /// 当前用户被从某个对话中移除 /// public Action OnKicked { get; set; } /// /// 有用户被添加至某个对话 /// public Action, string> OnMembersJoined { get; set; } public Action, string> OnMembersLeft { get; set; } public LCIMClient(string clientId) { this.clientId = clientId; conversationDict = new Dictionary(); } public async Task Open() { client = new LCWebSocketClient { OnNotification = OnNotification }; await client.Connect(); // Open Session GenericCommand command = NewCommand(CommandType.Session, OpType.Open); command.SessionMessage = new SessionCommand(); await client.SendRequest(command); } public async Task CreateChatRoom( string name, Dictionary properties = null) { LCIMChatRoom chatRoom = await CreateConv(name: name, transient: true, properties: properties) as LCIMChatRoom; return chatRoom; } public async Task CreateConversation( IEnumerable members, string name = null, bool unique = true, Dictionary properties = null) { return await CreateConv(members: members, name: name, unique: unique, properties: properties); } public async Task CreateTemporaryConversation( IEnumerable members, int ttl = 86400, Dictionary properties = null) { LCIMTemporaryConversation tempConversation = await CreateConv(members: members, temporary: true, temporaryTtl: ttl, properties: properties) as LCIMTemporaryConversation; return tempConversation; } private async Task CreateConv( IEnumerable members = null, string name = null, bool transient = false, bool unique = true, bool temporary = false, int temporaryTtl = 86400, Dictionary properties = null) { GenericCommand command = NewCommand(CommandType.Conv, OpType.Start); ConvCommand conv = new ConvCommand { Transient = transient, Unique = unique, TempConv = temporary, TempConvTTL = temporaryTtl }; if (members != null) { conv.M.AddRange(members); } if (!string.IsNullOrEmpty(name)) { conv.N = name; } if (properties != null) { conv.Attr = new JsonObjectMessage { Data = JsonConvert.SerializeObject(properties) }; } command.ConvMessage = conv; GenericCommand response = await client.SendRequest(command); LCIMConversation conversation = new LCIMConversation(this); conversation.MergeFrom(response.ConvMessage); conversationDict[conversation.Id] = conversation; return conversation; } public async Task GetConversation(string id) { return null; } public async Task> GetConversationList(List idList) { return null; } public async Task GetConversationQuery() { return null; } private void OnNotification(GenericCommand notification) { switch (notification.Cmd) { case CommandType.Conv: OnConversationNotification(notification); break; default: break; } } private void OnConversationNotification(GenericCommand notification) { switch (notification.Op) { case OpType.Joined: OnConversationJoined(notification.ConvMessage); break; case OpType.MembersJoined: OnConversationMembersJoined(notification.ConvMessage); break; default: break; } } private void OnConversationJoined(ConvCommand conv) { if (conversationDict.TryGetValue(conv.Cid, out LCIMConversation conversation)) { conversation.MergeFrom(conv); } OnInvited?.Invoke(conversation, conv.InitBy); } private void OnConversationMembersJoined(ConvCommand conv) { if (conversationDict.TryGetValue(conv.Cid, out LCIMConversation conversation)) { conversation.MergeFrom(conv); } OnMembersJoined?.Invoke(conversation, conv.M.ToList(), conv.InitBy); } private GenericCommand NewCommand(CommandType cmd, OpType op) { return new GenericCommand { Cmd = cmd, Op = op, AppId = LCApplication.AppId, PeerId = clientId, }; } } }