Author Topic: [Solved] Proper way to start and join a server?  (Read 5516 times)

Thurinus

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 27
    • View Profile
[Solved] Proper way to start and join a server?
« on: November 17, 2014, 05:47:40 AM »
Hi all,

I'm new to TNet, and relatively new to realtime networking in general (coming in from SmartFox). I'm trying to figure out how to properly execute a certain user flow in TNet.

I have a publicly accessible lobby server. Users are expected to either join an existing game server by connecting to a server found on this lobby, or host their own game servers and registering it with the lobby server. I want those that host their own game servers to automatically join their own servers upon creating them. Right now I call TNManager.Connect("127.0.0.1", tcpPort); in a coroutine after waiting a second from a TNServerInstance.Start() call. Calling TNManager.Connect() right after in the same function doesn't seem to work... or at least OnNetworkConnect() doesn't seem to be invoked when done that way.

2 immediate questions from this exercise:
  • Is calling TNManager.Connect() on 127.0.0.1 this the proper way to go about forcing a host to join his own server?
  • Having to call TNManager.Connect() after a coroutine wait feels a bit hackish... is there a more graceful way to go about this without requiring user input?

Thanks for any help!
« Last Edit: November 19, 2014, 04:18:26 AM by Thurinus »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Proper way to start and join a server?
« Reply #1 on: November 17, 2014, 08:48:01 AM »
TNManager.Connect is a delayed call. It starts the connection process. When the result is known, you get the "OnNetworkConnect" notification. Yes, 127.0.0.1 will connect the computer to itself.

TNServerInstance.Start() is an immediate function. It's not delayed. It will return either 'true' or 'false' based on whether the operation succeeds. You can call TNManager.Connect on the very next line, no wait time needed.

Thurinus

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 27
    • View Profile
Re: Proper way to start and join a server?
« Reply #2 on: November 18, 2014, 12:13:17 AM »
Thanks for responding Aren!

I'm still not having luck with calling TNManager.Connect() directly after a TNServerInstance.Start(), so I may be missing something else that's important and that I failed to mention. Here's the component written for this:

  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Net;
  4. using TNet;
  5.  
  6. public class NetworkManager : MonoBehaviour {
  7.  
  8.         public string lobbyUrl = "<my lobby's public IP address here>";
  9.         public int lobbyPort = 5129;
  10.         public TNServerInstance.Type lobbyType = TNServerInstance.Type.Tcp;
  11.  
  12.         public int tcpPort = 6127;
  13.  
  14.         void Awake ()
  15.         {
  16.                 TNLobbyClient.onChange += OnListChange;
  17.         }
  18.  
  19.         void Start () {
  20.                 StartServer();
  21.         }
  22.  
  23.         /// <summary>
  24.         /// Starts a server and registers it to the remote lobby.
  25.         /// </summary>
  26.         public void StartServer()
  27.         {
  28.                 int udpPort = Random.Range(10000,40000);
  29.                 // start a server and register it to the remote lobby
  30.                 Debug.Log("Starting Server...");
  31.                 bool didServerStart = TNServerInstance.Start(tcpPort, udpPort, null, lobbyType, Tools.ResolveEndPoint(lobbyUrl, lobbyPort));
  32.  
  33.                 // connect to the local server that was just started
  34.                 if(didServerStart)
  35.                 {
  36.                         Debug.Log("Server started! Attempting to connect to server");
  37.                         TNManager.Connect("127.0.0.1", tcpPort);
  38.                 }
  39.         }
  40.  
  41.         private void OnListChange()
  42.         {
  43.                 Debug.Log("Server list change detected.");
  44.         }
  45.  
  46.         public void OnNetworkConnect(bool success, string message)
  47.         {
  48.                 Debug.Log("Was connection successful? " + success + " what's the accompanying message? " + message);
  49.         }
  50. }
  51.  

The GameObject hosting this component also has TNTcpLobbyClient and TNManager attached, so it should hit OnNetworkConnect once it connects. OnNetworkConnect doesn't seem to get hit however, according to the attached screenshot of the resulting console log. If it matters, I started the Lobby server with these parameters:

TNServer -name "My Server" -tcpLobby 5129

Am I missing something in this setup that's causing it to not invoke OnNetworkConnect?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Proper way to start and join a server?
« Reply #3 on: November 18, 2014, 04:52:53 AM »
I am not seeing anything wrong. Have a look at the TNAutoJoin script. It connects to a remote server within. This is what my own StartServer() function looks like in Windward:
  1.         public bool StartServer ()
  2.         {
  3.                 if (!TNServerInstance.Start(5127, 0, serverFilePath)) return false;
  4.                 TNManager.Connect("127.0.0.1:5127");
  5.                 return true;
  6.         }
As you can see there is nothing to it. It starts the server and the immediately connects to it. When something doesn't work, keep simplifying it until it does. Comment out the part that uses the lobby, remove UDP, etc.

Thurinus

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 27
    • View Profile
Re: Proper way to start and join a server?
« Reply #4 on: November 19, 2014, 04:18:03 AM »
Just got home today to test it again, and it works without any changes. Very peculiar.... but I won't complain. Marking this resolved for now. Thanks very much for the help!