Author Topic: Call functions over server.  (Read 4238 times)

Trollvahkiin

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 6
    • View Profile
Call functions over server.
« on: December 02, 2014, 05:07:02 PM »
Hey, so I'm really new to TNet and I'm have some real trouble grasping how everything works haha. I made a really simple project to practice in which all that happens is that you can start a sever or join a server. When you do I simply want the console to say "Player joined server" or something. This is the scripts:

Main menu script:
  1. using UnityEngine;
  2. using TNet;
  3. using System.IO;
  4. using System.Collections;
  5. using UnityTools = TNet.UnityTools;
  6. using UnityEngine.UI;
  7.  
  8. public class Network_Manager : MonoBehaviour {
  9.  
  10.     public string SPSceneName;
  11.  
  12.     public string inputIP;
  13.  
  14.     public bool connected;
  15.  
  16.     public Text ipTextField;
  17.  
  18.     public int serverTcpPort = 5127;
  19.  
  20.     void Update()
  21.     {
  22.         inputIP = ipTextField.text;
  23.     }
  24.  
  25.     void Start()
  26.     {
  27.         ipTextField.text = Tools.externalAddress.ToString();
  28.  
  29.         if (Application.isPlaying)
  30.         {
  31.             // Make it possible to use UDP using a random port
  32.             TNManager.StartUDP(Random.Range(10000, 50000));
  33.         }
  34.     }
  35.  
  36.     public void Quit_Button()
  37.     {
  38.         Application.Quit();
  39.     }
  40.  
  41.     public void Create_Server()
  42.     {
  43.         int udpPort = Random.Range(10000, 40000);
  44.  
  45.         TNServerInstance.Start(serverTcpPort, udpPort);
  46.         if (TNManager.isHosting)
  47.         {
  48.             print("Hosting server.");
  49.             TNManager.LoadLevel("Game");
  50.         }
  51.     }
  52.  
  53.     public void Join_Server()
  54.     {
  55.         TNManager.Connect(inputIP);
  56.         if (TNManager.isTryingToConnect)
  57.         {
  58.             print("Trying to connect...");
  59.         }
  60.     }
  61.  
  62.     void OnNetworkConnect(bool success, string message)
  63.     {
  64.         if (success)
  65.         {
  66.             TNManager.JoinChannel(123, SPSceneName);
  67.             print("Connected!");
  68.             success = true;
  69.         }
  70.         else
  71.         {
  72.             success = false;
  73.             print(message);
  74.         }
  75.     }
  76. }
  77.  

and this is the game scene script:
  1. using UnityEngine;
  2. using System.Collections;
  3. using TNet;
  4.  
  5. public class PlayerJoin : TNBehaviour
  6. {
  7.  
  8.     private void Start()
  9.     {
  10.         tno.Send("PlayerJoin", Target.All);
  11.     }
  12.  
  13.     [RFC]
  14.     public void PlayerJoin()
  15.     {
  16.         Debug.Log("Player joined game");
  17.     }
  18. }

Unfortunately none of this works, Any idea why?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Call functions over server.
« Reply #1 on: December 04, 2014, 04:36:29 AM »
Quote
TNServerInstance.Start(serverTcpPort, udpPort);
if (TNManager.isHosting)
This will always be wrong. Starting a server and connecting to it are two different actions. TNManager.isHosting will be 'true' in offline mode (which you are in at this point). TNManager.isHosting will be 'true' only on one connected client when actually connected.

You need to check this value only after establishing a connection.

Same goes for TNManager.LoadLevel call right below that. Wait for connection, request to join a channel, wait for the channel joined message, and only then check for TNManager.isHosting and do a LoadLevel.

Trollvahkiin

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 6
    • View Profile
Re: Call functions over server.
« Reply #2 on: December 05, 2014, 04:17:30 PM »
Hey, I'm having some trouble understating this. Why would TNManager.isHosting be true when offline? Would it not make more sense if it was false. What's the simplest way of starting and join a server in which both players can communicate? Thanks a lot!  :)

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Call functions over server.
« Reply #3 on: December 05, 2014, 04:25:13 PM »
For offline gaming. When you want to test the game in single player without connecting to the server, TNet will behave as if you were connected, and happen to be the only one in the channel. TNManager.isConnected will be false, but TNManager.isHosting will be true.

Trollvahkiin

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 6
    • View Profile
Re: Call functions over server.
« Reply #4 on: December 05, 2014, 05:39:20 PM »
Ah great! I'm making progress here. Although no I've encountered an error "UPnP discovery failed. TNet won't be able to open ports automatically." I've port-forwarded TCP-5127 to 5127 and UDP-5129 to 5129 and still nothing. Also when I try running the external sever I get a gateway not found:



ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Call functions over server.
« Reply #5 on: December 07, 2014, 12:29:12 AM »
UPnP discovery will fail if your router doesn't support UPnP or has it disabled. Manually setting up port forwarding is the solution. Any difficulty in connectivity beyond that is on your end. All TNet does is listens to a port -- in your case 5127 -- for incoming connections. You would have the same issues if you ran any other kind of a server, such as a web server or an IRC server.

You can quickly test connectivity by opening up your browser and entering your IP in the address bar along with the port, such as XXX.XXX.XXX.218:5127

You will get "no data received" if it's accessible and something else if it's not.