2024-10-19 18:00:02 +03:00
|
|
|
|
using System.Collections;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Net.Sockets;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
using UnityEngine.Networking;
|
2024-10-19 20:58:35 +03:00
|
|
|
|
using UnityEngine.Serialization;
|
2024-10-19 18:00:02 +03:00
|
|
|
|
|
|
|
|
|
namespace Network
|
|
|
|
|
{
|
|
|
|
|
public class HttpRequests : MonoBehaviour
|
|
|
|
|
{
|
2024-10-19 20:58:35 +03:00
|
|
|
|
public CustomNetworkManager NetworkManager;
|
|
|
|
|
|
2024-10-19 18:00:02 +03:00
|
|
|
|
private string baseUrl = "https://kotpilnya.xyz/slowpoker/api/";
|
|
|
|
|
public Dictionary<string, string> ServerListResponse;
|
2024-10-19 20:58:35 +03:00
|
|
|
|
public string externalIP = "localhost";
|
2024-10-19 18:00:02 +03:00
|
|
|
|
|
|
|
|
|
[System.Serializable] public class ServerInfo
|
|
|
|
|
{
|
|
|
|
|
public string name;
|
|
|
|
|
public string address;
|
|
|
|
|
}
|
|
|
|
|
void Start()
|
|
|
|
|
{
|
|
|
|
|
StartCoroutine(GetExternalIPAddress());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void AddServer()
|
|
|
|
|
{
|
|
|
|
|
ServerInfo serverInfo = new ServerInfo
|
|
|
|
|
{
|
|
|
|
|
name = "Oscar",
|
2024-10-19 20:58:35 +03:00
|
|
|
|
address = externalIP
|
2024-10-19 18:00:02 +03:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
string jsonData = JsonUtility.ToJson(serverInfo);
|
|
|
|
|
StartCoroutine(SendPostRequest("add_server", jsonData));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void GetServerList()
|
|
|
|
|
{
|
|
|
|
|
StartCoroutine(SendGetRequest("get_server_list"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
IEnumerator GetExternalIPAddress()
|
|
|
|
|
{
|
|
|
|
|
UnityWebRequest request = UnityWebRequest.Get("https://api.ipify.org");
|
|
|
|
|
yield return request.SendWebRequest();
|
|
|
|
|
|
|
|
|
|
if (request.result == UnityWebRequest.Result.ConnectionError || request.result == UnityWebRequest.Result.ProtocolError)
|
|
|
|
|
{
|
|
|
|
|
Debug.LogError("Error getting external IP address: " + request.error);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-10-19 20:58:35 +03:00
|
|
|
|
externalIP = request.downloadHandler.text;
|
|
|
|
|
// NetworkManager.networkAddress = _externalIP;
|
|
|
|
|
Debug.Log("External IP Address: " + externalIP);
|
2024-10-19 18:00:02 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
IEnumerator SendGetRequest(string endpoint)
|
|
|
|
|
{
|
|
|
|
|
UnityWebRequest request = UnityWebRequest.Get(baseUrl + endpoint);
|
|
|
|
|
|
|
|
|
|
yield return request.SendWebRequest();
|
|
|
|
|
|
|
|
|
|
if (request.result == UnityWebRequest.Result.ConnectionError || request.result == UnityWebRequest.Result.ProtocolError)
|
|
|
|
|
{
|
|
|
|
|
Debug.LogError("Error: " + request.error);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
OnGetServerList(request.downloadHandler.text);
|
|
|
|
|
Debug.Log("Response: " + request.downloadHandler.text);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
IEnumerator SendPostRequest(string endpoint, string jsonData)
|
|
|
|
|
{
|
|
|
|
|
UnityWebRequest request = new UnityWebRequest(baseUrl + endpoint, "POST");
|
|
|
|
|
|
|
|
|
|
byte[] bodyRaw = System.Text.Encoding.UTF8.GetBytes(jsonData);
|
|
|
|
|
request.uploadHandler = new UploadHandlerRaw(bodyRaw);
|
|
|
|
|
|
|
|
|
|
request.SetRequestHeader("Content-Type", "application/json");
|
|
|
|
|
|
|
|
|
|
request.downloadHandler = new DownloadHandlerBuffer();
|
|
|
|
|
|
|
|
|
|
yield return request.SendWebRequest();
|
|
|
|
|
|
|
|
|
|
if (request.result == UnityWebRequest.Result.ConnectionError || request.result == UnityWebRequest.Result.ProtocolError)
|
|
|
|
|
{
|
|
|
|
|
Debug.LogError("Error: " + request.error);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Debug.Log("Response: " + request.downloadHandler.text);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
IEnumerator SendDeleteRequest(string endpoint)
|
|
|
|
|
{
|
|
|
|
|
UnityWebRequest request = UnityWebRequest.Delete(baseUrl + endpoint);
|
|
|
|
|
|
|
|
|
|
yield return request.SendWebRequest();
|
|
|
|
|
|
|
|
|
|
if (request.result == UnityWebRequest.Result.ConnectionError || request.result == UnityWebRequest.Result.ProtocolError)
|
|
|
|
|
{
|
|
|
|
|
Debug.LogError("Error: " + request.error);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Debug.Log("Response: " + request.downloadHandler.text);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
void OnGetServerList(string response)
|
|
|
|
|
{
|
|
|
|
|
string jsonResponse = response;
|
|
|
|
|
ServerInfo serverInfo = JsonUtility.FromJson<ServerInfo>(jsonResponse);
|
|
|
|
|
|
|
|
|
|
ServerListResponse = new Dictionary<string, string>
|
|
|
|
|
{
|
|
|
|
|
{ "name", serverInfo.name },
|
|
|
|
|
{ "address", serverInfo.address }
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|