Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

[Unity & Rasa Chatbot AI] How to connect to a local host?

Discussion in 'Multiplayer' started by Deleted User, Dec 7, 2019.

  1. Deleted User

    Deleted User

    Guest

    Hello everyone,

    first of all, I've some limited understanding of networking so please bare with me. I want to implement a Chatbot AI into one of my Unity Projects, I'm using Rasa as it's Open Source and I can use it locally.

    I can start with Rasa a Server on my localhost, the server starts up and I can see the a message from Rasa on "http://localhost:5005". That's fine, but how do I connect now with Unity to this server the right way?

    I'm using Unity 2019.2 14f1. If I run my Code below, I get for the "error" variable "0", which means everything is good but my Debug Log don't put out "We have connected". After a while the Debug Log just prints "We have been disconnected", which probably means there was a timeout or something went terrible wrong?

    Also, this is some additional help if someone by any chance knows a way, if I get the connection how do I receive the string of the Chatbot? And how do I send a string to this Chatbot?

    Thanks in advance!

    PS: UnityEngine.Networking seems to be outdated which one should I use? Because after the documentation I should use UnityEngine.__Networking__ which don't seem to be exist.

    Based on some tutorials, this is my code:

    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine;
    3. using UnityEngine.Networking;
    4.  
    5. public class NetworkManager : MonoBehaviour
    6. {
    7.     NetworkClient myClient;
    8.     private const string SERVER_IP = "127.0.0.1"; // 127.0.0.1
    9.     private const int PORT = 5005;
    10.     private const int BYTE_SIZE = 1024;
    11.  
    12.     private int hostID;
    13.     private byte error;
    14.     private byte reliableChannel;
    15.     private bool isStarted;
    16.  
    17.     // Start is called before the first frame update
    18.     void Start()
    19.     {
    20.         NetworkTransport.Init();
    21.         ConnectionConfig cc = new ConnectionConfig();
    22.         reliableChannel = cc.AddChannel(QosType.Reliable);
    23.         HostTopology topo = new HostTopology(cc, 1);
    24.  
    25.         hostID = NetworkTransport.AddHost(topo, 0);
    26.         NetworkTransport.Connect(hostID, SERVER_IP, PORT, 0, out error); // error is number!
    27.         Debug.Log(error);
    28.         isStarted = true;
    29.     }
    30.  
    31.     void Update()
    32.     {
    33.         UpdateMessagePump();
    34.     }
    35.  
    36.     public void UpdateMessagePump()
    37.     {
    38.         if (!isStarted)
    39.             return;
    40.  
    41.         int recHostID; // Is this from Web or Standalone?
    42.         int connectionID; // Which user is sending me this?
    43.         int channelID; // Which lane is he sending that message from?
    44.  
    45.         byte[] recBuffer = new byte[BYTE_SIZE];
    46.         int dataSize;
    47.  
    48.         NetworkEventType type = NetworkTransport.Receive(out recHostID, out connectionID, out channelID, recBuffer, BYTE_SIZE, out dataSize, out error);
    49.         switch (type)
    50.         {
    51.             case NetworkEventType.Nothing:
    52.                 break;
    53.             case NetworkEventType.ConnectEvent:
    54.                 Debug.Log("We have connected");
    55.                 break;
    56.             case NetworkEventType.DisconnectEvent:
    57.                 Debug.Log("We have been disconnected");
    58.                 break;
    59.             default:
    60.             case NetworkEventType.BroadcastEvent:
    61.                 Debug.Log("Broadcast");
    62.                 break;
    63.             case NetworkEventType.DataEvent:
    64.                 Debug.Log("Data");
    65.                 break;
    66.         }
    67.     }
    68. }