Skip to content

Instantly share code, notes, and snippets.

@tak-km
Created March 4, 2018 05:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save tak-km/1bd9860794434f35d2725f83b82e9987 to your computer and use it in GitHub Desktop.
Save tak-km/1bd9860794434f35d2725f83b82e9987 to your computer and use it in GitHub Desktop.
using UnityEngine;
using System;
using System.Collections;
using System.Net;
using System.IO;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using MiniJSON;
public class UDPReceive : MonoBehaviour
{
CharactorActor c_actor;
int LOCAL_PORT = 22222;
static UdpClient udp;
Thread thread;
public string parameter;
public static Action<string> DataCallBack;
void Start ()
{
UDPReceive.DataCallBack += Method;
UDPStart();
}
public void UDPStart(){
udp = new UdpClient(LOCAL_PORT);
thread = new Thread(new ThreadStart(ThreadMethod));
thread.Start();
}
public void Method(string data){
parameter = data;
}
void OnApplicationQuit()
{
thread.Abort();
}
private static void ThreadMethod()
{
while(true)
{
IPEndPoint remoteEP = null;
byte[] data = udp.Receive(ref remoteEP);
string text = Encoding.ASCII.GetString(data);
DataCallBack(text);
//Debug.Log(text);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment