1
This commit is contained in:
38
Core/Net/HttpClient.cs
Normal file
38
Core/Net/HttpClient.cs
Normal file
@@ -0,0 +1,38 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using System.Threading;
|
||||
using System.Net.Http;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
using Newtonsoft.Json;
|
||||
namespace BITKit.HttpNet
|
||||
{
|
||||
[System.Serializable]
|
||||
public sealed class HttpClient : WebProvider
|
||||
{
|
||||
System.Net.Http.HttpClient client = new();
|
||||
public override async UniTask<string> GetAsync(string url, CancellationToken cancellationToken = default)
|
||||
{
|
||||
using (var response = await client.GetAsync(url))
|
||||
{
|
||||
return await response.Content.ReadAsStringAsync();
|
||||
}
|
||||
}
|
||||
|
||||
public override async UniTask<string> PostAsync<T>(string url, T value, CancellationToken cancellationToken = default)
|
||||
{
|
||||
HttpContent content = null;
|
||||
switch (value)
|
||||
{
|
||||
case string _string:
|
||||
content = new StringContent(_string);
|
||||
break;
|
||||
default:
|
||||
//content = new ByteArrayContent(.WriteAsBytes(value));
|
||||
break;
|
||||
}
|
||||
using var response = await client.PostAsync(url, content, cancellationToken);
|
||||
return await response.Content.ReadAsStringAsync();
|
||||
}
|
||||
}
|
||||
}
|
0
Core/Net/INetMessager.cs
Normal file
0
Core/Net/INetMessager.cs
Normal file
17
Core/Net/IWebProvider.cs
Normal file
17
Core/Net/IWebProvider.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using System.Threading;
|
||||
namespace BITKit.HttpNet
|
||||
{
|
||||
public interface IWebProvider
|
||||
{
|
||||
UniTask<string> GetAsync(string url, CancellationToken cancellationToken = default);
|
||||
async UniTask<T> GetAsync<T>(string url, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var json = await GetAsync(url, cancellationToken);
|
||||
return Newtonsoft.Json.JsonConvert.DeserializeObject<T>(json);
|
||||
}
|
||||
UniTask<string> PostAsync<T>(string url, T value, CancellationToken cancellationToken = default);
|
||||
}
|
||||
}
|
36
Core/Net/NetMessage.cs
Normal file
36
Core/Net/NetMessage.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
namespace BITKit
|
||||
{
|
||||
public interface INetMessage { }
|
||||
public interface INetMessageReader
|
||||
{
|
||||
void Initialize();
|
||||
Type GetMessageType();
|
||||
object ReadBinaryAsObject(BinaryReader reader);
|
||||
void WriteBinaryAsObject(BinaryWriter writer, Object value);
|
||||
}
|
||||
public interface INetMessageReader<T> : INetMessageReader
|
||||
{
|
||||
T ReadBinary(BinaryReader reader);
|
||||
void WriteBinary(BinaryWriter writer, T value);
|
||||
}
|
||||
public abstract class NetMessageReader<T> : INetMessageReader<T>
|
||||
{
|
||||
public Type GetMessageType() => typeof(T);
|
||||
public virtual void Initialize() { }
|
||||
public abstract T ReadBinary(BinaryReader reader);
|
||||
public object ReadBinaryAsObject(BinaryReader reader) => ReadBinary(reader);
|
||||
public abstract void WriteBinary(BinaryWriter writer, T value);
|
||||
public void WriteBinaryAsObject(BinaryWriter writer, object value) => WriteBinary(writer, (T)value);
|
||||
}
|
||||
public interface INetMessager : IDiagnostics
|
||||
{
|
||||
void Init();
|
||||
void Send<T>(T message);
|
||||
void Register<T>(Action<T> action);
|
||||
void UnRegister<T>(Action<T> action);
|
||||
}
|
||||
}
|
32
Core/Net/NetProvider.cs
Normal file
32
Core/Net/NetProvider.cs
Normal file
@@ -0,0 +1,32 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
#if UNITY
|
||||
using UnityEngine;
|
||||
#endif
|
||||
|
||||
namespace BITKit
|
||||
{
|
||||
public interface INetProvider
|
||||
{
|
||||
void ServerRpc(Action<string> action, params object[] pars);
|
||||
void ServerRpc(Action<object> action, params object[] pars);
|
||||
void AddRpcHandle(object rpcHandle);
|
||||
void SendRT(string rpcName, params object[] pars);
|
||||
void SendAllRT(string rpcName, params object[] pars);
|
||||
#if UNITY
|
||||
void ServerRpc(Action<UnityEngine.Object> action, params object[] pars);
|
||||
void ClientRpc(Action<UnityEngine.Object> action, params object[] pars);
|
||||
void ServerRpc(Action<UnityEngine.GameObject> action, params object[] pars);
|
||||
void ClientRpc(Action<UnityEngine.GameObject> action, params object[] pars);
|
||||
#endif
|
||||
|
||||
////void ServerRpc<T>(Action<T> action,params object[] pars);
|
||||
}
|
||||
public interface INetServer
|
||||
{
|
||||
void StartServer(ushort port = 27014);
|
||||
void StopServer();
|
||||
bool IsRunningServer { get; }
|
||||
}
|
||||
}
|
29
Core/Net/NetServiceSample.cs
Normal file
29
Core/Net/NetServiceSample.cs
Normal file
@@ -0,0 +1,29 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
#if UNITY
|
||||
using UnityEngine;
|
||||
namespace BITKit
|
||||
{
|
||||
public class NetServiceSample:MonoBehaviour
|
||||
{
|
||||
[SerializeField,SerializeReference,SubclassSelector]
|
||||
private INetProvider netProvider;
|
||||
public void Main()
|
||||
{
|
||||
GameObject player = new("Player");
|
||||
|
||||
//Good Code
|
||||
netProvider.ServerRpc(KillPlayer,player);
|
||||
netProvider.ClientRpc(KillPlayer,player);
|
||||
|
||||
//Bad Code
|
||||
netProvider.SendRT(nameof(KillPlayer),player);
|
||||
netProvider.SendAllRT(nameof(KillPlayer),player);
|
||||
}
|
||||
public void KillPlayer(GameObject player)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
68
Core/Net/NetUtils.cs
Normal file
68
Core/Net/NetUtils.cs
Normal file
@@ -0,0 +1,68 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
#if GameDesigner && UNITY
|
||||
using System.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
|
||||
namespace BITKit
|
||||
{
|
||||
public class NetUtils : MonoBehaviour
|
||||
{
|
||||
/// <summary>
|
||||
/// 客户端调用ServerRpcMyMessage方法
|
||||
/// </summary>
|
||||
/// <param name="message"></param>
|
||||
public static void ClientInvokeServerRpcMyMessage(string message)
|
||||
{
|
||||
//简化方法
|
||||
ServerRpc<string>(NetUtils.ServerRpcMyMessage,message);
|
||||
//原始方法
|
||||
SendRT(nameof(ServerRpcMyMessage),message);
|
||||
}
|
||||
/// <summary>
|
||||
/// 仅服务器调用该方法
|
||||
/// </summary>
|
||||
/// <param name="message"></param>
|
||||
[ServerRpc]
|
||||
public static void ServerRpcMyMessage(string message)
|
||||
{
|
||||
ClientRpc<string>(ClientRpcMyMessage,message);
|
||||
}
|
||||
/// <summary>
|
||||
/// 所有客户端调用该方法
|
||||
/// </summary>
|
||||
/// <param name="message"></param>
|
||||
[ClientRpc]
|
||||
public static void ClientRpcMyMessage(string message)
|
||||
{
|
||||
Debug.Log($"已收到消息:{message}");
|
||||
}
|
||||
public static void InvokeOnServer<T>(Action<T> action, params object[] pars)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public static void RpcAll(Action<object> obj,params object[] pars)
|
||||
{
|
||||
|
||||
}
|
||||
public static void ClientRpc<T>(Action<T> obj, params object[] pars)
|
||||
{
|
||||
|
||||
}
|
||||
public static void ServerRpc<T>(Action<T> obj, params object[] pars)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
public static void SendRT(string funcName, params object[] pars)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
13
Core/Net/WebProvider.cs
Normal file
13
Core/Net/WebProvider.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using System.Threading;
|
||||
namespace BITKit.HttpNet
|
||||
{
|
||||
[System.Serializable]
|
||||
public abstract class WebProvider : IWebProvider
|
||||
{
|
||||
public abstract UniTask<string> GetAsync(string url, CancellationToken cancellationToken = default);
|
||||
public abstract UniTask<string> PostAsync<T>(string url, T value, CancellationToken cancellationToken = default);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user