* LCIMClient.cs:
* Message.cs: * LCIMMessage.cs: * Program.cs: * LCIMTextMessage.cs: * LCIMBinaryMessage.cs: * LCIMConversation.cs: * LCIMTypedMessage.cs: chore: 调试简单消息发送
parent
5eae6cfe76
commit
6d4befe446
|
@ -72,7 +72,19 @@ namespace LeanCloud.Realtime {
|
|||
}
|
||||
|
||||
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) {
|
||||
|
|
|
@ -9,12 +9,14 @@ using Newtonsoft.Json;
|
|||
|
||||
namespace LeanCloud.Realtime {
|
||||
public class LCIMClient {
|
||||
private string clientId;
|
||||
|
||||
private LCWebSocketClient client;
|
||||
internal LCWebSocketClient client;
|
||||
|
||||
private Dictionary<string, LCIMConversation> conversationDict;
|
||||
|
||||
public string ClientId {
|
||||
get; private set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 当前用户被加入某个对话的黑名单
|
||||
/// </summary>
|
||||
|
@ -69,7 +71,7 @@ namespace LeanCloud.Realtime {
|
|||
}
|
||||
|
||||
public LCIMClient(string clientId) {
|
||||
this.clientId = clientId;
|
||||
ClientId = clientId;
|
||||
conversationDict = new Dictionary<string, LCIMConversation>();
|
||||
}
|
||||
|
||||
|
@ -135,7 +137,7 @@ namespace LeanCloud.Realtime {
|
|||
}
|
||||
command.ConvMessage = conv;
|
||||
GenericCommand response = await client.SendRequest(command);
|
||||
LCIMConversation conversation = new LCIMConversation(this);
|
||||
LCIMConversation conversation = GetOrCreateConversation(response.ConvMessage.Cid);
|
||||
conversation.MergeFrom(response.ConvMessage);
|
||||
conversationDict[conversation.Id] = conversation;
|
||||
return conversation;
|
||||
|
@ -177,25 +179,39 @@ namespace LeanCloud.Realtime {
|
|||
}
|
||||
|
||||
private void OnConversationJoined(ConvCommand conv) {
|
||||
if (conversationDict.TryGetValue(conv.Cid, out LCIMConversation conversation)) {
|
||||
LCIMConversation conversation = GetOrCreateConversation(conv.Cid);
|
||||
conversation.MergeFrom(conv);
|
||||
}
|
||||
OnInvited?.Invoke(conversation, conv.InitBy);
|
||||
}
|
||||
|
||||
private void OnConversationMembersJoined(ConvCommand conv) {
|
||||
if (conversationDict.TryGetValue(conv.Cid, out LCIMConversation conversation)) {
|
||||
LCIMConversation conversation = GetOrCreateConversation(conv.Cid);
|
||||
conversation.MergeFrom(conv);
|
||||
}
|
||||
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 {
|
||||
Cmd = cmd,
|
||||
Op = op,
|
||||
AppId = LCApplication.AppId,
|
||||
PeerId = clientId,
|
||||
PeerId = ClientId,
|
||||
};
|
||||
}
|
||||
|
||||
internal GenericCommand NewDirectCommand() {
|
||||
return new GenericCommand {
|
||||
Cmd = CommandType.Direct,
|
||||
AppId = LCApplication.AppId,
|
||||
PeerId = ClientId,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -2,7 +2,7 @@
|
|||
using System.Collections.Generic;
|
||||
|
||||
namespace LeanCloud.Realtime {
|
||||
public class LCIMMessage {
|
||||
public abstract class LCIMMessage {
|
||||
public string ConversationId {
|
||||
get; set;
|
||||
}
|
||||
|
@ -15,36 +15,48 @@ namespace LeanCloud.Realtime {
|
|||
get; set;
|
||||
}
|
||||
|
||||
public int SentTimestamp {
|
||||
get; set;
|
||||
public long SentTimestamp {
|
||||
get; internal set;
|
||||
}
|
||||
|
||||
public DateTime SentAt {
|
||||
get; set;
|
||||
get {
|
||||
return DateTimeOffset.FromUnixTimeMilliseconds(SentTimestamp)
|
||||
.LocalDateTime;
|
||||
}
|
||||
}
|
||||
|
||||
public int DeliveredTimestamp {
|
||||
get; set;
|
||||
public long DeliveredTimestamp {
|
||||
get; internal set;
|
||||
}
|
||||
|
||||
public DateTime DeliveredAt {
|
||||
get; set;
|
||||
get {
|
||||
return DateTimeOffset.FromUnixTimeMilliseconds(DeliveredTimestamp)
|
||||
.LocalDateTime;
|
||||
}
|
||||
}
|
||||
|
||||
public int ReadTimestamp {
|
||||
get; set;
|
||||
public long ReadTimestamp {
|
||||
get; internal set;
|
||||
}
|
||||
|
||||
public DateTime ReadAt {
|
||||
get; set;
|
||||
get {
|
||||
return DateTimeOffset.FromUnixTimeMilliseconds(ReadTimestamp)
|
||||
.LocalDateTime;
|
||||
}
|
||||
}
|
||||
|
||||
public int PatchedTimestamp {
|
||||
get; set;
|
||||
public long PatchedTimestamp {
|
||||
get; internal set;
|
||||
}
|
||||
|
||||
public DateTime PatchedAt {
|
||||
get; set;
|
||||
get {
|
||||
return DateTimeOffset.FromUnixTimeMilliseconds(PatchedTimestamp)
|
||||
.LocalDateTime;
|
||||
}
|
||||
}
|
||||
|
||||
public List<string> MentionList {
|
||||
|
@ -55,6 +67,6 @@ namespace LeanCloud.Realtime {
|
|||
|
||||
}
|
||||
|
||||
|
||||
internal abstract string Serialize();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,8 +1,18 @@
|
|||
using System;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace LeanCloud.Realtime {
|
||||
public class LCIMTextMessage {
|
||||
public LCIMTextMessage() {
|
||||
public class LCIMTextMessage : LCIMTypedMessage {
|
||||
const int TextMessageType = -1;
|
||||
|
||||
private string text;
|
||||
|
||||
public LCIMTextMessage(string text) : base(TextMessageType) {
|
||||
this.text = text;
|
||||
}
|
||||
|
||||
internal override string Serialize() {
|
||||
return text;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,15 @@
|
|||
using System;
|
||||
namespace LeanCloud.Realtime.Message {
|
||||
public class LCIMTypedMessage {
|
||||
public LCIMTypedMessage() {
|
||||
|
||||
namespace LeanCloud.Realtime {
|
||||
public class LCIMTypedMessage : LCIMMessage {
|
||||
protected int type;
|
||||
|
||||
protected LCIMTypedMessage(int type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
internal override string Serialize() {
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -54,9 +54,10 @@ namespace RealtimeConsole {
|
|||
|
||||
List<string> memberIdList = new List<string> { "world", "code" };
|
||||
string name = Guid.NewGuid().ToString();
|
||||
_ = await client.CreateTemporaryConversation(memberIdList);
|
||||
//_ = await client.CreateChatRoom(name);
|
||||
//_ = await client.CreateConversation(memberIdList, name: name, unique: false);
|
||||
LCIMConversation conversation = await client.CreateConversation(memberIdList, name: name, unique: false);
|
||||
|
||||
LCIMTextMessage textMessage = new LCIMTextMessage("hello, world");
|
||||
await conversation.Send(textMessage);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue