Search Unity

  1. We have added dedicated sub-forums for UNet, the experimental MLAPI and Unity Transport Package. Please use the new forums to discuss related topics.
    Dismiss Notice
  2. How can our website serve you better? Give us your feedback. Take our survey and let us know.
    Dismiss Notice

MLAPI Dedicated Server/Client

Discussion in 'MLAPI' started by piggybank1974, Mar 27, 2021.

  1. piggybank1974

    piggybank1974

    Joined:
    Dec 15, 2015
    Posts:
    607
    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. JWLewis777

    JWLewis777

    Joined:
    May 17, 2015
    Posts:
    145
    Dang, I ran into the same issue today
    Yeah, something is definitely wrong. I can get a client to connect to a server, but I can't get it working trying to handle the player spawning myself.
    Also I can't get the RPCs working.

    I start the server which spawns a server gameobject with networkobject and my own server script containing ServerRPCs and ClientRPCs
    When I try to join this server with a client, using NetworkManagers Create Player Prefab ON and ConnectionApproval Off.

    My player spawning and chat system works great in client/host setup, but not a client/server setup.
     
  3. piggybank1974

    piggybank1974

    Joined:
    Dec 15, 2015
    Posts:
    607
    @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.
     
  4. JWLewis777

    JWLewis777

    Joined:
    May 17, 2015
    Posts:
    145
    After spending almost 2 days on this, I finally got it working, thanks to some fellas on the discord making sure I stayed on track and giving some advice.

    I was able to start the server, clone my server prefab as a a network object using connectionapproval, with the same network script I use on my player prefabs and network object, just without any models, network transform etc. I instantiated the server game object, then spawned it using the following code...

    goServer = Instantiate(Resources.Load("Prefabs/Server") as GameObject);
    goServer.GetComponent<NetworkObject>().Spawn();

    This started the server without issues. I had to make sure the server prefab is in the networkmanager prefab list, the connection approval box is ticked and the Create Player Prefab box is unticked (Since I am manually spawning the objects).

    After doing this, I was able to get a client to join this server, send chat messages back and forth and watch the player move on my dedicated server.

    It was a beatiful day.

    Also, as a side note, I am using the same project for client, host and server. I am not splitting the projects.
     
  5. piggybank1974

    piggybank1974

    Joined:
    Dec 15, 2015
    Posts:
    607
    @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.
     
  6. JWLewis777

    JWLewis777

    Joined:
    May 17, 2015
    Posts:
    145
    Yeah, according to Luke from Discord...

    Luke — 03/23/2021
    Right now the easiest way to setup MLAPI is to have the server and client in the same project/scene.

    https://discord.com/channels/449263083769036810/563033158480691211/823963494394888242

    Not sure why but probably because of the issue your having by separating the projects. I believe I ran into this issue just by having different scripts trying to talk to each other between server and client. They wont communicate using RPC's either. Or I may have gotten a wiff of my wifeys magic cigarette. Not sure which one it is.
     
  7. PutridEx

    PutridEx

    Joined:
    Feb 3, 2021
    Posts:
    225
    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.
  8. piggybank1974

    piggybank1974

    Joined:
    Dec 15, 2015
    Posts:
    607
    @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
     
unityunity