2023-08-27 02:58:19 +08:00
|
|
|
using System;
|
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Diagnostics;
|
|
|
|
using System.Net.Http;
|
|
|
|
using System.Threading;
|
|
|
|
using Cysharp.Threading.Tasks;
|
|
|
|
using UnityEditor.Rendering;
|
|
|
|
using UnityEngine;
|
|
|
|
using UnityEngine.Events;
|
|
|
|
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;
|
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();
|
|
|
|
private CancellationToken _cancellationToken;
|
|
|
|
|
|
|
|
private void Awake()
|
|
|
|
{
|
|
|
|
_cancellationToken = gameObject.GetCancellationTokenOnDestroy();
|
|
|
|
}
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
private void OnDestroy()
|
|
|
|
{
|
|
|
|
timer.Stop();
|
|
|
|
timer.Dispose();
|
|
|
|
}
|
|
|
|
private async void OnUpdate()
|
|
|
|
{
|
|
|
|
Stopwatch stopwatch = new();
|
|
|
|
stopwatch.Start();
|
2023-10-02 23:24:56 +08:00
|
|
|
//var result = await _httpClient.GetStringAsync(url.Value);
|
|
|
|
var response = await _httpClient.GetAsync(url.Value, _cancellationToken);
|
|
|
|
var result =await response.Content.ReadAsStringAsync();
|
2023-08-27 02:58:19 +08:00
|
|
|
_cancellationToken.ThrowIfCancellationRequested();
|
2023-10-02 23:24:56 +08:00
|
|
|
|
2023-08-27 02:58:19 +08:00
|
|
|
stopwatch.Stop();
|
|
|
|
time =System.TimeSpan.FromMilliseconds(stopwatch.ElapsedMilliseconds).TotalSeconds;
|
2023-10-02 23:24:56 +08:00
|
|
|
await UniTask.SwitchToMainThread(_cancellationToken);
|
|
|
|
if (response.StatusCode is not System.Net.HttpStatusCode.OK)
|
|
|
|
{
|
|
|
|
onException.Invoke(result);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
output.Invoke(result);
|
|
|
|
}
|
|
|
|
if(timer.AutoReset==false)
|
|
|
|
timer.Start();
|
2023-08-27 02:58:19 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|