Closed alessandrolopopolo closed 5 years ago
Have you tried to just open the project in this repo in Unity 2017.3.1? I’m using Unity 2017.3.1 these days, and it works fine. Maybe you forget to setup a separate thread? Or you forgot to run AsyncIO.ForceDotNet.Force(); before you start NetMQ?
Hi, my code is the same of this project, i test only the Client and i change the subscription and switch connect to bind like this:
public class NetMqListener
{
private readonly Thread _listenerWorker;
private bool _listenerCancelled;
public delegate void MessageDelegate(string message);
private readonly MessageDelegate _messageDelegate;
private readonly ConcurrentQueue<string> _messageQueue = new ConcurrentQueue<string>();
private bool frameBool;
private string frameString;
private void ListenerWork()
{
AsyncIO.ForceDotNet.Force();
using (var subSocket = new SubscriberSocket())
{
// subSocket.Options.ReceiveHighWatermark = 1000;
subSocket.Bind("tcp://*:9367");
/* subSocket.SubscribeToAnyTopic();
Debug.Log("Subscribe SubscribeToAnyTopic");*/
subSocket.Subscribe("cardio");
Debug.Log("Subscribe on cardio");
subSocket.Subscribe("devices");
Debug.Log("Subscribe on devices");
subSocket.Subscribe("mind");
Debug.Log("Subscribe on mind");
while (!_listenerCancelled)
{
if (!subSocket.TryReceiveFrameString(out frameString)) continue;
// frameString = subSocket.ReceiveFrameBytes().ToString();
Debug.Log(frameString);
_messageQueue.Enqueue(frameString);
}
subSocket.Close();
}
NetMQConfig.Cleanup();
}
public void Update()
{
Debug.Log("Update");
while (!_messageQueue.IsEmpty)
{
string message;
if (_messageQueue.TryDequeue(out message))
{
Debug.Log("message: "+ message);
_messageDelegate(message);
}
else
{
break;
}
}
}
public NetMqListener(MessageDelegate messageDelegate)
{
_messageDelegate = messageDelegate;
Debug.Log("NetMqListener messageDelegate: " + messageDelegate);
_listenerWorker = new Thread(ListenerWork);
}
public void Start()
{
_listenerCancelled = false;
_listenerWorker.Start();
}
public void Stop()
{
_listenerCancelled = true;
_listenerWorker.Join();
}
}
public class ClientObject : MonoBehaviour
{
private NetMqListener _netMqListener;
private void HandleMessage(string message)
{
Debug.Log("HandleMessage: "+message);
var splittedStrings = message.Split(' ');
if (splittedStrings.Length != 3) return;
var x = float.Parse(splittedStrings[0]);
var y = float.Parse(splittedStrings[1]);
var z = float.Parse(splittedStrings[2]);
transform.position = new Vector3(x, y, z);
}
private void Start()
{
_netMqListener = new NetMqListener(HandleMessage);
_netMqListener.Start();
Debug.Log("_netMqListener start");
}
private void Update()
{
_netMqListener.Update();
}
private void OnDestroy()
{
_netMqListener.Stop();
}
}
I send data with this script in JAVA:
ZContext context = new ZContext();
ZMQ.Socket publisher = context.createSocket(ZMQ.PUB);
publisher.connect("tcp://192.168.1.114:9367");
System.out.println("Publisher connect done");
// Ensure subscriber connection has time to complete
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
publisher.sendMore("cardio");
publisher.send("First");
publisher.sendMore("mind");
publisher.send("Second");
publisher.sendMore("controller");
publisher.send("Third");
publisher.sendMore("cardio");
publisher.send("Fourth");
context.close();
The same code in Unity 2017.1. works fine but with the other version 2017.2. and 2017.3.*:
if (!subSocket.TryReceiveFrameString(out frameString)) continue;
Debug.Log(frameString);
frameString is Null always;
Why in unity 2017.3.1 does not work? the same code in unity 2017.1 ... works well.
In version 2017.3.1 "frameString" always is null.
Does anyone know what has changed?