Skip to content

Instantly share code, notes, and snippets.

@togucchi
Created October 7, 2019 07:57
Show Gist options
  • Save togucchi/b9497eb84302c02f711314f38675161f to your computer and use it in GitHub Desktop.
Save togucchi/b9497eb84302c02f711314f38675161f to your computer and use it in GitHub Desktop.
using System;
using UnityEngine;
using UnityEngine.Events;
using UniRx;
namespace uOSC
{
public class uOscObservableServer : MonoBehaviour
{
[SerializeField]
int port = 3333;
#if NETFX_CORE
Udp udp_ = new Uwp.Udp();
Thread thread_ = new Uwp.Thread();
#else
Udp udp_ = new DotNet.Udp();
Thread thread_ = new DotNet.Thread();
#endif
Parser parser_ = new Parser();
private Subject<Message> dataReceivedSubject = new Subject<Message>();
public IObservable<Message> OnDataReceived => dataReceivedSubject;
void OnEnable()
{
udp_.StartServer(port);
thread_.Start(UpdateMessage);
}
void OnDisable()
{
thread_.Stop();
udp_.Stop();
}
void Update()
{
while (parser_.messageCount > 0)
{
var message = parser_.Dequeue();
dataReceivedSubject.OnNext(message);
}
}
void UpdateMessage()
{
while (udp_.messageCount > 0)
{
var buf = udp_.Receive();
int pos = 0;
parser_.Parse(buf, ref pos, buf.Length);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment