Now my question is: As I do to create a list of servers, I have to start several hosts for example. What I want to say is I need to create a List of Servers using the Network Discovery component for a game on LAN
Here how you can do network discovery
public class CustomNetworkDiscovery : NetworkDiscovery
{
public static CustomNetworkDiscovery Instance;
#region Events
public delegate void ServerDetectedEventHandler(string fromaddress, string data);
public event ServerDetectedEventHandler ServerDetected;
#endregion /Events
#region Event_Invokator
private void OnServerDetected(string address, string data)
{
var handler = ServerDetected;
if (handler != null)
{
handler.Invoke(address, data);
}
}
#endregion /Event_Invokator
#region Unity_Methods
private void Awake()
{
if (Instance == null)
{
Instance = this;
}
else
{
Destroy(gameObject);
}
}
#endregion /Unity_Methods
#region NetworkDiscovery_Methods
public override void OnReceivedBroadcast(string fromAddress, string data)
{
//Don't Call base class intentionally for handle broadcast manualy.
//base.OnReceivedBroadcast(fromAddress, data);
OnServerDetected(fromAddress.Split(':')[3], data);
}
#endregion /NetworkDiscovery_Methods
#region CustomDiscoveryMethodsCallbacks
public bool InitializeNetworkDiscovery()
{
return Initialize();
}
/// <summary>
/// Starts the server broadcasting.
/// Called by a Host only.
/// </summary>
public void StartServerBroadcasting()
{
//Debug.Log("########## StartBroadCasting As Server...");
InitializeNetworkDiscovery();
StartAsServer();
}
/// <summary>
/// Starts the client braodcast.
/// Used to join Game.
/// </summary>
public void StartClientBraodcast()
{
//Debug.Log("########## Listen Broadcast As Client...");
InitializeNetworkDiscovery();
StartAsClient();
}
public void StopBroadcasting()
{
if (running)
{
//Debug.Log("########## Stop Broadcasting...");
StopBroadcast();
}
}
public void SetBroadcastData(string broadcastPayload)
{
broadcastData = broadcastPayload;
}
#endregion /CustomDiscoveryMethodsCallbacks
}
I’m desperate…
This is my Network Discovery code :
public class DisNetwork : NetworkDiscovery
{
private bool end = false;
public override void OnReceivedBroadcast(string fromAddress, string data)
{
base.OnReceivedBroadcast(fromAddress, data);
Debug.Log("Broadcast recieved from" + fromAddress + " data:" + data);
//NetworkManager.singleton.networkAddress = fromAddress;
// NetworkManager.singleton.StartClient();
end = true;
}
private void LateUpdate()
{
if (end)
{
StopBroadcast();
end = false;
}
}
this is my server list code : @storybelldev
public class LobbyServerList : MonoBehaviour
{
public LobbyManager lobbyManager;
CustomNetworkDiscovery custom;
public RectTransform serverListRect;
public GameObject serverEntryPrefab;
public GameObject noServerFound;
protected int currentPage = 0;
protected int previousPage = 0;
static Color OddServerColor = new Color(1.0f, 1.0f, 1.0f, 1.0f);
static Color EvenServerColor = new Color(.94f, .94f, .94f, 1.0f);
private void Start()
{
FindObjectOfType<CustomNetworkDiscovery>();
}
void OnEnable()
{
currentPage = 0;
previousPage = 0;
foreach (Transform t in serverListRect)
Destroy(t.gameObject);
noServerFound.SetActive(false);
RequestPage(0);
}
public void OnGUIMatchList(bool success, string extendedInfo, List<MatchInfoSnapshot> matches)
{
if (matches.Count == 0)
{
if (currentPage == 0)
{
noServerFound.SetActive(true);
}
currentPage = previousPage;
return;
}
noServerFound.SetActive(false);
foreach (Transform t in serverListRect)
Destroy(t.gameObject);
for (int i = 0; i < matches.Count; ++i)
{
GameObject o = Instantiate(serverEntryPrefab) as GameObject;
o.GetComponent<LobbyServerEntry>().Populate(matches*, lobbyManager, (i % 2 == 0) ? OddServerColor : EvenServerColor);*
o.transform.SetParent(serverListRect, false);
}
}
public void ChangePage(int dir)
{
int newPage = Mathf.Max(0, currentPage + dir);
//if we have no server currently displayed, need we need to refresh page0 first instead of trying to fetch any other page
if (noServerFound.activeSelf)
newPage = 0;
RequestPage(newPage);
}
public void RequestPage(int page)
{
previousPage = currentPage;
currentPage = page;
lobbyManager.matchMaker.ListMatches(page, 6, “”, true, 0, 0, OnGUIMatchList);
}
// CustomNetworkDiscovery.Instance.ServerDetected += OnServerDetected;
void OnServerDetected(string ip, string data)
{
Debug.Log(“Received broadCast…”);
if (!CheckForIPAvailable(ip))
{
//MY UI PREFAB
LobbyServerPrefab serverPrefab = Instantiate(serverEntryPrefab);
serverPrefab.IpAddress = ip;
//Broadcast Data Class(Custom)
NetworkBroadcastData networkBroadcastData = (NetworkBroadcastData)JsonUtility.FromJson(data, typeof(NetworkBroadcastData));
serverPrefab.RoomName = networkBroadcastData.roomName;
serverPrefab.TotalPlayers = networkBroadcastData.totalPlayers;
serverPrefab.ConnectedPlayers = networkBroadcastData.connectedPlayers;
LobbyServerList server = new LobbyServerList(ip, serverPrefab, Time.time);
//List of Server Available
if (!LobbyServerList.Contains(server))
{
LobbyServerList.Add(server);
}
else
{
Destroy(serverPrefab.gameObject);
server = null;
}
}
else
{
//If already exist , then update data
UpdateServer(ip, data);
}
}
}