Search Unity

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

MLAPI Dedicated Server/Client

Discussion in 'Netcode for GameObjects' started by piggybank1974, Mar 27, 2021.

  1. piggybank1974

    piggybank1974

    Joined:
    Dec 15, 2015
    Posts:
    621
    I've been trying to get my head around the new MLAPI, but I can't figure out why my Client-side will not connect to my dedicated server.

    Here what I have Server:

    Connection Address: 127.0.0.1
    Server Listening Port: 1974

    Code (CSharp):
    1.      
    2. if (MLAPI.NetworkManager.Singleton.IsListening == false)
    3.   {
    4.    UNetTransport mTransport = MLAPI.NetworkManager.Singleton.GetComponent<UNetTransport>();
    5.      if (mTransport != null)
    6.        {
    7.          mTransport.ConnectAddress = this.TxtIPAddress.text;
    8.          mTransport.ServerListenPort = Convert.ToInt32(this.TxtPort.text);
    9.          MLAPI.NetworkManager.Singleton.StartServer();
    10.  
    11.          if (MLAPI.NetworkManager.Singleton.IsListening == true)
    12.            {
    13.             ColorBlock mColours = BtnServer.colors;
    14.             mColours.normalColor = Color.red;
    15.             BtnServer.colors = mColours;
    16.             BtnServer.GetComponentInChildren<TMP_Text>().text = "stop";
    17.            }
    18.            Debug.LogWarning("Server Is Listening");
    19.         }
    20.   }
    21. else
    22.       {
    23.        ColorBlock mColours = BtnServer.colors;
    24.        mColours.normalColor = Color.green;
    25.        BtnServer.colors = mColours;
    26.        BtnServer.GetComponentInChildren<TMP_Text>().text = "start";
    27.        MLAPI.NetworkManager.Singleton.StopServer();
    28.       }
    29.  
    Now I did open up a Command Prompt and run netstat eg. "netstat -a -n -o" and I found it was UDP 0.0.0.0 and 1974 which is my port number. normally you don't initially connect to a UDP socket you hand it over from TCP, but I understand this is different.

    Here is my Client:

    Connection Address: 127.0.0.1
    Connection Port: 1974

    Code (CSharp):
    1.      
    2. if (MLAPI.NetworkManager.Singleton.IsConnectedClient == false)
    3.   {
    4.    UNetTransport mTransport = MLAPI.NetworkManager.Singleton.GetComponent<UNetTransport>();
    5.     if (mTransport != null)
    6.       {
    7.         mTransport.ConnectAddress = this.TxtIPAddress.text;
    8.         mTransport.ConnectPort = Convert.ToInt32(this.TxtPort.text);
    9.         MLAPI.NetworkManager.Singleton.StartClient();
    10.  
    11.         if (MLAPI.NetworkManager.Singleton.IsConnectedClient == true)
    12.           {
    13.             ColorBlock mColours = BtnClient.colors;
    14.             mColours.normalColor = Color.red;
    15.             BtnClient.colors = mColours;
    16.             BtnClient.GetComponentInChildren<TMP_Text>().text = "stop";
    17.           }
    18.       }
    19.   }
    20. else
    21.       {
    22.        ColorBlock mColours = BtnClient.colors;
    23.         mColours.normalColor = Color.green;
    24.         BtnClient.colors = mColours;
    25.         BtnClient.GetComponentInChildren<TMP_Text>().text = "start";
    26.         MLAPI.NetworkManager.Singleton.StopClient();
    27.        }
    28.  
    I did try an override the OnClientConnectedCallback event, but I got nothing, as far as I can see UNetTransport which is the ONLY transport in the MLAPI that is available is UDP so it's connectionless?

    any advice in pointing me in the correct direction would be great cheers.
     
    Last edited: Mar 27, 2021
  2. piggybank1974

    piggybank1974

    Joined:
    Dec 15, 2015
    Posts:
    621
    @JWLewis777
    @CreativeChris

    I'm glad it's not just me
    are you like myself e.g I have two separate programs eg. a dedicated server and client? the hello word example works fine, personally, I'm not sure it can work separately and I think I can tell you why the Player Prefab ID which all object get when you create an object will be different on the two programs, I also would have thought copying the prefab from one to the other would produce a different ID, WE might be able, to work around this by editing the code in the assets folder that the Prefab was generated from and giving them both SAME id number, but again that's a hacky idea, and might not work, you should not need to do this, at the very least the MLAPI developer needs to clarify if WE are doing anything wrong on this I'm adding "CreativeChris" in this so we might get an answer. I'm not even started designing my new game idea until I can get the answers on this, does mirror have the same issue I wonder.
     
  3. piggybank1974

    piggybank1974

    Joined:
    Dec 15, 2015
    Posts:
    621
    @JWLewis777

    thanks for your reply.

    I think I'll try discord tonight, and see if I can get the answers a want, I have done sockets in 2008, and again in 2014 for work opposes, but if I can get away with it, I would rather go down this route.
     
  4. PutridEx

    PutridEx

    Joined:
    Feb 3, 2021
    Posts:
    1,133
    MLAPI is in really early stages, your probably better of using one of the more popular third-party multiplayer solutions.
    (Photon, Mirror)
     
    mmoprogramming likes this.
  5. piggybank1974

    piggybank1974

    Joined:
    Dec 15, 2015
    Posts:
    621
    @JWLewis777
    @PutridEx

    As PutridEx posted back I meet him in the discord last night, so thanks for taking the time to reply, it's greatly appreciated.

    I might need to explain more so you can base you answer on more details, I already have the single player game working on the goggle store since late 2016, I do get asked about making a tournament version, but at the time I was very new to unity. the past year I'm been trying to figure out a project I wanted to do, and not give up on half or more way though, I did think about a shmups "a question on the forum I post about it" and got a basic prototype working, but was not sure that was the way to go for me I'm a programmer not an artist and 20+ years I've been looking to work with one I've almost given up sadly I've done everything to find one, even asking unity for help "a coder and artist branch" but to no avail, but that's another story.

    The game will come in two flavours "I'm thinking ice cream", no back to it, a local networked version, and then a online version, these are basically the same it depends on the connection(and opening a port on the router), but I wanted to run before I can walk here.

    After looking at Photon can it be locally networked, I'm not sure? now Mirror can do both but can Mirror be a dedicated server, and a dedicated client in separate projects?

    Or I'm I coming at this at the wrong angle/thought process. Like I said writing a Server/Client is not for the faint hearted but it can be done, I know same of the pitfalls, as I've done it many moons ago, but I would like to get it finished in this decade if I can.

    cheers again