Files
BITFALL/Assets/Artists/Scripts/Network/ClientService.cs
CortexCore cd02761be7 init
2023-06-08 14:09:50 +08:00

55 lines
1.5 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using BITKit;
using Cysharp.Threading.Tasks;
using Net.Client;
using Net.Event;
using UnityEngine;
namespace BITFALL
{
public class ClientService:MonoBehaviour
{
public static event Action OnConnedted;
public static event Action OnDisconnect;
private static readonly List<object> RPCHandles = new();
public static void AddRpcHandle(object handle) => RPCHandles.Add(handle);
public static void RemoveRpcHandle(object handle) => RPCHandles.Remove(handle);
public async Task<bool> Connect(string ip = "localhost", ushort port = 27014)
{
var client = DI.Get<ClientBase>();
if (client.Connected) return false;
client.host = ip;
client.port = port;
foreach (var handle in RPCHandles)
{
client.AddRpcHandle(handle);
}
if (await client.Connect())
{
OnConnedted?.Invoke();
return true;
}
else
{
OnDisconnect?.Invoke();
return false;
}
}
public bool Disconnect()
{
var client = DI.Get<ClientBase>();
if (!client.Connected) return false;
client.Close();
OnDisconnect?.Invoke();
return true;
}
}
}