Skip to content

Commit f20cad3

Browse files
committed
完善推送升级订阅
1 parent 37b2070 commit f20cad3

File tree

6 files changed

+113
-225
lines changed

6 files changed

+113
-225
lines changed

src/c#/GeneralUpdate.Client/Program.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Diagnostics;
22
using System.Text;
33
using GeneralUpdate.ClientCore;
4+
using GeneralUpdate.ClientCore.Hubs;
45
using GeneralUpdate.Common.Download;
56
using GeneralUpdate.Common.Internal;
67
using GeneralUpdate.Common.Internal.Bootstrap;
@@ -68,17 +69,28 @@ private static void Main(string[] args)
6869
.LaunchAsync();
6970
});*/
7071

71-
var paramsOSS = new GlobalConfigInfoOSS();
72+
/*var paramsOSS = new GlobalConfigInfoOSS();
7273
paramsOSS.Url = "http://192.168.50.203/versions.json";
7374
paramsOSS.CurrentVersion = "1.0.0.0";
7475
paramsOSS.VersionFileName = "versions.json";
7576
paramsOSS.AppName = "GeneralUpdate.Client.exe";
7677
paramsOSS.Encoding = Encoding.UTF8.WebName;
77-
GeneralClientOSS.Start(paramsOSS);
78+
GeneralClientOSS.Start(paramsOSS);*/
79+
80+
81+
var hub = new UpgradeHubService("http://localhost:5008/UpgradeHub");
82+
hub.AddReceiveListener(Receive);
83+
hub.StartAsync().Wait();
7884

7985
Console.Read();
8086
}
8187

88+
private static void Receive(string arg1, string arg2)
89+
{
90+
throw new NotImplementedException();
91+
}
92+
93+
8294
private static void OnMultiDownloadError(object arg1, MultiDownloadErrorEventArgs arg2)
8395
{
8496
Debug.WriteLine(arg2.Exception);
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
4+
namespace GeneralUpdate.ClientCore.Hubs;
5+
6+
public interface IUpgradeHubService
7+
{
8+
/// <summary>
9+
/// Add a listener to receive upgrade information pushed from the server.
10+
/// </summary>
11+
/// <param name="receiveMessageCallback">string : group name , string : received message content.</param>
12+
public void AddReceiveListener(Action<string, string> receiveMessageCallback);
13+
14+
/// <summary>
15+
/// Add a listener to receive online and offline notifications.
16+
/// </summary>
17+
/// <param name="onlineMessageCallback">string : Offline or online information.</param>
18+
public void AddOnlineListener(Action<string> onlineMessageCallback);
19+
20+
/// <summary>
21+
/// Add a listener to receive reconnection notifications.
22+
/// </summary>
23+
/// <param name="reconnectedCallback">string? : Reconnection information.</param>
24+
public void AddReconnectedListener(Func<string?, Task>? reconnectedCallback);
25+
26+
/// <summary>
27+
/// Add a listener to receive disconnection notifications.
28+
/// </summary>
29+
/// <param name="closeCallback">Exception? : Offline exception information.</param>
30+
public void AddClosedListener(Func<Exception?, Task> closeCallback);
31+
32+
/// <summary>
33+
/// Start subscribing to upgrade push notifications, and the content of the notifications should be agreed upon independently (it is recommended to use JSON data format).
34+
/// </summary>
35+
public Task StartAsync();
36+
37+
/// <summary>
38+
/// When closing the connection, any ongoing message processing will be completed, but no new messages will be accepted.
39+
/// This should be called before the application closes or goes to sleep, so it can reconnect when it resumes next time.
40+
/// </summary>
41+
public Task StopAsync();
42+
43+
/// <summary>
44+
/// The Hub instance will be completely disposed of and cannot be used for reconnection.
45+
/// </summary>
46+
public Task DisposeAsync();
47+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using Microsoft.AspNetCore.SignalR.Client;
4+
5+
namespace GeneralUpdate.ClientCore.Hubs;
6+
7+
/// <summary>
8+
/// Upgrade the push notification service.
9+
/// </summary>
10+
/// <param name="url">Subscription address, for example: http://127.0.0.1/UpgradeHub</param>
11+
public class UpgradeHubService(string url) : IUpgradeHubService
12+
{
13+
private const string Onlineflag = "Online";
14+
private const string ReceiveMessageflag = "ReceiveMessage";
15+
16+
private readonly HubConnection? _connection = new HubConnectionBuilder()
17+
.WithUrl(url)
18+
.WithAutomaticReconnect(new RandomRetryPolicy())
19+
.Build();
20+
21+
public void AddReceiveListener(Action<string, string> receiveMessageCallback)
22+
=> _connection?.On(ReceiveMessageflag, receiveMessageCallback);
23+
24+
public void AddOnlineListener(Action<string> onlineMessageCallback)
25+
=> _connection?.On(Onlineflag, onlineMessageCallback);
26+
27+
public void AddReconnectedListener(Func<string?, Task>? reconnectedCallback)
28+
=> _connection!.Reconnected += reconnectedCallback;
29+
30+
public void AddClosedListener(Func<Exception?, Task> closeCallback)
31+
=> _connection!.Closed += closeCallback;
32+
33+
public async Task StartAsync()
34+
=> await _connection!.StartAsync();
35+
36+
public async Task StopAsync()
37+
=> await _connection!.StopAsync();
38+
39+
public async Task DisposeAsync()
40+
=> await _connection!.DisposeAsync();
41+
}

src/c#/GeneralUpdate.ClientCore/Hubs/VersionHub.cs

Lines changed: 0 additions & 205 deletions
This file was deleted.

src/c#/GeneralUpdate.Core/GeneralUpdateBootstrap.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Diagnostics;
4-
using System.IO;
54
using System.Runtime.InteropServices;
65
using System.Text;
76
using System.Text.Json;

src/c#/Generalupdate.CatBowl/Program.cs

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,19 @@ class Program
99
{
1010
static void Main(string[] args)
1111
{
12-
var processInfo = TestProcessInfo();
13-
Bowl.Launch(processInfo);
14-
Console.Read();
15-
}
16-
17-
private static MonitorParameter TestProcessInfo()
18-
{
19-
var path = @"D:\packet\test.json";
20-
var json = File.ReadAllText(path);
21-
var processInfo = JsonSerializer.Deserialize<ProcessInfo>(json);
22-
return new MonitorParameter
12+
var installPath = @"D:\github_project\GeneralUpdate\src\c#\GeneralUpdate.CatBowl\bin\Debug\net8.0\";
13+
var lastVersion = "1.0.0.3";
14+
var processInfo = new MonitorParameter
2315
{
24-
ProcessNameOrId = processInfo.AppName,
25-
DumpFileName = $"{processInfo.LastVersion}_fail.dmp",
26-
FailFileName = $"{processInfo.LastVersion}_fail.json",
27-
TargetPath = processInfo.InstallPath,
28-
FailDirectory = Path.Combine(processInfo.InstallPath, "fail", processInfo.LastVersion),
29-
BackupDirectory = Path.Combine(processInfo.InstallPath, processInfo.LastVersion),
16+
ProcessNameOrId = "JsonTest.exe",
17+
DumpFileName = $"{lastVersion}_fail.dmp",
18+
FailFileName = $"{lastVersion}_fail.json",
19+
TargetPath = installPath,
20+
FailDirectory = Path.Combine(installPath, "fail", lastVersion),
21+
BackupDirectory = Path.Combine(installPath, lastVersion),
3022
WorkModel = "Normal"
3123
};
24+
Bowl.Launch(processInfo);
25+
Console.Read();
3226
}
3327
}

0 commit comments

Comments
 (0)