(client, server) feat:Add UDP client and server
This commit is contained in:
28
Client/Assets/Scripts/Network/UnityUdpClient.cs
Normal file
28
Client/Assets/Scripts/Network/UnityUdpClient.cs
Normal file
@ -0,0 +1,28 @@
|
||||
using System.Net.Sockets;
|
||||
using System.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
using Utils;
|
||||
|
||||
namespace Network
|
||||
{
|
||||
public class UnityUdpClient : Singleton<UnityUdpClient>
|
||||
{
|
||||
private readonly UdpClient _client;
|
||||
|
||||
public UnityUdpClient()
|
||||
{
|
||||
_client = new UdpClient();
|
||||
_client.Connect("127.0.0.1", 12345);
|
||||
|
||||
Application.quitting += () => _client.Close();
|
||||
}
|
||||
|
||||
public async Task<byte[]> SendAndReceiveData(byte[] data)
|
||||
{
|
||||
await _client.SendAsync(data, data.Length);
|
||||
|
||||
var result = await _client.ReceiveAsync();
|
||||
return result.Buffer;
|
||||
}
|
||||
}
|
||||
}
|
2
Client/Assets/Scripts/Network/UnityUdpClient.cs.meta
Normal file
2
Client/Assets/Scripts/Network/UnityUdpClient.cs.meta
Normal file
@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1dde9b247fc7ffc478f6b77a79c1e11c
|
Reference in New Issue
Block a user