From 986252756a2f7b0f3364abd78ebad0e73c95f428 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=99=BA=E8=83=BD=E5=A4=A7=E7=9F=B3=E5=A4=B4?= Date: Thu, 27 Jun 2024 01:07:30 +0800 Subject: [PATCH] =?UTF-8?q?[fix]AppClient=E6=9C=AC=E8=BA=AB=E4=B8=8D?= =?UTF-8?q?=E9=9C=80=E8=A6=81=E7=99=BB=E5=BD=95=EF=BC=8C=E8=80=8C=E5=B7=B2?= =?UTF-8?q?=E9=80=9A=E8=BF=87TokenHttpFilter=E8=BF=87=E6=BB=A4=E5=99=A8?= =?UTF-8?q?=E7=9B=B4=E6=8E=A5=E8=AF=B7=E6=B1=82OAuth=E7=99=BB=E5=BD=95?= =?UTF-8?q?=E6=9D=A5=E8=8E=B7=E5=8F=96=E4=BB=A4=E7=89=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ClientTest/ClientTest.csproj | 2 +- Plugins/MySqlAgent/MySqlAgent.csproj | 2 +- Samples/TestA/TestA.csproj | 2 +- Samples/TestB/TestB.csproj | 2 +- StarAgent/StarAgent.csproj | 2 +- Stardust.Data/Stardust.Data.csproj | 2 +- Stardust.Server/Controllers/AppController.cs | 1 - Stardust.Web/Stardust.Web.csproj | 2 +- Stardust/AppClient.cs | 51 ++++++++++++++------ Stardust/Models/AppModel.cs | 5 +- Stardust/Monitors/StarTracer.cs | 2 - Stardust/StarFactory.cs | 4 +- Stardust/Stardust.csproj | 2 +- Test/Test.csproj | 2 +- 14 files changed, 48 insertions(+), 33 deletions(-) diff --git a/ClientTest/ClientTest.csproj b/ClientTest/ClientTest.csproj index 7bf2ea7b..3a7a4e78 100644 --- a/ClientTest/ClientTest.csproj +++ b/ClientTest/ClientTest.csproj @@ -21,7 +21,7 @@ - + diff --git a/Plugins/MySqlAgent/MySqlAgent.csproj b/Plugins/MySqlAgent/MySqlAgent.csproj index 6da36c94..8833e020 100644 --- a/Plugins/MySqlAgent/MySqlAgent.csproj +++ b/Plugins/MySqlAgent/MySqlAgent.csproj @@ -20,7 +20,7 @@ - + diff --git a/Samples/TestA/TestA.csproj b/Samples/TestA/TestA.csproj index c4d50336..cc18ad33 100644 --- a/Samples/TestA/TestA.csproj +++ b/Samples/TestA/TestA.csproj @@ -18,7 +18,7 @@ - + diff --git a/Samples/TestB/TestB.csproj b/Samples/TestB/TestB.csproj index 23b1ee94..59e9c38b 100644 --- a/Samples/TestB/TestB.csproj +++ b/Samples/TestB/TestB.csproj @@ -18,7 +18,7 @@ - + diff --git a/StarAgent/StarAgent.csproj b/StarAgent/StarAgent.csproj index 4175f9e7..d651b05a 100644 --- a/StarAgent/StarAgent.csproj +++ b/StarAgent/StarAgent.csproj @@ -42,7 +42,7 @@ - + diff --git a/Stardust.Data/Stardust.Data.csproj b/Stardust.Data/Stardust.Data.csproj index b3b0f8d3..7c2c4542 100644 --- a/Stardust.Data/Stardust.Data.csproj +++ b/Stardust.Data/Stardust.Data.csproj @@ -41,7 +41,7 @@ - + diff --git a/Stardust.Server/Controllers/AppController.cs b/Stardust.Server/Controllers/AppController.cs index 2eaa1186..f84fc82b 100644 --- a/Stardust.Server/Controllers/AppController.cs +++ b/Stardust.Server/Controllers/AppController.cs @@ -10,7 +10,6 @@ using NewLife.Serialization; using Stardust.Data; using Stardust.Data.Configs; -using Stardust.Data.Nodes; using Stardust.Models; using Stardust.Server.Services; using WebSocket = System.Net.WebSockets.WebSocket; diff --git a/Stardust.Web/Stardust.Web.csproj b/Stardust.Web/Stardust.Web.csproj index c2acce1c..34a246ad 100644 --- a/Stardust.Web/Stardust.Web.csproj +++ b/Stardust.Web/Stardust.Web.csproj @@ -50,7 +50,7 @@ - + diff --git a/Stardust/AppClient.cs b/Stardust/AppClient.cs index ad050493..3c2b26f8 100644 --- a/Stardust/AppClient.cs +++ b/Stardust/AppClient.cs @@ -69,9 +69,8 @@ public class AppClient : ClientBase, IRegistry /// 实例化 public AppClient() { - Features = Features.Login | Features.Ping | Features.Notify; + Features = Features.Ping | Features.Notify; SetActions("App/"); - Actions[Features.Login] = "App/Register"; // 加载已保存数据 var dic = LoadConsumeServicese(); @@ -125,10 +124,12 @@ public void Start() { try { + Code = AppId; + if (AppId != "StarServer") { // 等待注册到平台 - var task = TaskEx.Run(Login); + var task = TaskEx.Run(Register); task.Wait(1_000); } } @@ -138,6 +139,7 @@ public void Start() } StartTimer(); + Attach(this); } /// 创建Http客户端 @@ -156,21 +158,40 @@ protected override ApiHttpClient CreateHttp(String urls) return client; } - /// 构建登录请求 + /// 注册 /// - public override ILoginRequest BuildLoginRequest() + public async Task Register() { - var inf = new AppModel + try { - AppId = AppId, - AppName = AppName, - ClientId = ClientId, - Version = _version, - NodeCode = NodeCode, - IP = AgentInfo.GetIps() - }; + var inf = new AppModel + { + AppId = AppId, + AppName = AppName, + ClientId = ClientId, + Version = _version, + NodeCode = NodeCode, + IP = AgentInfo.GetIps() + }; - return inf; + var rs = await InvokeAsync("App/Register", inf); + WriteLog("接入星尘服务端:{0}", rs); + + Logined = true; + //if (Filter is NewLife.Http.TokenHttpFilter thf) Token = thf.Token?.AccessToken; + + return rs; + } + catch (Exception ex) + { + if (ex is HttpRequestException) + Log?.Info("注册异常[{0}] {1}", (Client as ApiHttpClient)?.Source, ex.GetTrue()?.Message); + else + Log?.Info(ex.ToString()); + + //throw; + return null; + } } /// 构建心跳请求 @@ -215,7 +236,7 @@ protected override async Task OnPing(Object state) { if (!NetworkInterface.GetIsNetworkAvailable()) return; - var rs = await Login(); + var rs = await Register(); if (rs == null) return; } diff --git a/Stardust/Models/AppModel.cs b/Stardust/Models/AppModel.cs index e0d41ceb..bc1f2465 100644 --- a/Stardust/Models/AppModel.cs +++ b/Stardust/Models/AppModel.cs @@ -3,7 +3,7 @@ namespace Stardust.Models; /// 应用模型 -public class AppModel : ILoginRequest +public class AppModel { /// 应用标识 public String AppId { get; set; } = null!; @@ -22,7 +22,4 @@ public class AppModel : ILoginRequest /// 节点编码 public String? NodeCode { get; set; } - - String? ILoginRequest.Code { get => AppId; set => AppId = value!; } - String? ILoginRequest.Secret { get; set; } } \ No newline at end of file diff --git a/Stardust/Monitors/StarTracer.cs b/Stardust/Monitors/StarTracer.cs index ca426cba..65b2724b 100644 --- a/Stardust/Monitors/StarTracer.cs +++ b/Stardust/Monitors/StarTracer.cs @@ -1,11 +1,9 @@ using System.Collections.Concurrent; using System.Diagnostics; -using System.Net.Http; using System.Net.NetworkInformation; using System.Net.Sockets; using System.Reflection; using NewLife; -using NewLife.Data; using NewLife.Http; using NewLife.Log; using NewLife.Reflection; diff --git a/Stardust/StarFactory.cs b/Stardust/StarFactory.cs index 5eebbe76..9a010d39 100644 --- a/Stardust/StarFactory.cs +++ b/Stardust/StarFactory.cs @@ -334,7 +334,7 @@ private void InitTracer() AppName = AppName, //Secret = Secret, ClientId = ClientId, - Client = _client.Client, + Client = _client, Log = Log }; @@ -366,7 +366,7 @@ public IConfigProvider? Config AppId = AppId!, //Secret = Secret, ClientId = ClientId, - Client = _client.Client, + Client = _client, }; //if (!ClientId.IsNullOrEmpty()) config.ClientId = ClientId; config.Attach(_client); diff --git a/Stardust/Stardust.csproj b/Stardust/Stardust.csproj index d9623844..9dfbf236 100644 --- a/Stardust/Stardust.csproj +++ b/Stardust/Stardust.csproj @@ -106,7 +106,7 @@ - + diff --git a/Test/Test.csproj b/Test/Test.csproj index 02469ad9..0fbd424a 100644 --- a/Test/Test.csproj +++ b/Test/Test.csproj @@ -30,7 +30,7 @@ - +