Skip to content

Commit

Permalink
兼容signal r AOT
Browse files Browse the repository at this point in the history
  • Loading branch information
JusterZhu committed Nov 27, 2024
1 parent d266c75 commit c545d90
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 19 deletions.
14 changes: 8 additions & 6 deletions src/c#/GeneralUpdate.Client/Program.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -12,7 +14,7 @@ internal class Program
{
static async Task Main(string[] args)
{
try
/*try
{
Console.WriteLine($"主程序初始化,{DateTime.Now}!");
Console.WriteLine("当前运行目录:" + Thread.GetDomain().BaseDirectory);
Expand Down Expand Up @@ -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";
Expand All @@ -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 () =>
{
Expand Down
39 changes: 26 additions & 13 deletions src/c#/GeneralUpdate.ClientCore/Hubs/UpgradeHubService.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -11,23 +14,33 @@ namespace GeneralUpdate.ClientCore.Hubs;
/// <param name="url">Subscription address, for example: http://127.0.0.1/UpgradeHub</param>
/// <param name="token">ID4 authentication token string.</param>
/// <param name="args">Parameters to be sent to the server upon connection (recommended as a JSON string).</param>
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<JsonHubProtocolOptions>(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<string> receiveMessageCallback)
=> _connection?.On(ReceiveMessageflag, receiveMessageCallback);

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
40 changes: 40 additions & 0 deletions src/c#/GeneralUpdate.Common/Shared/Object/Packet.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using System.Text.Json.Serialization;

namespace GeneralUpdate.Common.Shared.Object;

/// <summary>
/// Currently used only for upgrade push.
/// </summary>
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; }
}

0 comments on commit c545d90

Please sign in to comment.