Go to file
oneRain 659db5117c
Merge pull request #66 from onerain88/doc
Doc
2020-05-14 10:49:38 +08:00
.vscode chore 2020-05-06 17:33:37 +08:00
Common chore: LiveQuery 打包 2020-05-13 17:38:33 +08:00
LiveQuery chore 2020-05-13 17:18:49 +08:00
Realtime chore: 调整接口访问权限 2020-05-13 16:28:55 +08:00
Sample chore 2020-05-13 17:18:59 +08:00
Storage doc: api 2020-05-14 10:17:15 +08:00
UnityLibs chore: 整理工程目录 2020-04-30 15:36:06 +08:00
assets chore: 整理工程目录 2020-04-30 15:36:06 +08:00
script chore: LiveQuery 打包 2020-05-13 17:38:33 +08:00
.gitignore chore: Log code 2020-04-30 16:03:45 +08:00
.travis.yml release 2020-05-13 18:02:52 +08:00
Doxyfile doc: api 2020-05-14 10:17:15 +08:00
LICENSE Initial commit 2019-07-18 11:02:59 +08:00
README.md doc: readme 2020-05-14 10:39:51 +08:00
csharp-sdk.sln doc: readme 2020-05-14 10:39:51 +08:00

README.md

csharp-sdk

LeanCloud 数据存储,即时通讯 C# SDK基于 .Net Standard 2.0 标准开发。

安装

Release 下载指定版本 SDK暂不支持 Nuget 方式。

导入

using LeanCloud;
// 数据存储
using LeanCloud.Storage;
// 即时通讯
using LeanCloud.Realtime;

初始化

LCApplication.Initialize("ikGGdRE2YcVOemAaRbgp1xGJ-gzGzoHsz", "NUKmuRbdAhg1vrb2wexYo1jo", "https://ikggdre2.lc-cn-n1-shared.com");

调试

开启调试日志

LCLogger.LogDelegate += (level, info) => {
    switch (level) {
        case LCLogLevel.Debug:
            WriteLine($"[DEBUG] {DateTime.Now} {info}\n");
            break;
        case LCLogLevel.Warn:
            WriteLine($"[WARNING] {DateTime.Now} {info}\n");
            break;
        case LCLogLevel.Error:
            WriteLine($"[ERROR] {DateTime.Now} {info}\n");
            break;
        default:
            WriteLine(info);
            break;
    }
}

数据存储

对象

LCObject obj = new LCObject("Hello");
obj["intValue"] = 123;
await obj.Save();

更多关于对象用法:参考

查询

LCQuery<LCObject> query = new LCQuery<LCObject>("Hello");
query.Limit(2);
List<LCObject> list = await query.Find();

更多关于查询用法:参考

文件

LCFile file = new LCFile("avatar", AvatarFilePath);
await file.Save((count, total) => {
    TestContext.WriteLine($"progress: {count}/{total}");
});

更多关于文件用法:参考

用户

await LCUser.Login("hello", "world");

更多关于用户用法:参考

GeoPoint

LCGeoPoint p1 = new LCGeoPoint(20.0059, 110.3665);

更多关于 GeoPoint 用法:参考

即时通讯

用户

LCIMClient client = new LCIMClient("c1");
// 登录
await client.Open();
// 注销
await client.Close();

更多关于用户用法:参考

对话

// 创建普通对话
LCIMConversation conversation = await client.CreateConversation(new string[] { "world" }, name: name, unique: false);
// 创建聊天室
LCIMConversation chatroom = await client.CreateChatRoom(name);
// 创建临时对话
LCIMConversation tempConversation = await client.CreateTemporaryConversation(new string[] { "world" });

更多关于对话用法:参考

消息

// 发送消息
LCIMTextMessage textMessage = new LCIMTextMessage("hello, world");
await conversation.Send(textMessage);
// 接收消息
m2.OnMessage = (conv, msg) => {
    if (msg is LCIMTextMessage textMsg) {
        WriteLine($"text: {textMsg.Text}");
    }
};

更多关于对话用法:参考

排行榜

创建排行榜

LCLeaderboard leaderboard = await LCLeaderboard.CreateLeaderboard(leaderboardName);

更新成绩

await LCLeaderboard.UpdateStatistics(user, new Dictionary<string, double> {
    { leaderboardName, 100 }
});

获取成绩

LCUser user = await LCUser.Login(username, password);
ReadOnlyCollection<LCStatistic> statistics = await LCLeaderboard.GetStatistics(user);
foreach (LCStatistic statistic in statistics) {
    WriteLine($"{statistic.Name} : {statistic.Value}");
}

获取我附近的成绩

await LCUser.Login(username, password);
LCLeaderboard leaderboard = LCLeaderboard.CreateWithoutData(leaderboardName);
ReadOnlyCollection<LCRanking> rankings = await leaderboard.GetResultsAroundUser(limit: 5);
foreach (LCRanking ranking in rankings) {
    WriteLine($"{ranking.Rank} : {ranking.User.ObjectId}, {ranking.Value}");
}

获取榜单

LCLeaderboard leaderboard = LCLeaderboard.CreateWithoutData(leaderboardName);
ReadOnlyCollection<LCRanking> rankings = await leaderboard.GetResults();
foreach (LCRanking ranking in rankings) {
    WriteLine($"{ranking.Rank} : {ranking.User.ObjectId}, {ranking.Value}");
}

LiveQuery

订阅

LCQuery<LCObject> query = new LCQuery<LCObject>("Account");
query.WhereGreaterThan("balance", 100);
liveQuery = await query.Subscribe();

事件

// 新建符合条件对象的事件
liveQuery.OnCreate = (obj) => {
    WriteLine($"create: {obj}");
};

// 符合条件对象更新事件
liveQuery.OnUpdate = (obj, updatedKeys) => {
    WriteLine($"update: {obj}");
};

// 符合条件对象被删除事件
liveQuery.OnDelete = (objId) => {
    WriteLine($"delete: {objId}");
};

// 有新的符合条件的对象事件
liveQuery.OnEnter = (obj, updatedKeys) => {
    WriteLine($"enter: {obj}");
};

// 有符合条件的对象不再满足条件事件
liveQuery.OnLeave = (obj, updatedKeys) => {
    WriteLine($"level: {obj}");
};

特殊事件

当一个用户成功登录应用OnLogin 事件会被触发。下面的 user 就是登录的 LCUser:

await LCUser.Login("hello", "world");
LCQuery<LCUser> userQuery = LCUser.GetQuery();
userQuery.WhereEqualTo("username", "hello");
LCLiveQuery userLiveQuery = await userQuery.Subscribe();
userLiveQuery.OnLogin = (user) => {
    WriteLine($"login: {user}");
};

文档

API 文档