This commit is contained in:
CortexCore
2023-09-01 14:35:05 +08:00
parent a71288cf2d
commit 5561f5c3cc
136 changed files with 69284 additions and 66121 deletions

View File

@@ -0,0 +1,19 @@
{
"name": "BITKit.Http",
"rootNamespace": "",
"references": [
"GUID:14fe60d984bf9f84eac55c6ea033a8f4",
"GUID:f51ebe6a0ceec4240a699833d6309b23",
"GUID:d525ad6bd40672747bde77962f1c401e",
"GUID:49b49c76ee64f6b41bf28ef951cb0e50"
],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}

View File

@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 1d8f5f00ed7aff940991db22d9d3d5e1
AssemblyDefinitionImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,63 @@
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;
[Header(Constant.Header.Output)]
[SerializeField] private UnityEvent<string> output;
[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();
timer.Start();
}
private void OnDestroy()
{
timer.Stop();
timer.Dispose();
}
private async void OnUpdate()
{
Stopwatch stopwatch = new();
stopwatch.Start();
await _httpClient.GetStringAsync(url.Value);
_cancellationToken.ThrowIfCancellationRequested();
#if UNITY_EDITOR
if (UnityEditor.EditorApplication.isPlaying is false) return;
#endif
stopwatch.Stop();
time =System.TimeSpan.FromMilliseconds(stopwatch.ElapsedMilliseconds).TotalSeconds;
output.Invoke(url.Value);
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 81cd8c05ac3e3824b99d88ed163ababe
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: