Skip to content

Commit 7da00e3

Browse files
committed
更新使用示例
1 parent d3733b0 commit 7da00e3

File tree

17 files changed

+364
-356
lines changed

17 files changed

+364
-356
lines changed

src/Client/ClientSample.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
</ItemGroup>
1515

1616
<ItemGroup>
17-
<PackageReference Include="GeneralUpdate.ClientCore" Version="2.12.10" />
17+
<PackageReference Include="GeneralUpdate.ClientCore" Version="2.12.11" />
1818
</ItemGroup>
1919

2020
<ItemGroup>

src/Client/Program.cs

Lines changed: 59 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -1,160 +1,80 @@
11
using System.Text;
22
using GeneralUpdate.ClientCore;
3-
using GeneralUpdate.Core.Bootstrap;
4-
using GeneralUpdate.Core.Domain.Entity;
5-
using GeneralUpdate.Core.Domain.Enum;
6-
using GeneralUpdate.Core.Events.CommonArgs;
7-
using GeneralUpdate.Core.Events.MultiEventArgs;
8-
using GeneralUpdate.Core.Strategys.PlatformWindows;
3+
using GeneralUpdate.Common.Download;
4+
using GeneralUpdate.Common.Internal;
5+
using GeneralUpdate.Common.Internal.Bootstrap;
6+
using GeneralUpdate.Common.Shared.Object;
97

10-
//ClientStrategy该更新策略将完成1.自动升级组件自更新 2.启动更新组件 3.配置好ClientParameter无需再像之前的版本写args数组进程通讯了。
11-
//generalClientBootstrap.Config(baseUrl, "B8A7FADD-386C-46B0-B283-C9F963420C7C").
12-
13-
var baseUrl = "127.0.0.1";
14-
var configinfo = GetWindowsConfigInfo();
15-
var generalClientBootstrap = await new GeneralClientBootstrap()
16-
//单个或多个更新包下载通知事件
17-
.AddListenerMultiDownloadProgress(OnMultiDownloadProgressChanged)
18-
//单个或多个更新包下载速度、剩余下载事件、当前下载版本信息通知事件
19-
.AddListenerMultiDownloadStatistics(OnMultiDownloadStatistics)
20-
//单个或多个更新包下载完成
21-
.AddListenerMultiDownloadCompleted(OnMultiDownloadCompleted)
22-
//完成所有的下载任务通知
23-
.AddListenerMultiAllDownloadCompleted(OnMultiAllDownloadCompleted)
24-
//下载过程出现的异常通知
25-
.AddListenerMultiDownloadError(OnMultiDownloadError)
26-
//整个更新过程出现的任何问题都会通过这个事件通知
27-
.AddListenerException(OnException)
28-
.Config(configinfo)
29-
.Option(UpdateOption.DownloadTimeOut, 60)
30-
.Option(UpdateOption.Encoding, Encoding.Default)
31-
.Option(UpdateOption.Format, Format.ZIP)
32-
//开启驱动更新
33-
//.Option(UpdateOption.Drive, true)
34-
//开启遗言功能,需要部署GeneralUpdate.SystemService Windows服务。
35-
//.Option(UpdateOption.WillMessage, true)
36-
.Strategy<WindowsStrategy>()
37-
//注入一个func让用户决定是否跳过本次更新,如果是强制更新则不生效
38-
//.SetCustomSkipOption(ShowCustomOption)
39-
//注入一个自定义方法集合,该集合会在更新启动前执行。执行自定义方法列表如果出现任何异常,将通过异常订阅通知。(推荐在更新之前检查当前软件环境)
40-
//.AddCustomOption(new List<Func<bool>>() { () => Check1(), () => Check2() })
41-
//默认黑名单文件: { "Newtonsoft.Json.dll" } 默认黑名单文件扩展名: { ".patch", ".7z", ".zip", ".rar", ".tar" , ".json" }
42-
//如果不需要扩展,需要重新传入黑名单集合来覆盖。
43-
//.SetBlacklist(GetBlackFiles(), GetBlackFormats())
44-
.LaunchTaskAsync();
45-
46-
List<string> GetBlackFiles()
47-
{
48-
var blackFiles = new List<string>();
49-
blackFiles.Add("MainApp");
50-
return blackFiles;
51-
}
52-
53-
List<string> GetBlackFormats()
54-
{
55-
var blackFormats = new List<string>();
56-
blackFormats.Add(".zip");
57-
return blackFormats;
58-
}
59-
60-
/// <summary>
61-
/// 获取Windows平台所需的配置参数
62-
/// </summary>
63-
/// <returns></returns>
64-
Configinfo GetWindowsConfigInfo()
8+
try
659
{
66-
//该对象用于主程序客户端与更新组件进程之间交互用的对象
67-
var config = new Configinfo();
68-
//本机的客户端程序应用地址
69-
config.InstallPath = @"D:\packet\source";
70-
//更新公告网页
71-
config.UpdateLogUrl = "https://www.justerzhu.cn/";
72-
//客户端当前版本号
73-
config.ClientVersion = "1.1.1.1";
74-
//客户端类型:1.主程序客户端 2.更新组件
75-
config.AppType = AppType.UpgradeApp;
76-
//指定应用密钥,用于区分客户端应用
77-
config.AppSecretKey = "B8A7FADD-386C-46B0-B283-C9F963420C7C";
78-
//更新组件更新包下载地址
79-
config.UpdateUrl = $"{baseUrl}/versions/{config.AppType}/{config.ClientVersion}/{config.AppSecretKey}";
80-
//更新程序exe名称
81-
config.AppName = "GeneralUpdate.Core";
82-
//主程序客户端exe名称
83-
config.MainAppName = "GeneralUpdate.ClientCore";
84-
//主程序信息
85-
var mainVersion = "1.1.1.1";
86-
//主程序客户端更新包下载地址
87-
config.MainUpdateUrl = $"{baseUrl}/versions/{AppType.ClientApp}/{mainVersion}/{config.AppSecretKey}";
88-
return config;
10+
Console.WriteLine($"主程序初始化,{DateTime.Now}!");
11+
Console.WriteLine("当前运行目录:" + Thread.GetDomain().BaseDirectory);
12+
await Task.Delay(2000);
13+
var configinfo = new Configinfo
14+
{
15+
//configinfo.UpdateLogUrl = "https://www.baidu.com";
16+
ReportUrl = "http://127.0.0.1:5000/Report",
17+
UpdateUrl = "http://127.0.0.1:5000/Verification",
18+
AppName = "GeneralUpdate.Upgrad.exe",
19+
MainAppName = "GeneralUpdate.Client.exe",
20+
InstallPath = Thread.GetDomain().BaseDirectory,
21+
//configinfo.Bowl = "Generalupdate.CatBowl.exe";
22+
//当前客户端的版本号
23+
ClientVersion = "1.0.0.0",
24+
//当前升级端的版本号
25+
UpgradeClientVersion = "1.0.0.0",
26+
//产品id
27+
ProductId = "2d974e2a-31e6-4887-9bb1-b4689e98c77a",
28+
//应用密钥
29+
AppSecretKey = "dfeb5833-975e-4afb-88f1-6278ee9aeff6"
30+
};
31+
_ = await new GeneralClientBootstrap() //单个或多个更新包下载通知事件
32+
//单个或多个更新包下载速度、剩余下载事件、当前下载版本信息通知事件
33+
.AddListenerMultiDownloadStatistics(OnMultiDownloadStatistics)
34+
//单个或多个更新包下载完成
35+
.AddListenerMultiDownloadCompleted(OnMultiDownloadCompleted)
36+
//完成所有的下载任务通知
37+
.AddListenerMultiAllDownloadCompleted(OnMultiAllDownloadCompleted)
38+
//下载过程出现的异常通知
39+
.AddListenerMultiDownloadError(OnMultiDownloadError)
40+
//整个更新过程出现的任何问题都会通过这个事件通知
41+
.AddListenerException(OnException)
42+
.SetConfig(configinfo)
43+
.Option(UpdateOption.DownloadTimeOut, 60)
44+
.Option(UpdateOption.Encoding, Encoding.Default)
45+
.LaunchAsync();
46+
Console.WriteLine($"主程序已启动,{DateTime.Now}!");
8947
}
90-
91-
/// <summary>
92-
/// 获取Android平台所需要的参数
93-
/// </summary>
94-
/// <returns></returns>
95-
Configinfo GetAndroidConfigInfo()
96-
{
97-
var config = new Configinfo();
98-
config.InstallPath = System.Threading.Thread.GetDomain().BaseDirectory;
99-
//主程序客户端当前版本号
100-
config.ClientVersion = "1.0.0.0"; //VersionTracking.Default.CurrentVersion.ToString();
101-
config.AppType = AppType.ClientApp;
102-
config.AppSecretKey = "41A54379-C7D6-4920-8768-21A3468572E5";
103-
//主程序客户端exe名称
104-
config.MainAppName = "GeneralUpdate.ClientCore";
105-
//主程序信息
106-
var mainVersion = "1.1.1.1";
107-
config.MainUpdateUrl = $"{baseUrl}/versions/{AppType.ClientApp}/{mainVersion}/{config.AppSecretKey}";
108-
return config;
109-
}
110-
111-
/// <summary>
112-
/// 让用户决定是否跳过本次更新
113-
/// </summary>
114-
/// <returns></returns>
115-
async Task<bool> ShowCustomOption()
48+
catch (Exception e)
11649
{
117-
return await Task.FromResult(true);
50+
Console.WriteLine(e.Message + "\n" + e.StackTrace);
11851
}
11952

120-
void OnMultiDownloadStatistics(object sender, MultiDownloadStatisticsEventArgs e)
53+
void OnMultiDownloadError(object arg1, MultiDownloadErrorEventArgs arg2)
12154
{
122-
//e.Remaining 剩余下载时间
123-
//e.Speed 下载速度
124-
//e.Version 当前下载的版本信息
125-
Console.WriteLine($"{e.Version} download speed {e.Speed} bytes/s. Remaining {e.Remaining}");
55+
var version = arg2.Version as VersionInfo;
56+
Console.WriteLine($"{version.Version} {arg2.Exception}");
12657
}
12758

128-
void OnMultiDownloadProgressChanged(object sender, MultiDownloadProgressChangedEventArgs e)
59+
void OnMultiAllDownloadCompleted(object arg1, MultiAllDownloadCompletedEventArgs arg2)
12960
{
130-
//e.TotalBytesToReceive 当前更新包需要下载的总大小
131-
//e.ProgressValue 当前进度值
132-
//e.ProgressPercentage 当前进度的百分比
133-
//e.Version 当前下载的版本信息
134-
//e.Type 当前正在执行的操作 1.ProgressType.Check 检查版本信息中 2.ProgressType.Donwload 正在下载当前版本 3. ProgressType.Updatefile 更新当前版本 4. ProgressType.Done更新完成 5.ProgressType.Fail 更新失败
135-
//e.BytesReceived 已下载大小
136-
Console.WriteLine($"{e.ProgressPercentage}%");
61+
Console.WriteLine(arg2.IsAllDownloadCompleted ? "所有的下载任务已完成!" : $"下载任务已失败!{arg2.FailedVersions.Count}");
13762
}
13863

139-
void OnException(object sender, ExceptionEventArgs e)
64+
void OnMultiDownloadCompleted(object arg1, MultiDownloadCompletedEventArgs arg2)
14065
{
141-
Console.WriteLine(e.Exception.Message);
66+
var version = arg2.Version as VersionInfo;
67+
Console.WriteLine(arg2.IsComplated ? $"当前下载版本:{version.Version}, 下载完成!" : $"当前下载版本:{version.Version}, 下载失败!");
14268
}
14369

144-
void OnMultiAllDownloadCompleted(object sender, MultiAllDownloadCompletedEventArgs e)
70+
void OnMultiDownloadStatistics(object arg1, MultiDownloadStatisticsEventArgs arg2)
14571
{
146-
//e.FailedVersions; 如果出现下载失败则会把下载错误的版本、错误原因统计到该集合当中。
147-
Console.WriteLine($"Is all download completed {e.IsAllDownloadCompleted}.");
72+
var version = arg2.Version as VersionInfo;
73+
Console.WriteLine(
74+
$"当前下载版本:{version.Version},下载速度:{arg2.Speed},剩余下载时间:{arg2.Remaining},已下载大小:{arg2.BytesReceived},总大小:{arg2.TotalBytesToReceive}, 进度百分比:{arg2.ProgressPercentage}%");
14875
}
14976

150-
void OnMultiDownloadCompleted(object sender, MultiDownloadCompletedEventArgs e)
77+
void OnException(object arg1, ExceptionEventArgs arg2)
15178
{
152-
var info = e.Version as VersionInfo;
153-
Console.WriteLine($"{info.Name} download completed.");
154-
}
155-
156-
void OnMultiDownloadError(object sender, MultiDownloadErrorEventArgs e)
157-
{
158-
var info = e.Version as VersionInfo;
159-
Console.WriteLine($"{info.Name} error!");
160-
}
79+
Console.WriteLine($"{arg2.Exception}");
80+
}

src/FileService/hfs.exe

-2.07 MB
Binary file not shown.

src/Server/DTOs/HttpResponseDTO.cs

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
namespace ServerSample.DTOs;
2+
3+
public class HttpResponseDTO
4+
{
5+
/// <summary>
6+
/// 状态码
7+
/// </summary>
8+
public int Code { get; set; }
9+
10+
/// <summary>
11+
/// 响应消息内容
12+
/// </summary>
13+
public string Message { get; set; }
14+
15+
public static HttpResponseDTO Success(string message = "Success")
16+
{
17+
return new HttpResponseDTO
18+
{
19+
Code = 200,
20+
Message = message
21+
};
22+
}
23+
24+
public static HttpResponseDTO InnerException(string message)
25+
{
26+
return new HttpResponseDTO
27+
{
28+
Code = 500,
29+
Message = message
30+
};
31+
}
32+
}
33+
34+
public sealed class HttpResponseDTO<T> : HttpResponseDTO
35+
{
36+
/// <summary>
37+
/// 数据内容
38+
/// </summary>
39+
public T? Body { get; set; }
40+
41+
private HttpResponseDTO()
42+
{
43+
}
44+
45+
/// <summary>
46+
/// 成功
47+
/// </summary>
48+
/// <param name="data"></param>
49+
/// <param name="message"></param>
50+
/// <returns></returns>
51+
public static HttpResponseDTO<T> Success(T data, string message = "Success")
52+
{
53+
return new HttpResponseDTO<T>
54+
{
55+
Code = 200,
56+
Body = data,
57+
Message = message
58+
};
59+
}
60+
61+
/// <summary>
62+
/// 失败
63+
/// </summary>
64+
/// <param name="data"></param>
65+
/// <param name="message"></param>
66+
/// <returns></returns>
67+
public static HttpResponseDTO<T> Failure(string message, T? data = default)
68+
{
69+
return new HttpResponseDTO<T>
70+
{
71+
Code = 400,
72+
Body = data,
73+
Message = message
74+
};
75+
}
76+
77+
/// <summary>
78+
/// 服务器内部异常
79+
/// </summary>
80+
/// <param name="data"></param>
81+
/// <param name="message"></param>
82+
/// <returns></returns>
83+
public static HttpResponseDTO<T> InnerException(string message, T? data = default)
84+
{
85+
return new HttpResponseDTO<T>
86+
{
87+
Code = 500,
88+
Body = data,
89+
Message = message
90+
};
91+
}
92+
}

src/Server/DTOs/ReportDTO.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
namespace ServerSample.DTOs;
2+
3+
public record ReportDTO
4+
{
5+
/// <summary>
6+
/// 记录id
7+
/// </summary>
8+
public int RecordId { get; set; }
9+
10+
/// <summary>
11+
/// 更新状态
12+
/// </summary>
13+
public int Status { get; set; }
14+
15+
/// <summary>
16+
/// 1升级 2推送
17+
/// </summary>
18+
public int Type { get; set; }
19+
}

0 commit comments

Comments
 (0)