- Home /
Unity uNet Connection Problems
I have another window open running the same game so a server has be created and I know this because the registered sign has turned. After the client finds the HostList it connects to the first open server... But in this case it times out. Please Help.
using UnityEngine; using System.Collections; using UnityEngine.UI; using UnityEngine.Networking.NetworkSystem; using UnityEngine.Networking;
public class network_manager : MonoBehaviour {
string registeredGameName = "RockPaperScissorsOnline_randomOpponent";
public static HostData[] hostData;
int refreshRequestLength = 3;
public GameObject connecting;
public GameObject controller;
public Text counter;
public Text server;
public Text creating;
public Text created;
public Text connecttext;
mm_timer time;
int timetext = 0;
public static int i = 0;
NetworkClient my_client;
void Start()
{
server.text = ("");
creating.text = ("");
created.text = ("");
connecttext.text = ("");
time = new mm_timer(1f, 0f, true);
controller.SetActive(false);
connecting.SetActive(true);
StartCoroutine("RefreshHostList");
}
void Connect()
{
string[] tmpip;
int[] tmpport;
tmpip = new string[hostData.Length];
tmpport = new int[hostData.Length];
int z = 0;
my_client = new NetworkClient();
foreach (HostData element in hostData)
{
while(z < element.ip.Length)
{
tmpport[z] = element.port;
tmpip[z] = element.ip[z];
z++;
}
}
if (i == 0)
{
connecttext.text = ("Connecting to Server #" + 1);
Debug.Log("" + tmpip[i]);
Debug.Log("" + tmpport[i]);
my_client.RegisterHandler(Inputs.inputs, Oninputs);
my_client.RegisterHandler(Username.users, Onusername);
my_client.Connect(tmpip[i], tmpport[i]);
}
else if (i > 0)
{
int j = i++;
connecttext.text = ("Connecting to Server #" + j);
Debug.Log("" + tmpip[i]);
Debug.Log("" + tmpport[i]);
my_client.RegisterHandler(Inputs.inputs, Oninputs);
my_client.RegisterHandler(Username.users, Onusername);
my_client.Connect(tmpip[i], tmpport[i]);
}
}
public void OnConnected(NetworkMessage netMsg)
{
Debug.Log("Connected to server");
}
void OnFailedToConnect(NetworkConnectionError error)
{
i++;
Connect();
Debug.Log("Failed");
}
private void StartServer()
{
creating.text = ("Creating Server...");
Network.InitializeServer(2, 25002, false);
MasterServer.RegisterHost(registeredGameName, "Random Opponent", menu_keepusername.username);
NetworkServer.RegisterHandler(Inputs.inputs, Oninputs);
NetworkServer.RegisterHandler(Username.users, Onusername);
}
void OnMasterServerEvent(MasterServerEvent masterServerEvent)
{
if (masterServerEvent == MasterServerEvent.RegistrationSucceeded)
{
created.text = ("Registered Under Player " + menu_keepusername.username + ". Waiting for Other Player...");
}
}
public IEnumerator RefreshHostList()
{
MasterServer.RequestHostList(registeredGameName);
float timeEnd = Time.time + refreshRequestLength;
while (Time.time < timeEnd)
{
hostData = MasterServer.PollHostList();
yield return new WaitForEndOfFrame();
}
if (hostData == null || hostData.Length == 0)
{
StartServer();
}
else
{
Connect();
}
}
void Update()
{
server.text = (hostData.Length + " Servers Found.");
counter.text = ("seconds waited: " + timetext);
if (time.returntik())
{
timetext += 1;
}
if (Network.isClient || Network.isServer)
{
if (Network.connections.Length > 0)
{
controller.SetActive(true);
connecting.SetActive(false);
SendUsername(menu_keepusername.username);
}
}
}
Your answer
Follow this Question
Related Questions
Stun and turn server possible with Unity Netowking? 0 Answers
NetworkServer.Destroy(GameObject) disables Client objects instead of destroying them 1 Answer
Using SyncListString/Synchronizing lists 0 Answers
This is giving an error for the other client and cannot hit other client. 1 Answer
Animation over network using rpcs 0 Answers