1
This commit is contained in:
@@ -2,12 +2,14 @@ using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Threading;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using UnityEditor.Rendering;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Events;
|
||||
using Debug = UnityEngine.Debug;
|
||||
using Timer = System.Timers.Timer;
|
||||
|
||||
namespace BITKit.Http
|
||||
@@ -22,20 +24,33 @@ namespace BITKit.Http
|
||||
[Header(Constant.Header.Output)]
|
||||
[SerializeField] private UnityEvent<string> output;
|
||||
[SerializeField] private UnityEvent<string> onException;
|
||||
[SerializeField] private UnityEvent<int> onCalculateLength;
|
||||
|
||||
[Header(Constant.Header.Optional)]
|
||||
[SerializeField] private Optional<string> overrideResponse;
|
||||
|
||||
|
||||
[Header(Constant.Header.Debug)]
|
||||
[SerializeField, ReadOnly] private double time;
|
||||
|
||||
|
||||
|
||||
[SerializeField, ReadOnly] private bool isConnected;
|
||||
|
||||
private readonly Timer timer = new()
|
||||
{
|
||||
AutoReset = true
|
||||
};
|
||||
private readonly HttpClient _httpClient = new();
|
||||
private CancellationToken _cancellationToken;
|
||||
private bool _enabled;
|
||||
|
||||
private void Awake()
|
||||
private void OnEnable()
|
||||
{
|
||||
_cancellationToken = gameObject.GetCancellationTokenOnDestroy();
|
||||
_enabled = true;
|
||||
}
|
||||
private void OnDisable()
|
||||
{
|
||||
_enabled = false;
|
||||
}
|
||||
private void Start()
|
||||
{
|
||||
@@ -50,6 +65,7 @@ AutoReset = true
|
||||
_ => throw new ArgumentOutOfRangeException()
|
||||
};
|
||||
timer.Start();
|
||||
destroyCancellationToken.ThrowIfCancellationRequested();
|
||||
}
|
||||
private void OnDestroy()
|
||||
{
|
||||
@@ -58,26 +74,83 @@ AutoReset = true
|
||||
}
|
||||
private async void OnUpdate()
|
||||
{
|
||||
Stopwatch stopwatch = new();
|
||||
stopwatch.Start();
|
||||
//var result = await _httpClient.GetStringAsync(url.Value);
|
||||
var response = await _httpClient.GetAsync(url.Value, _cancellationToken);
|
||||
var result =await response.Content.ReadAsStringAsync();
|
||||
_cancellationToken.ThrowIfCancellationRequested();
|
||||
try
|
||||
{
|
||||
if (_enabled)
|
||||
{
|
||||
|
||||
stopwatch.Stop();
|
||||
time =System.TimeSpan.FromMilliseconds(stopwatch.ElapsedMilliseconds).TotalSeconds;
|
||||
await UniTask.SwitchToMainThread(_cancellationToken);
|
||||
if (response.StatusCode is not System.Net.HttpStatusCode.OK)
|
||||
{
|
||||
onException.Invoke(result);
|
||||
if (overrideResponse.Allow)
|
||||
{
|
||||
await UniTask.SwitchToMainThread();
|
||||
try
|
||||
{
|
||||
output.Invoke(overrideResponse.Value);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Debug.LogException(e);
|
||||
}
|
||||
|
||||
isConnected = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
Stopwatch stopwatch = new();
|
||||
stopwatch.Start();
|
||||
//var result = await _httpClient.GetStringAsync(url.Value);
|
||||
var response = await _httpClient.GetAsync(url.Value,destroyCancellationToken);
|
||||
if (destroyCancellationToken.IsCancellationRequested) return;
|
||||
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;
|
||||
|
||||
try
|
||||
{
|
||||
var length = int.Parse(response.Content.Headers.First(h => h.Key.Equals("Content-Length")).Value.First());
|
||||
onCalculateLength.Invoke(length);
|
||||
}
|
||||
catch (InvalidOperationException)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
if (response.StatusCode is not System.Net.HttpStatusCode.OK)
|
||||
{
|
||||
onException.Invoke(result);
|
||||
}
|
||||
else
|
||||
{
|
||||
try
|
||||
{
|
||||
output.Invoke(result);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Debug.LogException(e);
|
||||
}
|
||||
}
|
||||
|
||||
isConnected = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
catch(OperationCanceledException){}
|
||||
catch (Exception e)
|
||||
{
|
||||
output.Invoke(result);
|
||||
BIT4Log.LogException(e);
|
||||
isConnected = false;
|
||||
}
|
||||
if(timer.AutoReset==false)
|
||||
|
||||
if (timer.AutoReset == false)
|
||||
{
|
||||
if(destroyCancellationToken.IsCancellationRequested)return;
|
||||
timer.Start();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user