Skip to content

Commit e008690

Browse files
committed
添加generalupdate clinet示例
1 parent cc56441 commit e008690

File tree

2 files changed

+176
-1
lines changed

2 files changed

+176
-1
lines changed

src/Client/ClientSample.csproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,12 @@
77
<Nullable>enable</Nullable>
88
</PropertyGroup>
99

10+
<ItemGroup>
11+
<None Remove="build.bat" />
12+
</ItemGroup>
13+
14+
<ItemGroup>
15+
<PackageReference Include="GeneralUpdate.ClientCore" Version="2.12.10" />
16+
</ItemGroup>
17+
1018
</Project>

src/Client/Program.cs

Lines changed: 168 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,169 @@
11
// See https://aka.ms/new-console-template for more information
2-
Console.WriteLine("Hello, World!");
2+
3+
//ClientStrategy该更新策略将完成1.自动升级组件自更新 2.启动更新组件 3.配置好ClientParameter无需再像之前的版本写args数组进程通讯了。
4+
//generalClientBootstrap.Config(baseUrl, "B8A7FADD-386C-46B0-B283-C9F963420C7C").
5+
6+
using System.Text;
7+
using GeneralUpdate.ClientCore;
8+
using GeneralUpdate.Core.Bootstrap;
9+
using GeneralUpdate.Core.Domain.Entity;
10+
using GeneralUpdate.Core.Domain.Enum;
11+
using GeneralUpdate.Core.Events.CommonArgs;
12+
using GeneralUpdate.Core.Events.MultiEventArgs;
13+
using GeneralUpdate.Core.Strategys.PlatformWindows;
14+
15+
16+
var baseUrl = "127.0.0.1";
17+
18+
var configinfo = GetWindowsConfigInfo();
19+
var generalClientBootstrap = await new GeneralClientBootstrap()
20+
//单个或多个更新包下载通知事件
21+
.AddListenerMultiDownloadProgress(OnMultiDownloadProgressChanged)
22+
//单个或多个更新包下载速度、剩余下载事件、当前下载版本信息通知事件
23+
.AddListenerMultiDownloadStatistics(OnMultiDownloadStatistics)
24+
//单个或多个更新包下载完成
25+
.AddListenerMultiDownloadCompleted(OnMultiDownloadCompleted)
26+
//完成所有的下载任务通知
27+
.AddListenerMultiAllDownloadCompleted(OnMultiAllDownloadCompleted)
28+
//下载过程出现的异常通知
29+
.AddListenerMultiDownloadError(OnMultiDownloadError)
30+
//整个更新过程出现的任何问题都会通过这个事件通知
31+
.AddListenerException(OnException)
32+
.Config(configinfo)
33+
.Option(UpdateOption.DownloadTimeOut, 60)
34+
.Option(UpdateOption.Encoding, Encoding.Default)
35+
.Option(UpdateOption.Format, Format.ZIP)
36+
//开启驱动更新
37+
//.Option(UpdateOption.Drive, true)
38+
//开启遗言功能,需要部署GeneralUpdate.SystemService Windows服务。
39+
//.Option(UpdateOption.WillMessage, true)
40+
.Strategy<WindowsStrategy>()
41+
//注入一个func让用户决定是否跳过本次更新,如果是强制更新则不生效
42+
//.SetCustomSkipOption(ShowCustomOption)
43+
//注入一个自定义方法集合,该集合会在更新启动前执行。执行自定义方法列表如果出现任何异常,将通过异常订阅通知。(推荐在更新之前检查当前软件环境)
44+
//.AddCustomOption(new List<Func<bool>>() { () => Check1(), () => Check2() })
45+
//默认黑名单文件: { "Newtonsoft.Json.dll" } 默认黑名单文件扩展名: { ".patch", ".7z", ".zip", ".rar", ".tar" , ".json" }
46+
//如果不需要扩展,需要重新传入黑名单集合来覆盖。
47+
//.SetBlacklist(GetBlackFiles(), GetBlackFormats())
48+
.LaunchTaskAsync();
49+
50+
List<string> GetBlackFiles()
51+
{
52+
var blackFiles = new List<string>();
53+
blackFiles.Add("MainApp");
54+
return blackFiles;
55+
}
56+
57+
List<string> GetBlackFormats()
58+
{
59+
var blackFormats = new List<string>();
60+
blackFormats.Add(".zip");
61+
return blackFormats;
62+
}
63+
64+
/// <summary>
65+
/// 获取Windows平台所需的配置参数
66+
/// </summary>
67+
/// <returns></returns>
68+
Configinfo GetWindowsConfigInfo()
69+
{
70+
//该对象用于主程序客户端与更新组件进程之间交互用的对象
71+
var config = new Configinfo();
72+
//本机的客户端程序应用地址
73+
config.InstallPath = @"D:\packet\source";
74+
//更新公告网页
75+
config.UpdateLogUrl = "https://www.baidu.com/";
76+
//客户端当前版本号
77+
config.ClientVersion = "1.1.1.1";
78+
//客户端类型:1.主程序客户端 2.更新组件
79+
config.AppType = AppType.UpgradeApp;
80+
//指定应用密钥,用于区分客户端应用
81+
config.AppSecretKey = "B8A7FADD-386C-46B0-B283-C9F963420C7C";
82+
//更新组件更新包下载地址
83+
config.UpdateUrl = $"{baseUrl}/versions/{config.AppType}/{config.ClientVersion}/{config.AppSecretKey}";
84+
//更新程序exe名称
85+
config.AppName = "GeneralUpdate.Core";
86+
//主程序客户端exe名称
87+
config.MainAppName = "GeneralUpdate.ClientCore";
88+
//主程序信息
89+
var mainVersion = "1.1.1.1";
90+
//主程序客户端更新包下载地址
91+
config.MainUpdateUrl = $"{baseUrl}/versions/{AppType.ClientApp}/{mainVersion}/{config.AppSecretKey}";
92+
return config;
93+
}
94+
95+
/// <summary>
96+
/// 获取Android平台所需要的参数
97+
/// </summary>
98+
/// <returns></returns>
99+
Configinfo GetAndroidConfigInfo()
100+
{
101+
var config = new Configinfo();
102+
config.InstallPath = System.Threading.Thread.GetDomain().BaseDirectory;
103+
//主程序客户端当前版本号
104+
config.ClientVersion = "1.0.0.0"; //VersionTracking.Default.CurrentVersion.ToString();
105+
config.AppType = AppType.ClientApp;
106+
config.AppSecretKey = "41A54379-C7D6-4920-8768-21A3468572E5";
107+
//主程序客户端exe名称
108+
config.MainAppName = "GeneralUpdate.ClientCore";
109+
//主程序信息
110+
var mainVersion = "1.1.1.1";
111+
config.MainUpdateUrl = $"{baseUrl}/versions/{AppType.ClientApp}/{mainVersion}/{config.AppSecretKey}";
112+
return config;
113+
}
114+
115+
/// <summary>
116+
/// 让用户决定是否跳过本次更新
117+
/// </summary>
118+
/// <returns></returns>
119+
async Task<bool> ShowCustomOption()
120+
{
121+
return await Task.FromResult(true);
122+
}
123+
124+
void OnMultiDownloadStatistics(object sender, MultiDownloadStatisticsEventArgs e)
125+
{
126+
//e.Remaining 剩余下载时间
127+
//e.Speed 下载速度
128+
//e.Version 当前下载的版本信息
129+
}
130+
131+
void OnMultiDownloadProgressChanged(object sender, MultiDownloadProgressChangedEventArgs e)
132+
{
133+
//e.TotalBytesToReceive 当前更新包需要下载的总大小
134+
//e.ProgressValue 当前进度值
135+
//e.ProgressPercentage 当前进度的百分比
136+
//e.Version 当前下载的版本信息
137+
//e.Type 当前正在执行的操作 1.ProgressType.Check 检查版本信息中 2.ProgressType.Donwload 正在下载当前版本 3. ProgressType.Updatefile 更新当前版本 4. ProgressType.Done更新完成 5.ProgressType.Fail 更新失败
138+
//e.BytesReceived 已下载大小
139+
DispatchMessage($"{e.ProgressPercentage}%");
140+
//MyProgressBar.ProgressTo(e.ProgressValue, 100, Easing.Default);
141+
}
142+
143+
void OnException(object sender, ExceptionEventArgs e)
144+
{
145+
//DispatchMessage(e.Exception.Message);
146+
}
147+
148+
void OnMultiAllDownloadCompleted(object sender, MultiAllDownloadCompletedEventArgs e)
149+
{
150+
//e.FailedVersions; 如果出现下载失败则会把下载错误的版本、错误原因统计到该集合当中。
151+
DispatchMessage($"Is all download completed {e.IsAllDownloadCompleted}.");
152+
}
153+
154+
void OnMultiDownloadCompleted(object sender, MultiDownloadCompletedEventArgs e)
155+
{
156+
var info = e.Version as VersionInfo;
157+
DispatchMessage($"{info.Name} download completed.");
158+
}
159+
160+
void OnMultiDownloadError(object sender, MultiDownloadErrorEventArgs e)
161+
{
162+
var info = e.Version as VersionInfo;
163+
DispatchMessage($"{info.Name} error!");
164+
}
165+
166+
void DispatchMessage(string message)
167+
{
168+
//ShowMessage(message);
169+
}

0 commit comments

Comments
 (0)