* LCIMClient.cs:

* Message.cs:
* LCIMMessage.cs:
* Program.cs:
* LCIMTextMessage.cs:
* LCIMBinaryMessage.cs:
* LCIMConversation.cs:

* LCIMTypedMessage.cs: chore: 调试简单消息发送
oneRain 2020-03-16 11:50:49 +08:00
parent 5eae6cfe76
commit 6d4befe446
8 changed files with 152 additions and 36 deletions

View File

@ -72,7 +72,19 @@ namespace LeanCloud.Realtime {
} }
public async Task<LCIMMessage> Send(LCIMMessage message) { public async Task<LCIMMessage> Send(LCIMMessage message) {
return null; DirectCommand direct = new DirectCommand {
FromPeerId = client.ClientId,
Cid = Id,
Msg = message.Serialize(),
};
GenericCommand command = client.NewDirectCommand();
command.DirectMessage = direct;
GenericCommand response = await client.client.SendRequest(command);
// 消息发送应答
AckCommand ack = response.AckMessage;
message.Id = ack.Uid;
message.DeliveredTimestamp = ack.T;
return message;
} }
public async Task<LCIMRecalledMessage> Recall(LCIMMessage message) { public async Task<LCIMRecalledMessage> Recall(LCIMMessage message) {

View File

@ -9,12 +9,14 @@ using Newtonsoft.Json;
namespace LeanCloud.Realtime { namespace LeanCloud.Realtime {
public class LCIMClient { public class LCIMClient {
private string clientId; internal LCWebSocketClient client;
private LCWebSocketClient client;
private Dictionary<string, LCIMConversation> conversationDict; private Dictionary<string, LCIMConversation> conversationDict;
public string ClientId {
get; private set;
}
/// <summary> /// <summary>
/// 当前用户被加入某个对话的黑名单 /// 当前用户被加入某个对话的黑名单
/// </summary> /// </summary>
@ -69,7 +71,7 @@ namespace LeanCloud.Realtime {
} }
public LCIMClient(string clientId) { public LCIMClient(string clientId) {
this.clientId = clientId; ClientId = clientId;
conversationDict = new Dictionary<string, LCIMConversation>(); conversationDict = new Dictionary<string, LCIMConversation>();
} }
@ -135,7 +137,7 @@ namespace LeanCloud.Realtime {
} }
command.ConvMessage = conv; command.ConvMessage = conv;
GenericCommand response = await client.SendRequest(command); GenericCommand response = await client.SendRequest(command);
LCIMConversation conversation = new LCIMConversation(this); LCIMConversation conversation = GetOrCreateConversation(response.ConvMessage.Cid);
conversation.MergeFrom(response.ConvMessage); conversation.MergeFrom(response.ConvMessage);
conversationDict[conversation.Id] = conversation; conversationDict[conversation.Id] = conversation;
return conversation; return conversation;
@ -177,25 +179,39 @@ namespace LeanCloud.Realtime {
} }
private void OnConversationJoined(ConvCommand conv) { private void OnConversationJoined(ConvCommand conv) {
if (conversationDict.TryGetValue(conv.Cid, out LCIMConversation conversation)) { LCIMConversation conversation = GetOrCreateConversation(conv.Cid);
conversation.MergeFrom(conv); conversation.MergeFrom(conv);
}
OnInvited?.Invoke(conversation, conv.InitBy); OnInvited?.Invoke(conversation, conv.InitBy);
} }
private void OnConversationMembersJoined(ConvCommand conv) { private void OnConversationMembersJoined(ConvCommand conv) {
if (conversationDict.TryGetValue(conv.Cid, out LCIMConversation conversation)) { LCIMConversation conversation = GetOrCreateConversation(conv.Cid);
conversation.MergeFrom(conv); conversation.MergeFrom(conv);
}
OnMembersJoined?.Invoke(conversation, conv.M.ToList(), conv.InitBy); OnMembersJoined?.Invoke(conversation, conv.M.ToList(), conv.InitBy);
} }
private GenericCommand NewCommand(CommandType cmd, OpType op) { private LCIMConversation GetOrCreateConversation(string convId) {
if (!conversationDict.TryGetValue(convId, out LCIMConversation conversation)) {
conversation = new LCIMConversation(this);
conversationDict.Add(convId, conversation);
}
return conversation;
}
internal GenericCommand NewCommand(CommandType cmd, OpType op) {
return new GenericCommand { return new GenericCommand {
Cmd = cmd, Cmd = cmd,
Op = op, Op = op,
AppId = LCApplication.AppId, AppId = LCApplication.AppId,
PeerId = clientId, PeerId = ClientId,
};
}
internal GenericCommand NewDirectCommand() {
return new GenericCommand {
Cmd = CommandType.Direct,
AppId = LCApplication.AppId,
PeerId = ClientId,
}; };
} }
} }

View File

@ -0,0 +1,15 @@
using System;
namespace LeanCloud.Realtime {
public class LCIMBinaryMessage : LCIMMessage {
private byte[] data;
public LCIMBinaryMessage(byte[] data) {
this.data = data;
}
internal override string Serialize() {
throw new NotImplementedException();
}
}
}

View File

@ -2,7 +2,7 @@
using System.Collections.Generic; using System.Collections.Generic;
namespace LeanCloud.Realtime { namespace LeanCloud.Realtime {
public class LCIMMessage { public abstract class LCIMMessage {
public string ConversationId { public string ConversationId {
get; set; get; set;
} }
@ -15,36 +15,48 @@ namespace LeanCloud.Realtime {
get; set; get; set;
} }
public int SentTimestamp { public long SentTimestamp {
get; set; get; internal set;
} }
public DateTime SentAt { public DateTime SentAt {
get; set; get {
return DateTimeOffset.FromUnixTimeMilliseconds(SentTimestamp)
.LocalDateTime;
}
} }
public int DeliveredTimestamp { public long DeliveredTimestamp {
get; set; get; internal set;
} }
public DateTime DeliveredAt { public DateTime DeliveredAt {
get; set; get {
return DateTimeOffset.FromUnixTimeMilliseconds(DeliveredTimestamp)
.LocalDateTime;
}
} }
public int ReadTimestamp { public long ReadTimestamp {
get; set; get; internal set;
} }
public DateTime ReadAt { public DateTime ReadAt {
get; set; get {
return DateTimeOffset.FromUnixTimeMilliseconds(ReadTimestamp)
.LocalDateTime;
}
} }
public int PatchedTimestamp { public long PatchedTimestamp {
get; set; get; internal set;
} }
public DateTime PatchedAt { public DateTime PatchedAt {
get; set; get {
return DateTimeOffset.FromUnixTimeMilliseconds(PatchedTimestamp)
.LocalDateTime;
}
} }
public List<string> MentionList { public List<string> MentionList {
@ -55,6 +67,6 @@ namespace LeanCloud.Realtime {
} }
internal abstract string Serialize();
} }
} }

View File

@ -1,8 +1,18 @@
using System; using System;
using Newtonsoft.Json;
namespace LeanCloud.Realtime { namespace LeanCloud.Realtime {
public class LCIMTextMessage { public class LCIMTextMessage : LCIMTypedMessage {
public LCIMTextMessage() { const int TextMessageType = -1;
private string text;
public LCIMTextMessage(string text) : base(TextMessageType) {
this.text = text;
}
internal override string Serialize() {
return text;
} }
} }
} }

View File

@ -1,7 +1,15 @@
using System; using System;
namespace LeanCloud.Realtime.Message {
public class LCIMTypedMessage { namespace LeanCloud.Realtime {
public LCIMTypedMessage() { public class LCIMTypedMessage : LCIMMessage {
protected int type;
protected LCIMTypedMessage(int type) {
this.type = type;
}
internal override string Serialize() {
throw new NotImplementedException();
} }
} }
} }

View File

@ -0,0 +1,42 @@
using NUnit.Framework;
using System;
using System.Threading.Tasks;
using System.Collections.Generic;
using LeanCloud;
using LeanCloud.Common;
using LeanCloud.Realtime;
namespace Realtime.Test {
public class Message {
[SetUp]
public void SetUp() {
LCLogger.LogDelegate += Utils.Print;
LCApplication.Initialize("ikGGdRE2YcVOemAaRbgp1xGJ-gzGzoHsz", "NUKmuRbdAhg1vrb2wexYo1jo", "https://ikggdre2.lc-cn-n1-shared.com");
}
[TearDown]
public void TearDown() {
LCLogger.LogDelegate -= Utils.Print;
}
[Test]
public async Task Send() {
try {
string clientId = Guid.NewGuid().ToString();
LCIMClient client = new LCIMClient(clientId);
await client.Open();
List<string> memberIdList = new List<string> { "world" };
string name = Guid.NewGuid().ToString();
LCIMConversation conversation = await client.CreateConversation(memberIdList, name: name, unique: false);
LCIMTextMessage textMessage = new LCIMTextMessage("hello, world");
await conversation.Send(textMessage);
TestContext.WriteLine(textMessage.Id);
TestContext.WriteLine(textMessage.DeliveredAt);
Assert.NotNull(textMessage.Id);
} catch (Exception e) {
LCLogger.Error(e.Message);
}
}
}
}

View File

@ -54,9 +54,10 @@ namespace RealtimeConsole {
List<string> memberIdList = new List<string> { "world", "code" }; List<string> memberIdList = new List<string> { "world", "code" };
string name = Guid.NewGuid().ToString(); string name = Guid.NewGuid().ToString();
_ = await client.CreateTemporaryConversation(memberIdList); LCIMConversation conversation = await client.CreateConversation(memberIdList, name: name, unique: false);
//_ = await client.CreateChatRoom(name);
//_ = await client.CreateConversation(memberIdList, name: name, unique: false); LCIMTextMessage textMessage = new LCIMTextMessage("hello, world");
await conversation.Send(textMessage);
} }
} }
} }