mirror of
https://github.com/GTA-ASM/SanAndreasUnity
synced 2025-02-17 05:18:27 +00:00
add namespace
This commit is contained in:
parent
cfab2fec73
commit
9f83eadabb
1 changed files with 97 additions and 96 deletions
|
@ -1,6 +1,4 @@
|
|||
using System;
|
||||
using Newtonsoft.Json;
|
||||
using SanAndreasUnity.Net;
|
||||
using Newtonsoft.Json;
|
||||
using SanAndreasUnity.Utilities;
|
||||
using System.Collections.Generic;
|
||||
using System.Net.Http;
|
||||
|
@ -8,115 +6,118 @@ using System.Text;
|
|||
using System.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
|
||||
public class MasterServerClient : MonoBehaviour
|
||||
namespace SanAndreasUnity.Net
|
||||
{
|
||||
private string _masterServerUrl;
|
||||
private ServerInfo _serverInfo;
|
||||
private bool _updating;
|
||||
public static MasterServerClient Instance { get; private set; }
|
||||
private HttpClient _client;
|
||||
|
||||
|
||||
private void Awake()
|
||||
public class MasterServerClient : MonoBehaviour
|
||||
{
|
||||
Instance = this;
|
||||
}
|
||||
private string _masterServerUrl;
|
||||
private ServerInfo _serverInfo;
|
||||
private bool _updating;
|
||||
public static MasterServerClient Instance { get; private set; }
|
||||
private HttpClient _client;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
_client = new HttpClient();
|
||||
|
||||
Config.Load();
|
||||
_masterServerUrl = Config.Get<string>("masterserverurl");
|
||||
|
||||
if (string.IsNullOrWhiteSpace(_masterServerUrl))
|
||||
Debug.LogError("Url of master server not defined in config");
|
||||
|
||||
NetManager.Instance.onServerStatusChanged += OnServerStatusChange;
|
||||
}
|
||||
|
||||
private async void OnServerStatusChange()
|
||||
{
|
||||
if (!NetStatus.IsServer)
|
||||
return;
|
||||
|
||||
if (string.IsNullOrWhiteSpace(_masterServerUrl))
|
||||
return;
|
||||
|
||||
_serverInfo = new ServerInfo
|
||||
private void Awake()
|
||||
{
|
||||
Name = Config.Get<string>("server_name"),
|
||||
Port = NetManager.listenPortNumber,
|
||||
NumPlayersOnline = NetManager.numConnections,
|
||||
MaxPlayers = NetManager.maxNumPlayers,
|
||||
};
|
||||
Instance = this;
|
||||
}
|
||||
|
||||
await RegisterServer();
|
||||
}
|
||||
|
||||
private async Task RegisterServer()
|
||||
{
|
||||
await SendRequestToRegister();
|
||||
|
||||
_updating = true;
|
||||
Invoke(nameof(UpdateServer), 10);
|
||||
}
|
||||
|
||||
private async Task UpdateServer()
|
||||
{
|
||||
while (_updating)
|
||||
private void Start()
|
||||
{
|
||||
_serverInfo.NumPlayersOnline = NetManager.numConnections;
|
||||
_client = new HttpClient();
|
||||
|
||||
Config.Load();
|
||||
_masterServerUrl = Config.Get<string>("masterserverurl");
|
||||
|
||||
if (string.IsNullOrWhiteSpace(_masterServerUrl))
|
||||
Debug.LogError("Url of master server not defined in config");
|
||||
|
||||
NetManager.Instance.onServerStatusChanged += OnServerStatusChange;
|
||||
}
|
||||
|
||||
private async void OnServerStatusChange()
|
||||
{
|
||||
if (!NetStatus.IsServer)
|
||||
return;
|
||||
|
||||
if (string.IsNullOrWhiteSpace(_masterServerUrl))
|
||||
return;
|
||||
|
||||
_serverInfo = new ServerInfo
|
||||
{
|
||||
Name = Config.Get<string>("server_name"),
|
||||
Port = NetManager.listenPortNumber,
|
||||
NumPlayersOnline = NetManager.numConnections,
|
||||
MaxPlayers = NetManager.maxNumPlayers,
|
||||
};
|
||||
|
||||
await RegisterServer();
|
||||
}
|
||||
|
||||
private async Task RegisterServer()
|
||||
{
|
||||
await SendRequestToRegister();
|
||||
|
||||
await Task.Delay(10000);
|
||||
_updating = true;
|
||||
Invoke(nameof(UpdateServer), 10);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task SendRequestToRegister()
|
||||
{
|
||||
var response = await _client.PostAsync(_masterServerUrl + "/register", new StringContent(JsonConvert.SerializeObject(_serverInfo), Encoding.UTF8, "application/json"));
|
||||
|
||||
if (!response.IsSuccessStatusCode)
|
||||
private async Task UpdateServer()
|
||||
{
|
||||
string str = await response.Content.ReadAsStringAsync();
|
||||
Debug.LogError($"Master server returned error while trying to register: {str}");
|
||||
while (_updating)
|
||||
{
|
||||
_serverInfo.NumPlayersOnline = NetManager.numConnections;
|
||||
|
||||
await SendRequestToRegister();
|
||||
|
||||
await Task.Delay(10000);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task SendRequestToRegister()
|
||||
{
|
||||
var response = await _client.PostAsync(_masterServerUrl + "/register", new StringContent(JsonConvert.SerializeObject(_serverInfo), Encoding.UTF8, "application/json"));
|
||||
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
string str = await response.Content.ReadAsStringAsync();
|
||||
Debug.LogError($"Master server returned error while trying to register: {str}");
|
||||
}
|
||||
}
|
||||
|
||||
private async Task UnregisterServer()
|
||||
{
|
||||
_updating = false;
|
||||
|
||||
if (null == _serverInfo)
|
||||
return;
|
||||
|
||||
if (string.IsNullOrWhiteSpace(_masterServerUrl))
|
||||
return;
|
||||
|
||||
await _client.PostAsync(_masterServerUrl + "/unregister", new StringContent(JsonConvert.SerializeObject(_serverInfo), Encoding.UTF8, "application/json"));
|
||||
}
|
||||
|
||||
public async Task<List<ServerInfo>> GetAllServers()
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(_masterServerUrl))
|
||||
return new List<ServerInfo>();
|
||||
|
||||
return JsonConvert.DeserializeObject<List<ServerInfo>>(await _client.GetStringAsync(_masterServerUrl));
|
||||
}
|
||||
|
||||
public async void OnDestroy()
|
||||
{
|
||||
await UnregisterServer();
|
||||
}
|
||||
}
|
||||
|
||||
private async Task UnregisterServer()
|
||||
public class ServerInfo
|
||||
{
|
||||
_updating = false;
|
||||
|
||||
if (null == _serverInfo)
|
||||
return;
|
||||
|
||||
if (string.IsNullOrWhiteSpace(_masterServerUrl))
|
||||
return;
|
||||
|
||||
await _client.PostAsync(_masterServerUrl + "/unregister", new StringContent(JsonConvert.SerializeObject(_serverInfo), Encoding.UTF8, "application/json"));
|
||||
}
|
||||
|
||||
public async Task<List<ServerInfo>> GetAllServers()
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(_masterServerUrl))
|
||||
return new List<ServerInfo>();
|
||||
|
||||
return JsonConvert.DeserializeObject<List<ServerInfo>>(await _client.GetStringAsync(_masterServerUrl));
|
||||
}
|
||||
|
||||
public async void OnDestroy()
|
||||
{
|
||||
await UnregisterServer();
|
||||
public string Name { get; set; }
|
||||
public int NumPlayersOnline { get; set; }
|
||||
public string IP { get; set; }
|
||||
public int Port { get; set; }
|
||||
public int MaxPlayers { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
public class ServerInfo
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public int NumPlayersOnline { get; set; }
|
||||
public string IP { get; set; }
|
||||
public int Port { get; set; }
|
||||
public int MaxPlayers { get; set; }
|
||||
}
|
Loading…
Add table
Reference in a new issue