UNET Matchmaker, client disconnecting after 15 seconds

Hi all,

I’ve been playing around with the Unity Match Making system, and whilst I have managed to start a match and have a client join, the client always disconnects after about 15 seconds. There is no error message on either server or client, the only indication I get is that the matchmaker property on the client networkmanager becomes null.

Would anyone care to take a look at my network manager script to see if I’ve made any fundamental errors? Otherwise I think I’ll file it as a bug.

This is happening with just a single client connected to the host, that has a basic script attached that just slowly spins it on its Y axis. NetworkTransform component update rate is set to 9 (the default).

Thanks

NetworkManager Code

using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
using UnityEngine.Networking.Match;
using UnityEngine.Networking.Types;
using UnityEngine.UI;

public class MyNetworkManager : NetworkManager {
    [SerializeField] GameObject gameBrowserWindow;
    [SerializeField] GameObject noGamesText;
    [SerializeField] BrowserButton browserButtonPrefab;
    [SerializeField] GameObject offlineUI;
    [SerializeField] GameObject onlineUI;
    [HideInInspector] public bool isHost;
    [HideInInspector] public long m_networkID;
    [HideInInspector] public long m_nodeID;

    public NetworkMatch TheMatchMaker; // Used to visually track the existance of the NetworkMatch in the inspector - added for debugging purposes only

    void Start()
    {
        StartMatchMaker();
        StartCoroutine("AutoListGames");
    }

    public void StartGameServer()
    {
        maxConnections = 2;
        if(matchMaker == null) StartMatchMaker();
        matchMaker.CreateMatch("My Game", (uint)8, true, "", info => {
            base.OnMatchCreate (info);
            isHost = true;
            offlineUI.SetActive(false);
            onlineUI.SetActive(true);
            m_networkID = (long)info.networkId;
            Debug.Log("Match Created " + m_networkID.ToString());
            StopCoroutine("AutoListGames");
        });
    }
       
    public void JoinGame(MatchDesc match)
    {
        TheMatchMaker = matchMaker;
        if(matchMaker == null) StartMatchMaker();
        matchName = match.name;
        matchSize = (uint)match.currentSize;
        matchMaker.JoinMatch(match.networkId, "", info => {
            base.OnMatchJoined(info);
            if(info.success)
            {
                m_nodeID = (long)info.nodeId;
                StartClient(new MatchInfo(info));
                offlineUI.SetActive(false);
                onlineUI.SetActive(true);
                Debug.Log("Match found, joining game...");
                StopCoroutine("AutoListGames");
            }
            else
            {
                Debug.Log("Error Connecting!");
            }
        });
    }

    IEnumerator AutoListGames()
    {
        while(true)
        {
            yield return new WaitForSeconds(3f);

            if(gameBrowserWindow.gameObject.activeInHierarchy)
            {
                if(matchMaker == null) StartMatchMaker();
                matchMaker.ListMatches(0, 20, "", OnMatchList);
            }
        }
    }

    public override void OnMatchList (UnityEngine.Networking.Match.ListMatchResponse matchList)
    {
        // Remove existing buttons
        foreach(Transform child in gameBrowserWindow.GetComponentInChildren<GridLayoutGroup>().transform)
        {
            DestroyImmediate(child.gameObject);
        }

        if(matchList.matches == null || matchList.matches.Count == 0)
        {
            noGamesText.SetActive(true);
        }
        else
        {
            noGamesText.SetActive(false);
            foreach(var match in matchList.matches)
            {
                var button = Instantiate<BrowserButton>(browserButtonPrefab);
                button.OnClickedHandler = JoinGame;
                button.match = match;
                button.SetText(match.name);
                button.transform.SetParent(gameBrowserWindow.GetComponentInChildren<GridLayoutGroup>().transform, false);
            }
        }
    }

    public void Disconnect()
    {
        if(isHost)
        {
            matchMaker.DestroyMatch((NetworkID)m_networkID,response =>
                {
                    Debug.Log("Shutdown host");
                    NetworkManager.singleton.StopHost();
                    NetworkManager.singleton.StopMatchMaker();
                });
        }
        else
        {
            matchMaker.DropConnection((NetworkID)m_networkID, (NodeID)m_nodeID, response =>
                {
                    Debug.Log("Shutdown client");
                    NetworkManager.singleton.StopClient();
                    NetworkManager.singleton.StopMatchMaker();
                });
        }

        offlineUI.SetActive(true);
        onlineUI.SetActive(false);
        StartCoroutine("AutoListGames");
    }
}

Player script

using UnityEngine;
using System.Collections;
using UnityEngine.Networking;

public class Player : NetworkBehaviour {

    // Use this for initialization
    void Start () {
   
    }
   
    // Update is called once per frame
    void Update () {
        if(isLocalPlayer)
        {
            transform.Rotate(0,20 * Time.deltaTime,0);
        }
    }
}

BrowserButton script

using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
using UnityEngine.Networking;
using UnityEngine.Networking.Match;
using UnityEngine.UI;

public class BrowserButton : MonoBehaviour, IPointerClickHandler {
    public System.Action<MatchDesc> OnClickedHandler;
    public MatchDesc match;

    public void SetText(string gameName)
    {
        GetComponentInChildren<Text>().text = gameName;
    }

    #region IPointerClickHandler implementation
    public void OnPointerClick (PointerEventData eventData)
    {
        if(OnClickedHandler != null)
            OnClickedHandler.Invoke(match);
    }
    #endregion
}

Total beginner here… so please excuse the odd question, but…

When your client gets disconnected, can you go back into the matchmaker and search for the game again? Does it show up? if so can you rejoin?

Yes, because I get no notification in the program of a disconnect, I can still click the disconnect button on my UI, which takes me back to the game browser and allows me to reconnect.

However it then disconnects again after about 15 seconds.

Hmm, sounds kinda similar to my problems (although I’m not as experienced as you).

Out of curiosity, how many networked items have you got? What is their send rate and the like? With my problem I’m still testing to see if it’s to do with the number of syncing objects…

Have you tried reducing/turning things off to see what’s causing the problem?

Again, sorry if I’m not helping… lol.

Extremely simple test scene, one player object with the player script as above, network send rate is 9. So I know it’s nothing to do with bandwidth or number of synced objects.

Are you using the Network Simulator or an external network simulator like Clumsy? I’ve noticed that if you have the packet loss set to anything higher than 0%, you will always get a disconnection.

But since you’re using the matchmaker, it could also be that you’re sending too much data. You only get 4KB/s for each player I believe, and if you go over that it disconnects you. You do get an initial buffer for two minutes (I think?) where each player can send/receive more data per second.

@srylain do you have a link for that information… Might be my prob as well…

I’ve had a response from Unity and the bug has been passed to the developers for resolution (case no. 794106).

In the meantime if you override OnClientDisconnect() but don’t call base.OnClientDisconnect() then the client will stay connected despite the bug.

Example code:

public override void OnClientDisconnect (NetworkConnection conn)
    {
        Debug.Log("Client would disconnect now if the following line is uncommented.");
        //base.OnClientDisconnect (conn);
    }

Hope this helps.

1 Like