Hey, I’ve been trying to get Unity to communicate with ZeroMQ based on a handful of tutorials and examples (1, 2, 3, 4), but alas, it doesn’t seem to be working. My code is as follows:
client.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using NetMQ;
using NetMQ.Sockets;
public class client : MonoBehaviour {
void Start() {
AsyncIO.ForceDotNet.Force();
using (var socket = new RequestSocket())
{
socket.Connect("tcp://localhost:12345");
socket.TrySendFrame("Hello");
Debug.Log("sent");
}
NetMQConfig.Cleanup();
}
}
server.py
import zmq
import time
context = zmq.Context()
socket = context.socket(zmq.REP)
socket.bind("tcp://*:12345")
while True:
# Wait for next request from client
message = socket.recv()
print("Received request: %s" % message)
With this setup, we get to Debug.Log(“sent”) without errors, but nothing appears on the server. The following test client works:
testclient.py
import zmq
context = zmq.Context()
print("Connecting…")
socket = context.socket(zmq.REQ)
socket.connect("tcp://localhost:12345")
socket.send(b"Hello")
Am I doing something obviously wrong here on the Unity side? Any ideas for how to start investigating what’s going wrong? Apparently ZeroMQ won’t let you check if you’re connected successfully, but as no messages are going through, I assume not.
Thanks a lot!
Edit: if I remove AsyncIO.ForceDotNet.Force(); and NetMQConfig.Cleanup();, the message goes through. However, after that, Unity’s player won’t launch anymore and the program goes into an eternal state of reloading until you restart it. Hmm…