(client, server) feat:Add UDP client and server
This commit is contained in:
@ -568,6 +568,50 @@ Transform:
|
||||
m_Children: []
|
||||
m_Father: {fileID: 613797070}
|
||||
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
|
||||
--- !u!1 &1388451205
|
||||
GameObject:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
serializedVersion: 6
|
||||
m_Component:
|
||||
- component: {fileID: 1388451206}
|
||||
- component: {fileID: 1388451207}
|
||||
m_Layer: 0
|
||||
m_Name: UdpClientTest
|
||||
m_TagString: Untagged
|
||||
m_Icon: {fileID: 0}
|
||||
m_NavMeshLayer: 0
|
||||
m_StaticEditorFlags: 0
|
||||
m_IsActive: 1
|
||||
--- !u!4 &1388451206
|
||||
Transform:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 1388451205}
|
||||
serializedVersion: 2
|
||||
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
|
||||
m_LocalPosition: {x: 0, y: 0, z: 0}
|
||||
m_LocalScale: {x: 1, y: 1, z: 1}
|
||||
m_ConstrainProportionsScale: 0
|
||||
m_Children: []
|
||||
m_Father: {fileID: 0}
|
||||
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
|
||||
--- !u!114 &1388451207
|
||||
MonoBehaviour:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 1388451205}
|
||||
m_Enabled: 1
|
||||
m_EditorHideFlags: 0
|
||||
m_Script: {fileID: 11500000, guid: d76ad8f9d40092848abc97f05b1e2131, type: 3}
|
||||
m_Name:
|
||||
m_EditorClassIdentifier: Assembly-CSharp::Test.UdpClientTest
|
||||
--- !u!1 &2036983430
|
||||
GameObject:
|
||||
m_ObjectHideFlags: 0
|
||||
@ -684,4 +728,5 @@ SceneRoots:
|
||||
- {fileID: 1057087090}
|
||||
- {fileID: 613797070}
|
||||
- {fileID: 2104915508}
|
||||
- {fileID: 1388451206}
|
||||
- {fileID: 2036983432}
|
||||
|
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
|
19
Client/Assets/Scripts/Test/UdpClientTest.cs
Normal file
19
Client/Assets/Scripts/Test/UdpClientTest.cs
Normal file
@ -0,0 +1,19 @@
|
||||
using Network;
|
||||
using System.Text;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Test
|
||||
{
|
||||
public class UdpClientTest : MonoBehaviour
|
||||
{
|
||||
private async void Start()
|
||||
{
|
||||
var sendBytes = Encoding.UTF8.GetBytes("Test 汉语 and English simultaneously!");
|
||||
|
||||
var receivedBytes = await UnityUdpClient.Instance.SendAndReceiveData(sendBytes);
|
||||
var receivedString = Encoding.UTF8.GetString(receivedBytes);
|
||||
|
||||
Debug.Log($"Received string: {receivedString}");
|
||||
}
|
||||
}
|
||||
}
|
2
Client/Assets/Scripts/Test/UdpClientTest.cs.meta
Normal file
2
Client/Assets/Scripts/Test/UdpClientTest.cs.meta
Normal file
@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d76ad8f9d40092848abc97f05b1e2131
|
Reference in New Issue
Block a user