* LCIMAudioMessage.cs: chore: 完善消息类型

* Program.cs:
* LCIMFileMessage.cs:
* LCIMImageMessage.cs:
* LCIMVideoMessage.cs:
* LCIMLocationMessage.cs:
oneRain 2020-03-18 11:41:13 +08:00
parent 6b4d28b000
commit 29a84b8afb
6 changed files with 140 additions and 18 deletions

View File

@ -1,7 +1,17 @@
using System; using LeanCloud.Storage;
namespace LeanCloud.Realtime.Message {
public class LCIMAudioMessage { namespace LeanCloud.Realtime {
public LCIMAudioMessage() { public class LCIMAudioMessage : LCIMFileMessage {
public double Duration {
get {
if (double.TryParse("duration", out double duration)) {
return duration;
}
return 0;
}
}
public LCIMAudioMessage(LCFile file) : base(file) {
} }
} }
} }

View File

@ -1,7 +1,53 @@
using System; using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using LeanCloud.Storage;
namespace LeanCloud.Realtime { namespace LeanCloud.Realtime {
public class LCIMFileMessage { public class LCIMFileMessage : LCIMTextMessage {
public LCIMFileMessage() { public LCFile File {
get; set;
}
public int Size {
get {
if (int.TryParse(File.MetaData["size"] as string, out int size)) {
return size;
}
return 0;
}
}
public string Format {
get {
return File.MimeType;
}
}
public string Url {
get {
return File.Url;
}
}
public LCIMFileMessage(LCFile file) : base(null) {
File = file;
}
internal override string Serialize() {
if (File == null) {
throw new Exception("File MUST NOT be null before sent.");
}
File.MetaData["name"] = File.Name;
File.MetaData["format"] = File.MimeType;
Dictionary<string, object> data = new Dictionary<string, object> {
{ "objId", File.ObjectId },
{ "url", File.Url },
{ "metaData", File.MetaData }
};
return JsonConvert.SerializeObject(new Dictionary<string, object> {
{ "_lcfile", data }
});
} }
} }
} }

View File

@ -1,7 +1,27 @@
using System; using System;
namespace LeanCloud.Realtime.Message { using LeanCloud.Storage;
public class LCIMImageMessage {
public LCIMImageMessage() { namespace LeanCloud.Realtime {
public class LCIMImageMessage : LCIMFileMessage {
public int Width {
get {
if (int.TryParse(File.MetaData["width"] as string, out int width)) {
return width;
}
return 0;
}
}
public int Height {
get {
if (int.TryParse(File.MetaData["height"] as string, out int height)) {
return height;
}
return 0;
}
}
public LCIMImageMessage(LCFile file) : base(file) {
} }
} }
} }

View File

@ -1,7 +1,25 @@
using System; using System.Collections.Generic;
namespace LeanCloud.Realtime.Message { using Newtonsoft.Json;
public class LCIMLocationMessage { using LeanCloud.Storage;
public LCIMLocationMessage() {
namespace LeanCloud.Realtime {
public class LCIMLocationMessage : LCIMTextMessage {
public LCGeoPoint Location {
get; set;
}
public LCIMLocationMessage(LCGeoPoint locaction) : base(null) {
Location = locaction;
}
internal override string Serialize() {
Dictionary<string, object> data = new Dictionary<string, object> {
{ "longitude", Location.Longitude },
{ "latitude", Location.Latitude }
};
return JsonConvert.SerializeObject(new Dictionary<string, object> {
{ "_lcloc", data }
});
} }
} }
} }

View File

@ -0,0 +1,18 @@
using LeanCloud.Storage;
namespace LeanCloud.Realtime {
public class LCIMVideoMessage : LCIMFileMessage {
public double Duration {
get {
if (double.TryParse(File.MetaData["duration"] as string, out double duration)) {
return duration;
}
return 0;
}
}
public LCIMVideoMessage(LCFile file) : base(file) {
}
}
}

View File

@ -4,6 +4,7 @@ using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using LeanCloud; using LeanCloud;
using LeanCloud.Common; using LeanCloud.Common;
using LeanCloud.Storage;
using LeanCloud.Realtime; using LeanCloud.Realtime;
namespace RealtimeConsole { namespace RealtimeConsole {
@ -56,13 +57,13 @@ namespace RealtimeConsole {
string name = Guid.NewGuid().ToString(); string name = Guid.NewGuid().ToString();
LCIMConversation conversation = await client.CreateConversation(memberIdList, name: name, unique: true); LCIMConversation conversation = await client.CreateConversation(memberIdList, name: name, unique: true);
LCIMTextMessage textMessage = new LCIMTextMessage("hello, world"); //LCIMTextMessage textMessage = new LCIMTextMessage("hello, world");
await conversation.Send(textMessage); //await conversation.Send(textMessage);
await Task.Delay(3000); //await Task.Delay(3000);
LCIMTextMessage newMessage = new LCIMTextMessage("hello, code"); //LCIMTextMessage newMessage = new LCIMTextMessage("hello, code");
await conversation.Update(textMessage, newMessage); //await conversation.Update(textMessage, newMessage);
//// 设置成员的角色 //// 设置成员的角色
//await conversation.UpdateMemberRole("world", LCIMConversationMemberInfo.Manager); //await conversation.UpdateMemberRole("world", LCIMConversationMemberInfo.Manager);
@ -75,6 +76,15 @@ namespace RealtimeConsole {
//LCIMTextMessage textMessage = new LCIMTextMessage("hello, world"); //LCIMTextMessage textMessage = new LCIMTextMessage("hello, world");
//await conversation.Send(textMessage); //await conversation.Send(textMessage);
LCFile file = new LCFile("avatar", "../../../Storage.Test/assets/hello.png");
await file.Save();
LCIMImageMessage imageMessage = new LCIMImageMessage(file);
await conversation.Send(imageMessage);
LCGeoPoint location = new LCGeoPoint(11, 12);
LCIMLocationMessage locationMessage = new LCIMLocationMessage(location);
await conversation.Send(locationMessage);
} }
} }
} }