This commit is contained in:
2026-02-12 22:15:15 +08:00
parent 47a5cff08b
commit 502c6efedc
58 changed files with 2114 additions and 708 deletions

View File

@@ -16,19 +16,44 @@ namespace NBF
public JToken Data { get; set; }
}
class NetData
{
public string action;
public Dictionary<string, string> form;
public Action<HttpResult> callback = null;
public bool Success = false;
public void Send()
{
}
}
public class Net : MonoBehaviour
{
private string serverUrl = "http://localhost:8080";
public string ServerUrl { get; private set; } = "http://localhost:8080";
private string serverPassword = "";
public static Net Instance { get; private set; }
public event Action<UnityWebRequest.Result, string> OnError;
private string DeviceId;
private string DeviceName;
Queue<NetData> _netDataQueue = new Queue<NetData>();
private void Awake()
{
Instance = this;
}
private void Start()
{
DeviceId = PlatformInfo.GetAndroidID();
DeviceName = PlatformInfo.GetDeviceModel();
}
public void SetServer(string server, string password)
{
if (!(server.StartsWith("http://") || server.StartsWith("https://")))
@@ -45,11 +70,24 @@ namespace NBF
server = server.Substring(0, server.Length - 1);
}
serverUrl = server;
ServerUrl = server;
serverPassword = password;
}
public void Tick(int id, int time, int count, Action<HttpResult> callback = null)
{
var dic = new Dictionary<string, string>
{
{ "device", DeviceId },
{ "deviceName", DeviceName },
{ "id", id.ToString() },
{ "time", time.ToString() },
{ "watchCount", count.ToString() }
};
Send("/api/tick", dic, callback);
}
#region Post
public void Send(string action, Dictionary<string, string> form, Action<HttpResult> callback = null)
@@ -65,11 +103,11 @@ namespace NBF
form = new Dictionary<string, string>();
}
var url = serverUrl + action;
var url = ServerUrl + action;
form["sign"] = serverPassword;
UnityWebRequest request = UnityWebRequest.Post(url, form);
request.downloadHandler = new DownloadHandlerBuffer();
Debug.Log("Request: " + JsonConvert.SerializeObject(form));
// 4⃣ 发送
yield return request.SendWebRequest();
@@ -83,6 +121,7 @@ namespace NBF
Code = -1,
Data = request.error
});
OnError?.Invoke(request.result, request.error);
}
else
{