Multiple Sockets in Unity

I was wondering if any of you have been able to connect to multiple sockets across different scripts? Every time I try to run these two scripts together it causes Unity to freeze and for the game not to be loaded. Does anyone know how to fix this issue? Thanks so much. Here are the two script:

First script attached to a game object:

using NetMQ;
using NetMQ.Sockets;
using System;
using System.Collections.Concurrent;
using System.Threading;
using UnityEngine;
using UnityEngine.UI;
using AsyncIO;

public class Sender : MonoBehaviour
{
    private DateTime today;
    private PushSocket socket;

    public OVRInput.Controller leftController;
    public OVRInput.Controller rightController;
    private ControllerState stateStore;

    private void Start()
    {
        ForceDotNet.Force();
        socket = new PushSocket();
        socket.Bind("tcp://*:23456");
        stateStore = new ControllerState(leftController, rightController);
    }

    private void Update()
    {
        stateStore.UpdateState();
        socket.SendFrame(stateStore.ToJSON());
    }

    private void OnDestroy()
    {
        var terminationString = "terminate";
        for (int i = 0; i < 10; i++)
        {
            socket.SendFrame(terminationString);
        }
        socket.Close();
        NetMQConfig.Cleanup();
    }
    private void OnApplicationPause()
    {
        var terminationString = "terminate";
        for (int i = 0; i < 10; i++)
        {
            socket.SendFrame(terminationString);
        }
        socket.Close();
        NetMQConfig.Cleanup();
    }
}

Second script attached to a game object:

using NetMQ;
using NetMQ.Sockets;
using System;
using System.Collections.Concurrent;
using System.Threading;
using UnityEngine;
using UnityEngine.UI;

[Serializable]
public struct Data
{
    public bool bool_;
    public int int_;
    public string str;
    public byte[] image;
}

public class ReceiverOneWay
{
    private readonly Thread receiveThread;
    private bool running;

    public ReceiverOneWay()
    {
        receiveThread = new Thread((object callback) =>
        {
            using (var socket = new SubscriberSocket())  // <- The PULL socket
            {
                socket.Connect("tcp://someipaddress:12345");
                socket.Subscribe("");
              
                var localEndpoint = socket.Options.LastEndpoint;
                Debug.Log(localEndpoint);

                while (running)
                {

                    Data data = new Data();
                    data.image = socket.ReceiveFrameBytes();
                    data.str = "frame loaded : )";
                    //Debug.Log("received image");
                    ((Action<Data>)callback)(data);
                   
                }
            }
        });
    }

    public void Start(Action<Data> callback)
    {
        running = true;
        receiveThread.Start(callback);
    }

    public void Stop()
    {
        running = false;
        receiveThread.Join();
    }
}

public class Client : MonoBehaviour
{
    private readonly ConcurrentQueue<Action> runOnMainThread = new ConcurrentQueue<Action>();
    private ReceiverOneWay receiver;
    private Texture2D tex;
    public RawImage image;

    public void Start()
    {
        tex = new Texture2D(2, 2, TextureFormat.RGB24, mipChain: false);
        image.texture = tex;

        AsyncIO.ForceDotNet.Force();
        // - You might remove it, but if you have more than one socket
        //   in the following threads, leave it.
        receiver = new ReceiverOneWay();
        receiver.Start((Data d) => runOnMainThread.Enqueue(() =>
            {  
                if (d.str != null){
                    //Debug.Log(d.str);
                    tex.LoadImage(d.image);
                }
               
            }
        ));
    }

    public void Update()
    {
        if (!runOnMainThread.IsEmpty)
        {
            Action action;
            while (runOnMainThread.TryDequeue(out action))
            {
                action.Invoke();
            }
        }
    }

    private void OnDestroy()
    {
        receiver.Stop();
        NetMQConfig.Cleanup();  // Must be here to work more than once
        NetMQConfig.ContextTerminate();
    }
}
1 Like

Hi did you find a fix regarding this? I also encounter the same problem.