From c545d9046bfe6edf2657d7386153e651a236b120 Mon Sep 17 00:00:00 2001 From: justerzhu Date: Wed, 27 Nov 2024 20:16:31 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=BC=E5=AE=B9signal=20r=20AOT?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/c#/GeneralUpdate.Client/Program.cs | 14 ++++--- .../Hubs/UpgradeHubService.cs | 39 ++++++++++++------ .../Internal/JsonContext/PacketJsonContext.cs | 7 ++++ .../Shared/Object/Packet.cs | 40 +++++++++++++++++++ 4 files changed, 81 insertions(+), 19 deletions(-) create mode 100644 src/c#/GeneralUpdate.Common/Internal/JsonContext/PacketJsonContext.cs create mode 100644 src/c#/GeneralUpdate.Common/Shared/Object/Packet.cs diff --git a/src/c#/GeneralUpdate.Client/Program.cs b/src/c#/GeneralUpdate.Client/Program.cs index 167e7e36..328636ac 100644 --- a/src/c#/GeneralUpdate.Client/Program.cs +++ b/src/c#/GeneralUpdate.Client/Program.cs @@ -1,5 +1,7 @@ -using System.Text; +using System.Diagnostics; +using System.Text; using GeneralUpdate.ClientCore; +using GeneralUpdate.ClientCore.Hubs; using GeneralUpdate.Common.Download; using GeneralUpdate.Common.Internal; using GeneralUpdate.Common.Internal.Bootstrap; @@ -12,7 +14,7 @@ internal class Program { static async Task Main(string[] args) { - try + /*try { Console.WriteLine($"主程序初始化,{DateTime.Now}!"); Console.WriteLine("当前运行目录:" + Thread.GetDomain().BaseDirectory); @@ -55,7 +57,7 @@ static async Task Main(string[] args) catch (Exception e) { Console.WriteLine(e.Message + "\n" + e.StackTrace); - } + }*/ /*var paramsOSS = new GlobalConfigInfoOSS(); paramsOSS.Url = "http://192.168.50.203/versions.json"; @@ -65,13 +67,13 @@ static async Task Main(string[] args) paramsOSS.Encoding = Encoding.UTF8.WebName; GeneralClientOSS.Start(paramsOSS);*/ - /*var hub = new UpgradeHubService("http://localhost:5000/UpgradeHub" - , null,"a84d21d4-3448-48d4-b418-12c5a7a039cd"); + var hub = new UpgradeHubService("http://localhost:5000/UpgradeHub" + , null,"dfeb5833-975e-4afb-88f1-6278ee9aeff6"); hub.AddListenerReceive((message) => { Debug.WriteLine(message); }); - await hub.StartAsync();*/ + await hub.StartAsync(); /*Task.Run(async () => { diff --git a/src/c#/GeneralUpdate.ClientCore/Hubs/UpgradeHubService.cs b/src/c#/GeneralUpdate.ClientCore/Hubs/UpgradeHubService.cs index 2e2e677a..a583cfec 100644 --- a/src/c#/GeneralUpdate.ClientCore/Hubs/UpgradeHubService.cs +++ b/src/c#/GeneralUpdate.ClientCore/Hubs/UpgradeHubService.cs @@ -1,7 +1,10 @@ using System; using System.Diagnostics; using System.Threading.Tasks; +using GeneralUpdate.Common.Internal.JsonContext; +using Microsoft.AspNetCore.SignalR; using Microsoft.AspNetCore.SignalR.Client; +using Microsoft.Extensions.DependencyInjection; namespace GeneralUpdate.ClientCore.Hubs; @@ -11,23 +14,33 @@ namespace GeneralUpdate.ClientCore.Hubs; /// Subscription address, for example: http://127.0.0.1/UpgradeHub /// ID4 authentication token string. /// Parameters to be sent to the server upon connection (recommended as a JSON string). -public class UpgradeHubService(string url, string? token = null, string? appkey = null) : IUpgradeHubService +public class UpgradeHubService : IUpgradeHubService { private const string Onlineflag = "Online"; private const string ReceiveMessageflag = "ReceiveMessage"; - - private readonly HubConnection? _connection = new HubConnectionBuilder() - .WithUrl(url, config => + private HubConnection? _connection; + + public UpgradeHubService(string url, string? token = null, string? appkey = null) + => _connection = BuildHubConnection(url, token, appkey); + + private HubConnection BuildHubConnection(string url, string? token = null, string? appkey = null) + { + var builder = new HubConnectionBuilder() + .WithUrl(url, config => + { + if (!string.IsNullOrWhiteSpace(token)) + config.AccessTokenProvider = () => Task.FromResult(token); + + if (!string.IsNullOrWhiteSpace(appkey)) + config.Headers.Add("appkey", appkey); + }).WithAutomaticReconnect(new RandomRetryPolicy()); + builder.Services.Configure(o => { - if (!string.IsNullOrWhiteSpace(token)) - config.AccessTokenProvider = () => Task.FromResult(token); - - if (!string.IsNullOrWhiteSpace(appkey)) - config.Headers.Add("appkey", appkey); - }) - .WithAutomaticReconnect(new RandomRetryPolicy()) - .Build(); - + o.PayloadSerializerOptions.TypeInfoResolverChain.Insert(0, PacketJsonContext.Default); + }); + return builder.Build(); + } + public void AddListenerReceive(Action receiveMessageCallback) => _connection?.On(ReceiveMessageflag, receiveMessageCallback); diff --git a/src/c#/GeneralUpdate.Common/Internal/JsonContext/PacketJsonContext.cs b/src/c#/GeneralUpdate.Common/Internal/JsonContext/PacketJsonContext.cs new file mode 100644 index 00000000..7b64503d --- /dev/null +++ b/src/c#/GeneralUpdate.Common/Internal/JsonContext/PacketJsonContext.cs @@ -0,0 +1,7 @@ +using System.Text.Json.Serialization; +using GeneralUpdate.Common.Shared.Object; + +namespace GeneralUpdate.Common.Internal.JsonContext; + +[JsonSerializable(typeof(Packet))] +public partial class PacketJsonContext : JsonSerializerContext; \ No newline at end of file diff --git a/src/c#/GeneralUpdate.Common/Shared/Object/Packet.cs b/src/c#/GeneralUpdate.Common/Shared/Object/Packet.cs new file mode 100644 index 00000000..19fa1f4b --- /dev/null +++ b/src/c#/GeneralUpdate.Common/Shared/Object/Packet.cs @@ -0,0 +1,40 @@ +using System; +using System.Text.Json.Serialization; + +namespace GeneralUpdate.Common.Shared.Object; + +/// +/// Currently used only for upgrade push. +/// +public class Packet +{ + [JsonPropertyName("Name")] + public string? Name { get; set; } + + [JsonPropertyName("Hash")] + public string Hash { get; set; } + + [JsonPropertyName("ReleaseDate")] + public DateTime? ReleaseDate { get; set; } + + [JsonPropertyName("Url")] + public string? Url { get; set; } + + [JsonPropertyName("Version")] + public string? Version { get; set; } + + [JsonPropertyName("AppType")] + public int? AppType { get; set; } + + [JsonPropertyName("Platform")] + public int? Platform { get; set; } + + [JsonPropertyName("ProductId")] + public string? ProductId { get; set; } + + [JsonPropertyName("IsForcibly")] + public bool? IsForcibly { get; set; } + + [JsonPropertyName("IsFreeze")] + public bool? IsFreeze { get; set; } +} \ No newline at end of file