Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Unity and RabbitMQ Error

Discussion in 'Editor & General Support' started by CTE, Mar 24, 2013.

  1. CTE

    CTE

    Joined:
    Mar 24, 2013
    Posts:
    7
    Good Afternoon,

    I have been working with RabbitMQ and wanted to integrate it into a unity project I am working on so I can take advantage of the Pub/Sub nature of the Rabbit Queuing system.

    Unfortunately, whenever I attempt to create a connection I am getting some errors such as:

    "None of teh specified endpoints were reachable"

    if I take a look a little deeper at the inner exception I have:

    "Inner Exception: System.Net.Sockets.SocketException: An address incompatible with the requested protocol was used"

    The code itself is pretty basic. Not pretty since I have been debugging, but essentially looks like:

    Debug.Log("RabbitMQ Initialization Started");
    var connectionFactory = new ConnectionFactory();
    connectionFactory.HostName = "192.168.10.20";

    Debug.Log("connectionFactory Created");

    IConnection connection = null;
    try
    {
    connection = connectionFactory.CreateConnection();
    }
    catch (Exception e)
    {
    Debug.Log("connection - Error has Occured");
    Debug.Log("Exception: " + e.ToString());
    Debug.Log("Stack Trace: " + e.StackTrace.ToString());
    Debug.Log("Message: " + e.Message);
    Debug.Log("Inner Exception: " + e.InnerException.ToString()); ;
    }

    Debug.Log("connection Created");

    IModel channel = null;
    try
    {
    Debug.Log("Attempting to create channel");
    channel = connection.CreateModel();
    }
    catch (Exception e)
    {
    Debug.Log("Channel - Error has Occured");
    Debug.Log(e.ToString());
    }


    Has anyone succesfully gotten RabbitMQ to work with Unity, or am I missing something here on these errors?

    Thanks in advance

    Chris Eisnaugle
     
  2. CarpeDiem_17

    CarpeDiem_17

    Joined:
    Jan 24, 2013
    Posts:
    3
    I am trying to do the same currently !

    My code is similar, and it throws the same error.

    Did you figure it out ?

    I am trying different things with the formatting of the address..but nothing workin for now.

    Benjamin
     
  3. CarpeDiem_17

    CarpeDiem_17

    Joined:
    Jan 24, 2013
    Posts:
    3
    I succeeded in having RabbitMQ working in Unity3D.

    In Unity, the use of the DLL was causing trouble, so I downloaded the source files in C# and put them in my Unity Project.

    After reading some documentation, I saw that the error "Inner Exception: System.Net.Sockets.SocketException: An address incompatible with the requested protocol was used" could be that the default Internet Protocol is IPV6 whereas it has to be IPV4.

    Setting the ConnectionFactory AddressFamily to InterNetwork was not working for me. So in the ConnectionFactory class, I modified the code to have the InterNetwork set up while creating a TCP Client.

    ----------------
    In ConnectionFactory.cs
    public static TcpClient DefaultSocketFactory(AddressFamily addressFamily)
    {
    // TcpClient tcpClient = new TcpClient(addressFamily);
    // tcpClient.NoDelay = true;
    // return tcpClient;

    TcpClient tcpClient = new TcpClient(AddressFamily.InterNetwork);
    tcpClient.NoDelay = true;
    return tcpClient;
    }
     
  4. mikhail.d

    mikhail.d

    Joined:
    Jun 7, 2013
    Posts:
    1
    Hi CarpeDiem_17,
    Thanks a lot for posting the solution - it saved a lot of time for me.

    However, you could have done this in a more sophisticated way. You actually do not need to modify the source code.
    Instead, you can define your own Custom Socket Factory and specify the
    Code (csharp):
    1.  
    2. ConnectionFactory.SocketFactory
    3.  
    Property. Here's the instructions:
    1. Put the RabbitMQ.Client.dll into your Assets folder (I created a separate folder for RabbitMQ inside Assets)
    2. Create a class and do not inherit it from the MonoBehavior. Here's my code:
    Code (csharp):
    1.  
    2. using System;
    3. using System.Collections.Generic;
    4. using System.Linq;
    5. using System.Text;
    6.  
    7. public static class CustomSocketFactory
    8. {
    9.     public static System.Net.Sockets.TcpClient GetSocket(System.Net.Sockets.AddressFamily addressFamily)
    10.     {
    11.         System.Net.Sockets.TcpClient tcpClient = new System.Net.Sockets.TcpClient(System.Net.Sockets.AddressFamily.InterNetwork);
    12.         tcpClient.NoDelay = true;
    13.         return tcpClient;
    14.     }
    15. }
    16.  
    17.  
    3. In your Unity Script (the one that is supposed to receive messages) when you create your ConnectionFactory specify the following:
    Code (csharp):
    1.  
    2.  
    3. ConnectionFactory cf = new ConnectionFactory();
    4. cf.SocketFactory = new ConnectionFactory.ObtainSocket(CustomSocketFactory.GetSocket);
    5. //and you can specify other properties as necessary
    6.  
    By doing this you provide a custom factory for creating the TcpClient. It's not the best implementation of the Factory pattern but it's better than modifying library source code for obvious reasons.

    I hope this helps.
     
  5. HybridCG

    HybridCG

    Joined:
    Sep 6, 2013
    Posts:
    1
    Does anybody have problem when he builds the project although it works fine via unity? Thanks!
     
  6. Oscar Sweatman

    Oscar Sweatman

    Joined:
    Jun 18, 2013
    Posts:
    2
    CarpeDiem_17,

    I'm trying to compile RabbitMQ within Unity, but I'm getting a slew of compiler errors from MonoDevelop and Unity. I pulled my source code from here:

    http://www.rabbitmq.com/dotnet.html

    Is this where your source came from as well? Could you give a little more detail on your process for pulling the RabbitMQ source into Unity?
     
  7. Oscar Sweatman

    Oscar Sweatman

    Joined:
    Jun 18, 2013
    Posts:
    2
    HybridCG,

    I was having an issue with RabbitMQ working after building. My solution was to change the "Api Compatibility Level" to just ".NET 2.0" instead of ".NET 2.0 Subset". You can find the option under:

    Edit->Project Settings->Player->Other Settings->Optimization->Api Compatibility Level

    Not sure why this fixed my issue, but I was only able to successfully use RabbitMQ while running within Unity until I changed this option to fix my builds. Hope that helps.
     
  8. ajpessoa

    ajpessoa

    Joined:
    Dec 11, 2013
    Posts:
    3
    I'm having the same issue but I'm trying to build my project for web player and it won't let me change api compatibility there.
    It just gives me an error saying "The Assembly System.ServiceModel is referenced by RabbitMQ.Client. But the dll is not allowed to be included or could not be found." and "Extracting referenced dlls failed" Any ideas how I can solve this?
     
  9. jsashikala

    jsashikala

    Joined:
    Jan 21, 2016
    Posts:
    1
    Have you got RabbitMQ working with Unity? I'm using Unity 3d 5.3.4 and did everything mentioned in the previous posts. But unable to connect to rabbitmq as i always get the error unknown bindings or
    Exception: JNI: Init'd AndroidJavaClass with null ptr!
    UnityEngine.AndroidJavaClass..ctor (IntPtr jclass) (at C:/buildslave/unity/build/Runtime/Export/AndroidJavaImpl.cs:556)
    UnityEngine.AndroidJavaObject.get_JavaLangClass () (at C:/buildslave/unity/build/Runtime/Export/AndroidJavaImpl.cs:534)
    UnityEngine.AndroidJavaObject.FindClass (System.String name) (at C:/buildslave/unity/build/Runtime/Export/AndroidJavaImpl.cs:525)
    UnityEngine.AndroidJavaObject._AndroidJavaObject (System.String className, System.Object[] args) (at C:/buildslave/unity/build/Runtime/Export/AndroidJavaImpl.cs:127)
    UnityEngine.AndroidJavaObject..ctor (System.String className, System.Object[] args) (at C:/buildslave/unity/build/artifacts/generated/common/runtime/AndroidJavaBindings.gen.cs:18)

    Any help is highly appreciated.
     
  10. ajpessoa

    ajpessoa

    Joined:
    Dec 11, 2013
    Posts:
    3
    I am continuing a project started by someone else, and I have no idea how to user RabbitMQ or how he got it to work in the first place. The thing is I was having problems with the webplayer sandbox and so I eventually gave up on exporting my application as a webplayer game and ended up just using the standalone build. I'm sorry I can't be of much help here...
     
  11. FamerJoe

    FamerJoe

    Joined:
    Dec 21, 2013
    Posts:
    193
    I was having this issue; finally figured out the reasoning: Mono debugger interrupts connecting to rabbitMQ for whatever reason.
     
  12. meverett

    meverett

    Joined:
    Sep 9, 2005
    Posts:
    34
    I have begun work on a project that aims to create a very clean and easy integration of AMQP/RabbitMQ into Unity. I found this forum thread through a search when attempting to see if there were any other straight-forward AMQP integrations for Unity, specifically RabbitMQ. I definitely found some helpful posts and hints like this forum thread, but nothing that worked out-of-the-box.

    I've got a simple Unity asset package that can be imported into your projects to get up and running quickly. It's very new, but I will continue to update it as I use it in my own projects. If it reaches a critical mass, I will probably publish it to the asset store.

    I don't have great documentation yet, but feel free to monitor progress here: https://github.com/CymaticLabs/Unity3D.Amqp
     
  13. AminAghajoon

    AminAghajoon

    Joined:
    May 17, 2013
    Posts:
    20
    For anybody who reaches to this page, Currently with Unity 2019.1 and final version of Rabbitmq dotnet client (5.1.0) which requires dotnet standard > 1.5, everything works fine.
     
    jcorferplexus likes this.