BITFALL/Assets/BITKit/Unity/Scripts/Http/HttpService.cs

108 lines
2.7 KiB
C#
Raw Normal View History

2023-08-27 02:58:19 +08:00
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
2024-05-01 16:51:50 +08:00
using System.Linq;
2023-08-27 02:58:19 +08:00
using System.Net.Http;
using System.Threading;
using Cysharp.Threading.Tasks;
using UnityEditor.Rendering;
using UnityEngine;
using UnityEngine.Events;
2024-05-01 16:51:50 +08:00
using Debug = UnityEngine.Debug;
2023-08-27 02:58:19 +08:00
using Timer = System.Timers.Timer;
namespace BITKit.Http
{
public class HttpService : MonoBehaviour
{
[Header(Constant.Header.Settings)]
[SerializeReference,SubclassSelector] private IReference url;
[SerializeField] private double interval;
2023-10-02 23:24:56 +08:00
[SerializeField] private CollisionDetectionMode detectionMode;
2023-08-27 02:58:19 +08:00
[Header(Constant.Header.Output)]
[SerializeField] private UnityEvent<string> output;
2023-10-02 23:24:56 +08:00
[SerializeField] private UnityEvent<string> onException;
2024-05-01 16:51:50 +08:00
[SerializeField] private UnityEvent<int> onCalculateLength;
2023-08-27 02:58:19 +08:00
[Header(Constant.Header.Debug)]
[SerializeField, ReadOnly] private double time;
private readonly Timer timer = new()
{
AutoReset = true
};
private readonly HttpClient _httpClient = new();
2024-05-01 16:51:50 +08:00
private bool _enabled;
2023-08-27 02:58:19 +08:00
2024-05-01 16:51:50 +08:00
private void OnEnable()
2023-08-27 02:58:19 +08:00
{
2024-05-01 16:51:50 +08:00
_enabled = true;
}
private void OnDisable()
{
_enabled = false;
2023-08-27 02:58:19 +08:00
}
private void Start()
{
timer.Interval = interval*1000;
timer.Elapsed += (x, y) => OnUpdate();
2023-10-02 23:24:56 +08:00
timer.AutoReset = detectionMode switch
{
CollisionDetectionMode.ContinuousDynamic => true,
CollisionDetectionMode.ContinuousSpeculative => true,
CollisionDetectionMode.Continuous => true,
CollisionDetectionMode.Discrete => false,
_ => throw new ArgumentOutOfRangeException()
};
2023-08-27 02:58:19 +08:00
timer.Start();
2024-05-01 16:51:50 +08:00
destroyCancellationToken.ThrowIfCancellationRequested();
2023-08-27 02:58:19 +08:00
}
private void OnDestroy()
{
timer.Stop();
timer.Dispose();
}
private async void OnUpdate()
{
2024-05-01 16:51:50 +08:00
try
2023-10-02 23:24:56 +08:00
{
2024-05-01 16:51:50 +08:00
if (_enabled)
{
Stopwatch stopwatch = new();
stopwatch.Start();
//var result = await _httpClient.GetStringAsync(url.Value);
var response = await _httpClient.GetAsync(url.Value);
var result =await response.Content.ReadAsStringAsync();
if (destroyCancellationToken.IsCancellationRequested) return;
stopwatch.Stop();
time =System.TimeSpan.FromMilliseconds(stopwatch.ElapsedMilliseconds).TotalSeconds;
await UniTask.SwitchToMainThread();
if (destroyCancellationToken.IsCancellationRequested) return;
var length = int.Parse(response.Content.Headers.First(h => h.Key.Equals("Content-Length")).Value.First());
onCalculateLength.Invoke(length);
if (response.StatusCode is not System.Net.HttpStatusCode.OK)
{
onException.Invoke(result);
}
else
{
output.Invoke(result);
}
}
2023-10-02 23:24:56 +08:00
}
2024-05-01 16:51:50 +08:00
catch (Exception e)
2023-10-02 23:24:56 +08:00
{
2024-05-01 16:51:50 +08:00
BIT4Log.LogException(e);
2023-10-02 23:24:56 +08:00
}
2024-05-01 16:51:50 +08:00
2023-10-02 23:24:56 +08:00
if(timer.AutoReset==false)
timer.Start();
2023-08-27 02:58:19 +08:00
}
}
}