using System; using System.Collections.Generic; using System.Net; using System.Net.Sockets; using System.Text; using System.Threading; using System.Threading.Tasks; using UnityEngine; public class UdpConnection { //public int port = 9090; //public String ipAddress = "192.168.1.7"; private UdpClient client; private bool listening = false; private Action onAccData; public UdpConnection(String ipAddress, int port, Action onAccData) { this.onAccData = onAccData; client = new UdpClient(new IPEndPoint(IPAddress.Parse(ipAddress), port)); } public void Listen() => Task.Run(async () => { listening = true; while (listening) { var result = await client.ReceiveAsync(); String receivedStr = Encoding.UTF8.GetString(result.Buffer); var data = receivedStr.Split(':'); if (data.Length == 2) { var name = data[0]; if (name.Equals("ACC")) { onAccData(AccData.fromString(data[1])); }else if (name.Equals("ECG")) { //TODO }else if (name.Equals("HR")) { //TODO } } Debug.Log($"RECEIVED DATA VIA UDP: {receivedStr}"); } }); public void StopListening() { listening = false; } }