Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Client not connecting to the discovered servers. #223

Open
saqibkhan2523 opened this issue May 16, 2023 · 0 comments
Open

Client not connecting to the discovered servers. #223

saqibkhan2523 opened this issue May 16, 2023 · 0 comments

Comments

@saqibkhan2523
Copy link

saqibkhan2523 commented May 16, 2023

So I used the scripts (network discovery folder) as it is except for the UI, Client is getting the list of servers but on selecting one server I think there is no connection, client after attempting to connect to a server timesout and connection is failed.

Here is my UI script.

`
[SerializeField] private RectTransform canvas;

[SerializeField] private GameObject roomDataPrefab;

[SerializeField, HideInInspector]
DiscoverRooms m_Discovery;

NetworkManager m_NetworkManager;

Dictionary<IPAddress, DiscoveryResponseData> discoveredServers = new Dictionary<IPAddress, DiscoveryResponseData>();

// Define UI Components
[SerializeField] private Button startFindingRoomBtn;
[SerializeField] private Button stopFindingRoomBtn;
[SerializeField] private Button refreshListBtn;

[SerializeField] private Button createRoomPanelBtn;
[SerializeField] private Button createRoomBtn;
[SerializeField] private Button hideServerBtn;

[SerializeField] private Transform serverListParent; // A parent transform for server list UI

[SerializeField] private GameObject createRoomPanel;
[SerializeField] private GameObject LobbyPanel;

[SerializeField] private TMP_InputField roomName;
[SerializeField] private TMP_InputField maxPlayers;

[SerializeField] private GameObject roomPanel;

[SerializeField] private Button enterIPBtn;
[SerializeField] private Button enterIPJoinBtn;
[SerializeField] private GameObject enterIPPanel;

void Awake()
{
    createRoomPanelBtn.onClick.AddListener(() =>
    {
        LobbyPanel.gameObject.SetActive(false);
        createRoomPanel.gameObject.SetActive(true);
    });

    m_Discovery = GetComponent<DiscoverRooms>();
    m_NetworkManager = GetComponent<NetworkManager>();

    // Set up button actions
    startFindingRoomBtn.onClick.AddListener(() => {
        
        m_Discovery.StartClient();
        m_Discovery.ClientBroadcast(new DiscoveryBroadcastData());
        stopFindingRoomBtn.gameObject.SetActive(true);
        refreshListBtn.gameObject.SetActive(true);
        startFindingRoomBtn.gameObject.SetActive(false);
    });

    stopFindingRoomBtn.onClick.AddListener(() => {
        m_Discovery.StopDiscovery();
        discoveredServers.Clear();
        stopFindingRoomBtn.gameObject.SetActive(false);
        refreshListBtn.gameObject.SetActive(false);
        startFindingRoomBtn.gameObject.SetActive(true);
    });

    refreshListBtn.onClick.AddListener(() => {
        discoveredServers.Clear();
        m_Discovery.ClientBroadcast(new DiscoveryBroadcastData());
    });
    createRoomBtn.onClick.AddListener(() => {
        m_Discovery.ServerName = roomName.text;
        m_Discovery.maxPlayers = int.Parse(maxPlayers.text);
        NetworkManager.Singleton.StartServer();
        //NetworkManager.Singleton.OnServerStarted += m_Discovery.StartServer;
        createRoomPanel.gameObject.SetActive(false);
        roomPanel.gameObject.SetActive(true);

    });

    enterIPBtn.onClick.AddListener(() => {
        LobbyPanel.gameObject.SetActive(false);

        enterIPPanel.gameObject.SetActive(true);
    });

    enterIPBtn.onClick.AddListener(() => {
        //string ip = enterIPPanel.transform.GetChild(0).GetComponent<TMP_InputField>().text;
        //int port = int.Parse(enterIPPanel.transform.GetChild(1).GetComponent<TMP_InputField>().text);



        //UnityTransport transport = (UnityTransport)m_NetworkManager.NetworkConfig.NetworkTransport;
        //transport.SetConnectionData(ip, (ushort)port);
        //m_NetworkManager.StartClient();

        //enterIPPanel.gameObject.SetActive(false);
    });

    hideServerBtn.onClick.AddListener(m_Discovery.StopDiscovery);
}

#if UNITY_EDITOR
void OnValidate()
{
if (m_Discovery == null)
{
m_Discovery = GetComponent();
UnityEventTools.AddPersistentListener(m_Discovery.OnServerFound, OnServerFound);
Undo.RecordObjects(new Object[] { this, m_Discovery }, "Set NetworkDiscovery");
}
}
#endif

void OnServerFound(IPEndPoint sender, DiscoveryResponseData response)
{
    discoveredServers[sender.Address] = response;
    UpdateServerList();
}

void UpdateServerList()
{
    // Clear existing server list UI
    foreach (Transform child in serverListParent)
    {
        Destroy(child.gameObject);
    }

    // Create new server list UI
    foreach (var discoveredServer in discoveredServers)
    {
        var newButtonObject = Instantiate(roomDataPrefab, serverListParent);
        var newButton = newButtonObject.transform.GetChild(1).GetComponent<Button>();
        var newText = newButtonObject.transform.GetChild(0).GetComponentInChildren<TextMeshProUGUI>();

        newText.text = $"{discoveredServer.Value.ServerName},[{discoveredServer.Key}],Max Players:{discoveredServer.Value.MaxPlayers}";
        newButton.onClick.AddListener(() => 
        {
            UnityTransport transport = (UnityTransport)m_NetworkManager.NetworkConfig.NetworkTransport;
            transport.SetConnectionData(discoveredServer.Key.ToString(), discoveredServer.Value.Port);
            m_NetworkManager.StartClient();
            LobbyPanel.gameObject.SetActive(false);
        });
    }
}`
@saqibkhan2523 saqibkhan2523 changed the title network Manager functions not working. Client not connecting to the discovered servers. May 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant