Invoking action on receiving data
This commit is contained in:
parent
0b09d473e4
commit
533ddb404f
|
@ -1,6 +1,6 @@
|
|||
using System;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using UnityEngine;
|
||||
|
||||
|
@ -10,10 +10,10 @@ namespace GameDev.UDP
|
|||
{
|
||||
[SerializeField] private int _port;
|
||||
[SerializeField] private string _ipAddress;
|
||||
[SerializeField] private bool listen;
|
||||
|
||||
[Header("State")]
|
||||
[SerializeField] private byte _watchdog;
|
||||
public byte[] Data { get; private set; }
|
||||
|
||||
private UdpClient UdpClient { get; set; }
|
||||
|
||||
|
@ -21,6 +21,8 @@ namespace GameDev.UDP
|
|||
|
||||
private IPAddress _ipAddressParsed;
|
||||
|
||||
public event Action ReceivedNewUdpData;
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
_ipAddressParsed = IPAddress.Parse(_ipAddress);
|
||||
|
@ -28,7 +30,6 @@ namespace GameDev.UDP
|
|||
UdpClient = new(_port);
|
||||
UdpClient.JoinMulticastGroup(_ipAddressParsed);
|
||||
_receiveThread = new(ReceiveData);
|
||||
_receiveThread.IsBackground = true;
|
||||
_receiveThread.Start();
|
||||
}
|
||||
|
||||
|
@ -41,8 +42,9 @@ namespace GameDev.UDP
|
|||
try
|
||||
{
|
||||
var data = UdpClient.Receive(ref ipEndPoint);
|
||||
|
||||
_watchdog = data[0];
|
||||
Data = data;
|
||||
ReceivedNewUdpData.Invoke();
|
||||
}
|
||||
catch (SocketException e)
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue