NLDClient-yudde/ProjectNLD/Assets/Code/Scripts/HotUpdate/DownloadTest.cs

232 lines
7.8 KiB
C#
Raw Normal View History

2024-12-13 15:15:19 +08:00
using Cysharp.Threading.Tasks;
using System;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;
using System.Threading;
using UnityEngine;
using UnityEngine.Networking;
/// <summary>
/// 下载测试
/// </summary>
public class DownloadTest
{
/// <summary>
/// 下载URL
/// </summary>
2024-12-24 17:12:38 +08:00
private const string DOWNLOAD_URL = "http://1.14.62.163/Build/TapTapDev/Android/0.1.0/OutputCache.manifest";
2024-12-13 15:15:19 +08:00
/// <summary>
/// 超时时间
/// </summary>
private readonly double timeOutTick = 10f; // 10秒没下完超时
/// <summary>
/// 当前下载间隔
/// </summary>
private float intervals = 15f;
/// <summary>
/// 计时
/// </summary>
private float timer = 0f;
/// <summary>
/// 总下载次数
/// </summary>
private int total;
private StreamWriter writerUnityWebRequest;
private StreamWriter writerHttpClient;
/// <summary>
/// 最大下载次数
/// </summary>
public int MaxDownloadCount { get; private set; }
/// <summary>
/// 是否开始下载
/// </summary>
public bool IsBeginDownload { get; private set; }
public DownloadTest()
{
IsBeginDownload = false;
}
/// <summary>
/// 开始下载测试
/// </summary>
public async void BeginDownload()
{
if (IsBeginDownload)
return;
IsBeginDownload = true;
string fileUnityWebRequestPath = Path.Combine(Application.persistentDataPath, "UnityWebRequestTest.txt");
string fileHttpClientPath = Path.Combine(Application.persistentDataPath, "HttpClientTest.txt");
Debug.Log($"开始下载测试,下载{MaxDownloadCount}次,保存文件路径:{fileUnityWebRequestPath}和{fileHttpClientPath}");
timer = intervals;
total = 0;
MaxDownloadCount = 5;
writerUnityWebRequest = File.CreateText(fileUnityWebRequestPath);
writerHttpClient = File.CreateText(fileHttpClientPath);
while (IsBeginDownload)
{
if (timer >= intervals)
{
timer -= intervals;
++total;
if (total > MaxDownloadCount)
{
StopDownload();
return;
}
2024-12-24 14:50:55 +08:00
string networdType = Application.internetReachability switch
{
NetworkReachability.ReachableViaCarrierDataNetwork => "流量",
NetworkReachability.ReachableViaLocalAreaNetwork => "Wifi",
_ => "没有网络"
};
DownloadByUnityWebRequest(networdType);
2024-12-24 17:12:38 +08:00
DownloadByHttpClient(networdType);
2024-12-13 15:15:19 +08:00
}
await UniTask.DelayFrame(1);
timer += Time.deltaTime;
}
}
/// <summary>
/// 停止下载
/// </summary>
public void StopDownload()
{
writerUnityWebRequest.Flush();
writerUnityWebRequest.Close();
writerHttpClient.Flush();
writerHttpClient.Close();
IsBeginDownload = false;
}
/// <summary>
/// 通过WebRequest下载
/// </summary>
2024-12-24 14:50:55 +08:00
private async void DownloadByUnityWebRequest(string netword)
2024-12-13 15:15:19 +08:00
{
string log = string.Empty;
2024-12-13 16:27:20 +08:00
DateTime startTime = DateTime.Now;
2024-12-13 15:15:19 +08:00
DateTime endTime;
using UnityWebRequest request = UnityWebRequest.Get(DOWNLOAD_URL);
UnityWebRequestAsyncOperation asyncOperation = request.SendWebRequest();
while (!asyncOperation.isDone)
{
await UniTask.DelayFrame(1);
2024-12-13 16:27:20 +08:00
endTime = DateTime.Now;
2024-12-13 15:15:19 +08:00
TimeSpan timeSpan = endTime - startTime;
if (timeSpan.TotalSeconds > timeOutTick)
{
request.Abort();
2024-12-24 14:50:55 +08:00
log = $"第{total}次下载,类型:{nameof(UnityWebRequest)}, 网络:{netword}, {timeOutTick}s超时URL:{DOWNLOAD_URL}";
2024-12-13 17:09:08 +08:00
Debug.LogWarning(log);
2024-12-13 15:15:19 +08:00
writerUnityWebRequest.WriteLine(log);
writerUnityWebRequest.Flush();
2024-12-24 14:50:55 +08:00
SendWechatMessage(log);
2024-12-13 15:15:19 +08:00
return;
}
}
2024-12-13 16:27:20 +08:00
endTime = DateTime.Now;
2024-12-13 15:15:19 +08:00
if (request.result != UnityWebRequest.Result.Success)
{
2024-12-24 14:50:55 +08:00
log = $"第{total}次下载,类型:{nameof(UnityWebRequest)}, 网络:{netword}, 开始时间:{startTime},完成时间:{endTime},间隔:{(endTime - startTime).TotalSeconds}秒,是否成功:{false}URL:{DOWNLOAD_URL}";
2024-12-13 15:15:19 +08:00
Debug.LogError(log);
writerUnityWebRequest.WriteLine(log);
writerUnityWebRequest.Flush();
2024-12-24 14:50:55 +08:00
SendWechatMessage(log);
2024-12-13 15:15:19 +08:00
return;
}
2024-12-24 14:50:55 +08:00
log = $"第{total}次下载,类型:{nameof(UnityWebRequest)}, 网络:{netword}, 开始时间:{startTime},完成时间:{endTime},间隔:{(endTime - startTime).TotalSeconds}秒,是否成功:{true}URL:{DOWNLOAD_URL}";
2024-12-13 15:15:19 +08:00
Debug.Log(log);
writerUnityWebRequest.WriteLine(log);
writerUnityWebRequest.Flush();
}
/// <summary>
/// 通过HttpClient下载
/// </summary>
2024-12-24 17:12:38 +08:00
private async void DownloadByHttpClient(string netword)
2024-12-13 15:15:19 +08:00
{
string log = string.Empty;
2024-12-13 16:27:20 +08:00
DateTime startTime = DateTime.Now;
2024-12-13 15:15:19 +08:00
DateTime endTime;
CancellationTokenSource cancellationTokenSource = new();
using HttpClient client = new();
Task<HttpResponseMessage> response = client.GetAsync(DOWNLOAD_URL, cancellationTokenSource.Token);
while (!response.IsCompleted)
{
await UniTask.DelayFrame(1);
2024-12-13 16:27:20 +08:00
endTime = DateTime.Now;
2024-12-13 15:15:19 +08:00
TimeSpan timeSpan = endTime - startTime;
if (timeSpan.TotalSeconds > timeOutTick)
{
cancellationTokenSource.Cancel();
2024-12-24 14:50:55 +08:00
log = $"第{total}次下载,类型:{nameof(HttpClient)}, 网络:{netword}, {timeOutTick}s超时URL:{DOWNLOAD_URL}";
2024-12-13 17:09:08 +08:00
Debug.LogWarning(log);
2024-12-13 15:15:19 +08:00
writerHttpClient.WriteLine(log);
writerHttpClient.Flush();
2024-12-24 14:50:55 +08:00
SendWechatMessage(log);
2024-12-13 15:15:19 +08:00
return;
}
}
2024-12-13 16:27:20 +08:00
endTime = DateTime.Now;
2024-12-24 17:12:38 +08:00
if (!response.IsCompletedSuccessfully || !response.Result.IsSuccessStatusCode)
2024-12-13 15:15:19 +08:00
{
2024-12-24 14:50:55 +08:00
log = $"第{total}次下载,类型:{nameof(HttpClient)}, 网络:{netword}, 开始时间:{startTime},完成时间:{endTime},间隔:{(endTime - startTime).TotalSeconds}秒,是否成功:{false}URL:{DOWNLOAD_URL}";
2024-12-13 17:09:08 +08:00
Debug.LogError(log);
2024-12-13 15:15:19 +08:00
writerHttpClient.WriteLine(log);
writerHttpClient.Flush();
2024-12-24 14:50:55 +08:00
SendWechatMessage(log);
2024-12-13 15:15:19 +08:00
return;
}
2024-12-24 14:50:55 +08:00
log = $"第{total}次下载,类型:{nameof(HttpClient)}, 网络:{netword}, 开始时间:{startTime},完成时间:{endTime},间隔:{(endTime - startTime).TotalSeconds}秒,是否成功:{true}URL:{DOWNLOAD_URL}";
2024-12-13 15:15:19 +08:00
Debug.Log(log);
writerHttpClient.WriteLine(log);
writerHttpClient.Flush();
}
2024-12-24 14:50:55 +08:00
/// <summary>
/// 发送企业微信通知
/// </summary>
public async void SendWechatMessage(string content)
{
string url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=bf89f555-f710-48c4-87d0-7a8182dec13e";
string message = "{\r\n" +
"\"msgtype\":\"text\",\r\n" +
"\"text\":{\r\n" +
$"\"content\":\"{content}\",\r\n" +
$"\"mentioned_list\":[\"@all\"]" +
"}\r\n" + "}";
UnityWebRequest request = UnityWebRequest.Post(url, message, "application/json");
await request.SendWebRequest();
if (request.result != UnityWebRequest.Result.Success)
Debug.LogError($"Error: {request.error}");
else
Debug.Log($"Response: {request.downloadHandler.text}"); // 请求成功,输出响应数据
request.Dispose();
}
2024-12-13 15:15:19 +08:00
}