1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- using System;
- using System.Net;
- using System.Net.Sockets;
- using System.Text;
- using System.Threading.Tasks;
- using UnityEngine;
- public class PolarClient
- {
- private static bool running = false;
- private static UdpClient client;
- //TODO: onData callback
- public static void Start(string ipAddress, int port)
- {
- var endPoint = new IPEndPoint(IPAddress.Parse(ipAddress), port);
- client = new UdpClient();
- try
- {
- Debug.Log("PolarClient: Connecting to server");
- client.Connect(endPoint);
- running = true;
- Debug.Log($"PolarClient: Connected to server: {client.Client.Connected}");
- var d = Encoding.UTF8.GetBytes("Blabla");
- client.Send(d, d.Length);
-
- while (running)
- {
- var data = client.Receive(ref endPoint);
- HandleData(data);
- }
-
- /*await Task.Run(()
- =>
- {
-
- })*;*/
- }
- catch (Exception e)
- {
- Debug.Log($"PolarClient Error: {e}");
- }
- }
- private static void Stop()
- {
- running = false;
- client.Dispose();
- }
- private static void HandleData(byte[] data)
- {
- Debug.Log($"PolarClient Data: {Encoding.UTF8.GetString(data)}");
- }
- }
|