Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Question Multiple Sockets in Unity

Discussion in 'Scripting' started by tealsu, May 15, 2024.

  1. tealsu

    tealsu

    Joined:
    Jul 22, 2019
    Posts:
    1
    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:
    Code (CSharp):
    1. using NetMQ;
    2. using NetMQ.Sockets;
    3. using System;
    4. using System.Collections.Concurrent;
    5. using System.Threading;
    6. using UnityEngine;
    7. using UnityEngine.UI;
    8. using AsyncIO;
    9.  
    10. public class Sender : MonoBehaviour
    11. {
    12.     private DateTime today;
    13.     private PushSocket socket;
    14.  
    15.     public OVRInput.Controller leftController;
    16.     public OVRInput.Controller rightController;
    17.     private ControllerState stateStore;
    18.  
    19.     private void Start()
    20.     {
    21.         ForceDotNet.Force();
    22.         socket = new PushSocket();
    23.         socket.Bind("tcp://*:23456");
    24.         stateStore = new ControllerState(leftController, rightController);
    25.     }
    26.  
    27.     private void Update()
    28.     {
    29.         stateStore.UpdateState();
    30.         socket.SendFrame(stateStore.ToJSON());
    31.     }
    32.  
    33.     private void OnDestroy()
    34.     {
    35.         var terminationString = "terminate";
    36.         for (int i = 0; i < 10; i++)
    37.         {
    38.             socket.SendFrame(terminationString);
    39.         }
    40.         socket.Close();
    41.         NetMQConfig.Cleanup();
    42.     }
    43.     private void OnApplicationPause()
    44.     {
    45.         var terminationString = "terminate";
    46.         for (int i = 0; i < 10; i++)
    47.         {
    48.             socket.SendFrame(terminationString);
    49.         }
    50.         socket.Close();
    51.         NetMQConfig.Cleanup();
    52.     }
    53. }
    Second script attached to a game object:
    Code (CSharp):
    1. using NetMQ;
    2. using NetMQ.Sockets;
    3. using System;
    4. using System.Collections.Concurrent;
    5. using System.Threading;
    6. using UnityEngine;
    7. using UnityEngine.UI;
    8.  
    9. [Serializable]
    10. public struct Data
    11. {
    12.     public bool bool_;
    13.     public int int_;
    14.     public string str;
    15.     public byte[] image;
    16. }
    17.  
    18. public class ReceiverOneWay
    19. {
    20.     private readonly Thread receiveThread;
    21.     private bool running;
    22.  
    23.     public ReceiverOneWay()
    24.     {
    25.         receiveThread = new Thread((object callback) =>
    26.         {
    27.             using (var socket = new SubscriberSocket())  // <- The PULL socket
    28.             {
    29.                 socket.Connect("tcp://someipaddress:12345");
    30.                 socket.Subscribe("");
    31.              
    32.                 var localEndpoint = socket.Options.LastEndpoint;
    33.                 Debug.Log(localEndpoint);
    34.  
    35.                 while (running)
    36.                 {
    37.  
    38.                     Data data = new Data();
    39.                     data.image = socket.ReceiveFrameBytes();
    40.                     data.str = "frame loaded : )";
    41.                     //Debug.Log("received image");
    42.                     ((Action<Data>)callback)(data);
    43.                    
    44.                 }
    45.             }
    46.         });
    47.     }
    48.  
    49.     public void Start(Action<Data> callback)
    50.     {
    51.         running = true;
    52.         receiveThread.Start(callback);
    53.     }
    54.  
    55.     public void Stop()
    56.     {
    57.         running = false;
    58.         receiveThread.Join();
    59.     }
    60. }
    61.  
    62. public class Client : MonoBehaviour
    63. {
    64.     private readonly ConcurrentQueue<Action> runOnMainThread = new ConcurrentQueue<Action>();
    65.     private ReceiverOneWay receiver;
    66.     private Texture2D tex;
    67.     public RawImage image;
    68.  
    69.     public void Start()
    70.     {
    71.         tex = new Texture2D(2, 2, TextureFormat.RGB24, mipChain: false);
    72.         image.texture = tex;
    73.  
    74.         AsyncIO.ForceDotNet.Force();
    75.         // - You might remove it, but if you have more than one socket
    76.         //   in the following threads, leave it.
    77.         receiver = new ReceiverOneWay();
    78.         receiver.Start((Data d) => runOnMainThread.Enqueue(() =>
    79.             {  
    80.                 if (d.str != null){
    81.                     //Debug.Log(d.str);
    82.                     tex.LoadImage(d.image);
    83.                 }
    84.                
    85.             }
    86.         ));
    87.     }
    88.  
    89.     public void Update()
    90.     {
    91.         if (!runOnMainThread.IsEmpty)
    92.         {
    93.             Action action;
    94.             while (runOnMainThread.TryDequeue(out action))
    95.             {
    96.                 action.Invoke();
    97.             }
    98.         }
    99.     }
    100.  
    101.     private void OnDestroy()
    102.     {
    103.         receiver.Stop();
    104.         NetMQConfig.Cleanup();  // Must be here to work more than once
    105.         NetMQConfig.ContextTerminate();
    106.     }
    107. }
     
    jmancoder likes this.
  2. ZU95

    ZU95

    Joined:
    Oct 31, 2023
    Posts:
    1
    Hi did you find a fix regarding this? I also encounter the same problem.