更新00
This commit is contained in:
@@ -1,24 +0,0 @@
|
||||
using YooAsset;
|
||||
|
||||
namespace Stary.Evo
|
||||
{
|
||||
public class RemoteServices : IRemoteServices
|
||||
{
|
||||
private readonly string _defaultHostServer;
|
||||
private readonly string _fallbackHostServer;
|
||||
|
||||
public RemoteServices(string defaultHostServer, string fallbackHostServer)
|
||||
{
|
||||
_defaultHostServer = defaultHostServer;
|
||||
_fallbackHostServer = fallbackHostServer;
|
||||
}
|
||||
string IRemoteServices.GetRemoteMainURL(string fileName)
|
||||
{
|
||||
return $"{_defaultHostServer}/{fileName}";
|
||||
}
|
||||
string IRemoteServices.GetRemoteFallbackURL(string fileName)
|
||||
{
|
||||
return $"{_fallbackHostServer}/{fileName}";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,3 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 24f14ead8b7e46eaa0eddd6e68db8d56
|
||||
timeCreated: 1741248220
|
||||
@@ -1,51 +0,0 @@
|
||||
using YooAsset;
|
||||
using System.Collections;
|
||||
using Stary.Evo;
|
||||
using UnityEngine.Networking;
|
||||
|
||||
public class RemoteServicesWithAuth : IRemoteServices
|
||||
{
|
||||
private readonly string _defaultHostServer;
|
||||
private readonly string _fallbackHostServer;
|
||||
private string _authToken;
|
||||
private string _authServiceUrl;
|
||||
|
||||
public string CurrentToken { get; private set; }
|
||||
|
||||
public RemoteServicesWithAuth(string defaultHostServer,
|
||||
string fallbackHostServer,
|
||||
string authToken,
|
||||
string authServiceUrl)
|
||||
{
|
||||
_defaultHostServer = defaultHostServer;
|
||||
_fallbackHostServer = fallbackHostServer;
|
||||
_authToken = authToken;
|
||||
_authServiceUrl = authServiceUrl;
|
||||
RefreshToken();
|
||||
}
|
||||
|
||||
private void RefreshToken()
|
||||
{
|
||||
// 这里实现具体的token刷新逻辑
|
||||
CurrentToken = _authToken; // 简单示例直接使用初始token
|
||||
// 实际项目应该通过authServiceUrl获取新token
|
||||
}
|
||||
|
||||
string IRemoteServices.GetRemoteMainURL(string fileName)
|
||||
{
|
||||
// 在原始URL后附加鉴权参数
|
||||
return $"{_defaultHostServer}/{fileName}?token={CurrentToken}";
|
||||
}
|
||||
|
||||
public string GetRemoteFallbackURL(string fileName)
|
||||
{
|
||||
return $"{_fallbackHostServer}/{fileName}?token={CurrentToken}";
|
||||
}
|
||||
|
||||
// UnityWebRequest CreateRequest(string url)
|
||||
// {
|
||||
// var request = base.CreateRequest(url);
|
||||
// request.SetRequestHeader("Authorization", $"Bearer {CurrentToken}");
|
||||
// return request;
|
||||
// }
|
||||
}
|
||||
@@ -1,3 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6dc545bfe9be45789ff05152e3679e20
|
||||
timeCreated: 1741319247
|
||||
67
Assets/00.StaryEvo/Runtime/Utility/UnityAsyncExtensions.cs
Normal file
67
Assets/00.StaryEvo/Runtime/Utility/UnityAsyncExtensions.cs
Normal file
@@ -0,0 +1,67 @@
|
||||
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
|
||||
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
#if ENABLE_UNITYWEBREQUEST && (!UNITY_2019_1_OR_NEWER || UNITASK_WEBREQUEST_SUPPORT)
|
||||
using UnityEngine.Networking;
|
||||
#endif
|
||||
|
||||
namespace Stary.Evo
|
||||
{
|
||||
public static partial class UnityAsyncExtensions
|
||||
{
|
||||
|
||||
|
||||
#if !UNITY_2023_1_OR_NEWER
|
||||
// from Unity2023.1.0a15, AsyncOperationAwaitableExtensions.GetAwaiter is defined in UnityEngine.
|
||||
public static AsyncOperationAwaiter GetAwaiter(this AsyncOperation asyncOperation)
|
||||
{
|
||||
return new AsyncOperationAwaiter(asyncOperation);
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
public struct AsyncOperationAwaiter : ICriticalNotifyCompletion
|
||||
{
|
||||
AsyncOperation asyncOperation;
|
||||
Action<AsyncOperation> continuationAction;
|
||||
|
||||
public AsyncOperationAwaiter(AsyncOperation asyncOperation)
|
||||
{
|
||||
this.asyncOperation = asyncOperation;
|
||||
this.continuationAction = null;
|
||||
}
|
||||
|
||||
public bool IsCompleted => asyncOperation.isDone;
|
||||
|
||||
public void GetResult()
|
||||
{
|
||||
if (continuationAction != null)
|
||||
{
|
||||
asyncOperation.completed -= continuationAction;
|
||||
continuationAction = null;
|
||||
asyncOperation = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
asyncOperation = null;
|
||||
}
|
||||
}
|
||||
|
||||
public void OnCompleted(Action continuation)
|
||||
{
|
||||
UnsafeOnCompleted(continuation);
|
||||
}
|
||||
|
||||
public void UnsafeOnCompleted(Action continuation)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a42a4edb452e470b87e698b7d7b7db4a
|
||||
timeCreated: 1743403227
|
||||
@@ -1,6 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using System.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Networking;
|
||||
|
||||
@@ -8,8 +8,8 @@ namespace Stary.Evo
|
||||
{
|
||||
public interface IWebRequestSystem
|
||||
{
|
||||
UniTask<string> Post(string url, string postData);
|
||||
UniTask<string> Get(string url, string token=null);
|
||||
Task<string> Post(string url, string postData);
|
||||
Task<string> Get(string url, string token=null);
|
||||
|
||||
}
|
||||
|
||||
@@ -21,7 +21,7 @@ namespace Stary.Evo
|
||||
/// <param name="url">获取Token值的服务URL地址(很重要)</param>
|
||||
/// <param name="postData">传入请求的参数,此处参数为JOSN格式</param>
|
||||
/// <returns></returns>
|
||||
public async UniTask<string> Post(string url, string postData)
|
||||
public async Task<string> Post(string url, string postData)
|
||||
{
|
||||
using (UnityWebRequest webRequest = UnityWebRequest.Post(url, postData)) //第二种写法此行注释
|
||||
{
|
||||
@@ -51,7 +51,7 @@ namespace Stary.Evo
|
||||
/// <param name="url">请求数据的URL地址</param>
|
||||
/// <param name="token">token验证的参数,此处为authorization</param>
|
||||
/// <returns></returns>
|
||||
public async UniTask<string> Get(string url, string token=null)
|
||||
public async Task<string> Get(string url, string token=null)
|
||||
{
|
||||
try
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user