{"id":19614945,"url":"https://github.com/leancloud/csharp-sdk","last_synced_at":"2025-04-28T02:30:58.599Z","repository":{"id":38842512,"uuid":"197501181","full_name":"leancloud/csharp-sdk","owner":"leancloud","description":"LeanCloud C# SDK","archived":false,"fork":false,"pushed_at":"2024-03-15T08:03:58.000Z","size":68737,"stargazers_count":23,"open_issues_count":3,"forks_count":8,"subscribers_count":12,"default_branch":"master","last_synced_at":"2025-04-05T05:11:23.136Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/leancloud.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2019-07-18T03:02:58.000Z","updated_at":"2025-01-07T12:12:21.000Z","dependencies_parsed_at":"2024-03-04T10:41:04.302Z","dependency_job_id":null,"html_url":"https://github.com/leancloud/csharp-sdk","commit_stats":null,"previous_names":[],"tags_count":85,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leancloud%2Fcsharp-sdk","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leancloud%2Fcsharp-sdk/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leancloud%2Fcsharp-sdk/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leancloud%2Fcsharp-sdk/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/leancloud","download_url":"https://codeload.github.com/leancloud/csharp-sdk/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251237859,"owners_count":21557352,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-11T10:54:35.763Z","updated_at":"2025-04-28T02:30:54.341Z","avatar_url":"https://github.com/leancloud.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# csharp-sdk\n\n![build](https://img.shields.io/github/workflow/status/leancloud/csharp-sdk/.NET)\n![version](https://img.shields.io/github/v/release/leancloud/csharp-sdk?include_prereleases)\n\nLeanCloud C# SDK，基于 .Net Standard 2.0 标准开发，包括服务如下：\n\n- 存储\n- 排行榜\n- 即时通讯\n- Live Query\n- 云引擎\n- 多人对战\n\n参考：[Unity Demo](https://github.com/leancloud/CSharp-SDK-Unity-Demo)\n\n## 安装\n\n### 直接导入\n\n从 [Release](https://github.com/leancloud/csharp-sdk/releases) 下载指定版本 SDK，暂不支持 Nuget 方式。\n\n### UPM\n\n在 Unity 项目的 Packages/manifest.json 中添加依赖项\n\n```json\n\"dependencies\": {\n  \"com.leancloud.storage\": \"https://github.com/leancloud/csharp-sdk-upm.git#storage-2.4.1\",\n  \"com.leancloud.realtime\": \"https://github.com/leancloud/csharp-sdk-upm.git#realtime-2.4.1\",\n  \"com.leancloud.play\": \"https://github.com/leancloud/csharp-sdk-upm.git#play-2.4.1\"\n}\n```\n\n## 编译\n\n从 [Repo](https://github.com/leancloud/csharp-sdk) clone 仓库，使用 Visual Studio 打开 csharp-sdk.sln 编译。\nUnity 用户在编译完成后，请将 XX-Unity 工程中 Debug/Release 的 dlls 拷贝至 Unity 工程下的 Plugins 目录中即可使用。\n其他 .Net 平台用户使用 XX 工程即可。\n（XX 指 Storage，Realtime，LiveQuery 等）\n\n## 测试\n\n- Install .NET SDK 3.1 and 5.0 (use `dotnet --list-sdks` to list all installed versions).\n- Run `dotnet test` in a .sln/.csproj dir.\n\nARM arch Mac users can install x64 arch .NET SDK via https://github.com/isen-ng/homebrew-dotnet-sdk-versions and use `dotnetx64`` instead.\n\n## 项目结构\n\n由于 Unity 平台并不是标准的 .Net Standard 2.0，所以在每个服务下单独拆分出了 XX-Unity 工程，源码和主工程是一致的，只是在依赖库方面有些区别。后面也可能针对 Unity 平台做些相关支持。\n\n```\n├── csharp-sdk.sln              // 项目配置\n├── Common                      // 公共库，包含基础功能\n├── Storage                     // 存储服务\n│   ├── Storage                 // .Net Standard 2.0 工程\n│   ├── Storage-Unity           // Unity 工程\n│   └── Storage.Test            // 单元测试\n├── Realtime                    // 即时通讯服务\n│   ├── Realtime                // .Net Standard 2.0 工程\n│   ├── Realtime-Unity          // Unity 工程\n│   └── Realtime.Test           // 单元测试\n├── LiveQuery                   // LiveQuery 服务\n│   ├── LiveQuery               // .Net Standard 2.0 工程\n│   ├── LiveQuery-Unity         // Unity 工程\n│   └── LiveQuery.Test          // 单元测试\n├── Sample                      // 示例\n│   ├── RealtimeApp             // 即时通讯应用，主要测试断线重连\n│   └── LiveQueryApp            // LiveQuery 应用，主要测试断线重连\n└── UnityLibs                   // Unity 依赖\n    └── Newtonsoft.Json.dll     // Json 库，由于 Unity iOS AOT 的原因，不能使用 .Net Standard 2.0 版本\n```\n\n## 导入\n\n```csharp\nusing LeanCloud;\n// 数据存储\nusing LeanCloud.Storage;\n// 即时通讯\nusing LeanCloud.Realtime;\n// 多人对战\nusing Leancloud.Play;\n```\n\n## 初始化\n\n```csharp\nLCApplication.Initialize(\"ikGGdRE2YcVOemAaRbgp1xGJ-gzGzoHsz\", \"NUKmuRbdAhg1vrb2wexYo1jo\", \"https://ikggdre2.lc-cn-n1-shared.com\");\n```\n\n## 调试\n\n开启调试日志\n\n```csharp\nLCLogger.LogDelegate += (level, info) =\u003e {\n    switch (level) {\n        case LCLogLevel.Debug:\n            WriteLine($\"[DEBUG] {DateTime.Now} {info}\\n\");\n            break;\n        case LCLogLevel.Warn:\n            WriteLine($\"[WARNING] {DateTime.Now} {info}\\n\");\n            break;\n        case LCLogLevel.Error:\n            WriteLine($\"[ERROR] {DateTime.Now} {info}\\n\");\n            break;\n        default:\n            WriteLine(info);\n            break;\n    }\n}\n```\n\n## 数据存储\n\n### 对象\n\n```csharp\nLCObject obj = new LCObject(\"Hello\");\nobj[\"intValue\"] = 123;\nawait obj.Save();\n```\n\n更多关于**对象**用法：[参考](https://github.com/leancloud/csharp-sdk/blob/master/Storage/Storage.Test/ObjectTest.cs)\n\n### 查询\n\n```csharp\nLCQuery\u003cLCObject\u003e query = new LCQuery\u003cLCObject\u003e(\"Hello\");\nquery.Limit(2);\nList\u003cLCObject\u003e list = await query.Find();\n```\n\n更多关于**查询**用法：[参考](https://github.com/leancloud/csharp-sdk/blob/master/Storage/Storage.Test/QueryTest.cs)\n\n### 文件\n\n```csharp\nLCFile file = new LCFile(\"avatar\", AvatarFilePath);\nawait file.Save((count, total) =\u003e {\n    TestContext.WriteLine($\"progress: {count}/{total}\");\n});\n```\n\n更多关于**文件**用法：[参考](https://github.com/leancloud/csharp-sdk/blob/master/Storage/Storage.Test/FileTest.cs)\n\n### 用户\n\n```csharp\nawait LCUser.Login(\"hello\", \"world\");\n```\n\n更多关于**用户**用法：[参考](https://github.com/leancloud/csharp-sdk/blob/master/Storage/Storage.Test/UserTest.cs)\n\n### GeoPoint\n\n```csharp\nLCGeoPoint p1 = new LCGeoPoint(20.0059, 110.3665);\n```\n\n更多关于 **GeoPoint** 用法：[参考](https://github.com/leancloud/csharp-sdk/blob/master/Storage/Storage.Test/GeoTest.cs)\n\n## 即时通讯\n\n### 用户\n\n```csharp\nLCIMClient client = new LCIMClient(\"c1\");\n// 登录\nawait client.Open();\n// 注销\nawait client.Close();\n```\n\n更多关于**用户**用法：[参考](https://github.com/leancloud/csharp-sdk/blob/master/Realtime/Realtime.Test/Client.cs)\n\n### 对话\n\n```csharp\n// 创建普通对话\nLCIMConversation conversation = await client.CreateConversation(new string[] { \"world\" }, name: name, unique: false);\n// 创建聊天室\nLCIMConversation chatroom = await client.CreateChatRoom(name);\n// 创建临时对话\nLCIMConversation tempConversation = await client.CreateTemporaryConversation(new string[] { \"world\" });\n```\n\n更多关于**对话**用法：[参考](https://github.com/leancloud/csharp-sdk/blob/master/Realtime/Realtime.Test/Conversation.cs)\n\n### 消息\n\n```csharp\n// 发送消息\nLCIMTextMessage textMessage = new LCIMTextMessage(\"hello, world\");\nawait conversation.Send(textMessage);\n// 接收消息\nm2.OnMessage = (conv, msg) =\u003e {\n    if (msg is LCIMTextMessage textMsg) {\n        WriteLine($\"text: {textMsg.Text}\");\n    }\n};\n```\n\n更多关于**对话**用法：[参考](https://github.com/leancloud/csharp-sdk/blob/master/Realtime/Realtime.Test/Message.cs)\n\n## 排行榜\n\n### 创建排行榜\n\n```csharp\nLCLeaderboard leaderboard = await LCLeaderboard.CreateLeaderboard(leaderboardName);\n```\n\n### 更新成绩\n\n```csharp\nawait LCLeaderboard.UpdateStatistics(user, new Dictionary\u003cstring, double\u003e {\n    { leaderboardName, 100 }\n});\n```\n\n### 获取成绩\n\n```csharp\nLCUser user = await LCUser.Login(username, password);\nReadOnlyCollection\u003cLCStatistic\u003e statistics = await LCLeaderboard.GetStatistics(user);\nforeach (LCStatistic statistic in statistics) {\n    WriteLine($\"{statistic.Name} : {statistic.Value}\");\n}\n```\n\n### 获取我附近的成绩\n\n```csharp\nawait LCUser.Login(username, password);\nLCLeaderboard leaderboard = LCLeaderboard.CreateWithoutData(leaderboardName);\nReadOnlyCollection\u003cLCRanking\u003e rankings = await leaderboard.GetResultsAroundUser(limit: 5);\nforeach (LCRanking ranking in rankings) {\n    WriteLine($\"{ranking.Rank} : {ranking.User.ObjectId}, {ranking.Value}\");\n}\n```\n\n### 获取榜单\n\n```csharp\nLCLeaderboard leaderboard = LCLeaderboard.CreateWithoutData(leaderboardName);\nReadOnlyCollection\u003cLCRanking\u003e rankings = await leaderboard.GetResults();\nforeach (LCRanking ranking in rankings) {\n    WriteLine($\"{ranking.Rank} : {ranking.User.ObjectId}, {ranking.Value}\");\n}\n```\n\n## LiveQuery\n\n### 订阅\n\n```csharp\nLCQuery\u003cLCObject\u003e query = new LCQuery\u003cLCObject\u003e(\"Account\");\nquery.WhereGreaterThan(\"balance\", 100);\nliveQuery = await query.Subscribe();\n```\n\n### 事件\n\n```csharp\n// 新建符合条件对象的事件\nliveQuery.OnCreate = (obj) =\u003e {\n    WriteLine($\"create: {obj}\");\n};\n\n// 符合条件对象更新事件\nliveQuery.OnUpdate = (obj, updatedKeys) =\u003e {\n    WriteLine($\"update: {obj}\");\n};\n\n// 符合条件对象被删除事件\nliveQuery.OnDelete = (objId) =\u003e {\n    WriteLine($\"delete: {objId}\");\n};\n\n// 有新的符合条件的对象事件\nliveQuery.OnEnter = (obj, updatedKeys) =\u003e {\n    WriteLine($\"enter: {obj}\");\n};\n\n// 有符合条件的对象不再满足条件事件\nliveQuery.OnLeave = (obj, updatedKeys) =\u003e {\n    WriteLine($\"level: {obj}\");\n};\n```\n\n### 特殊事件\n\n当一个用户成功登录应用，OnLogin 事件会被触发。下面的 user 就是登录的 LCUser:\n\n```csharp\nawait LCUser.Login(\"hello\", \"world\");\nLCQuery\u003cLCUser\u003e userQuery = LCUser.GetQuery();\nuserQuery.WhereEqualTo(\"username\", \"hello\");\nLCLiveQuery userLiveQuery = await userQuery.Subscribe();\nuserLiveQuery.OnLogin = (user) =\u003e {\n    WriteLine($\"login: {user}\");\n};\n```\n\n## 云引擎\n\n[脚手架工程](https://github.com/leancloud/dotnet-core-getting-started)\n\n## 文档\n\n[API 文档](https://leancloud.github.io/csharp-sdk/html/index.html)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fleancloud%2Fcsharp-sdk","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fleancloud%2Fcsharp-sdk","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fleancloud%2Fcsharp-sdk/lists"}