IEnumerator PollMatchmakingServer()
    {
        string server = $"http://{GameController.Instance.MatchmakingServer.Address.ToString()}:{GameController.Instance.MatchmakingServer.Port}";
        // join room
        UnityWebRequest www = UnityWebRequest.Post($"{server}/join/{JoinRoom}/{GameController.Instance.Username}", "");

        yield return(www.SendWebRequest());

        MatchmakingJoinResponse joinResponse = null;

        if (www.isNetworkError || www.isHttpError)
        {
            // TODO: room has closed error or something, or host left,
            // or bad room code
            Debug.Log("Invalid room code.");
            GameController.Instance.CleanupNetwork();
            MatchmakingUI.Instance.ResetUI();
            yield break;
        }
        else
        {
            joinResponse = JsonConvert.DeserializeObject <MatchmakingJoinResponse>(www.downloadHandler.text);
            if (joinResponse != null)
            {
                // connect to new clients
                client.ConnectionKey = joinResponse.connection_key;
                client.netManager.NatPunchModule.SendNatIntroduceRequest(GameController.Instance.NatPunchServer, joinResponse.connect);
            }
        }
        // keep room open for next play
        while (client.GameStarted)
        {
            yield return(www.SendWebRequest());

            MatchmakingRefreshResponse refreshResponse = null;
            if (www.isNetworkError || www.isHttpError)
            {
                // TODO: room closed
                throw new UnityException(www.error);
            }
            else
            {
                refreshResponse = JsonConvert.DeserializeObject <MatchmakingRefreshResponse>(www.downloadHandler.text);
                if (refreshResponse.expired)
                {
                    throw new UnityException("Lost spot in matchmaking server.");
                }
            }
            // refresh only enough to keep our spot
            yield return(new WaitForSeconds(8f));
        }
    }
示例#2
0
    IEnumerator PollMatchmakingServer()
    {
        string server = $"http://{GameController.Instance.MatchmakingServer.Address.ToString()}:{GameController.Instance.MatchmakingServer.Port}";
        // create room
        UnityWebRequest www = UnityWebRequest.Post($"{server}/rooms/{GameController.Instance.Username}/{(roomIsPublic ? 1 : 0)}", "");

        yield return(www.SendWebRequest());

        MatchmakingCreateResponse createResponse = null;

        if (www.isNetworkError || www.isHttpError)
        {
            throw new UnityException(www.error);
        }
        else
        {
            createResponse     = JsonConvert.DeserializeObject <MatchmakingCreateResponse>(www.downloadHandler.text);
            host.ConnectionKey = createResponse.connection_key;
            Debug.Log($"Room code: {createResponse.room_code}");
        }
        // continuously refresh room until start
        while (!host.GameStarted)
        {
            www = UnityWebRequest.Post($"{server}/refresh/{createResponse.user_id}", "");
            yield return(www.SendWebRequest());

            MatchmakingRefreshResponse refreshResponse = null;
            if (www.isNetworkError || www.isHttpError)
            {
                throw new UnityException(www.error);
            }
            else
            {
                refreshResponse = JsonConvert.DeserializeObject <MatchmakingRefreshResponse>(www.downloadHandler.text);
                if (refreshResponse != null)
                {
                    // connect to new clients
                    foreach (string natPunchCode in refreshResponse.connect)
                    {
                        if (!sentNatPunch.ContainsKey(natPunchCode))
                        {
                            sentNatPunch[natPunchCode] = true;
                            host.netManager.NatPunchModule.SendNatIntroduceRequest(GameController.Instance.NatPunchServer, natPunchCode);
                        }
                    }
                }
            }
            // host refreshes a lot since it has to look for new clients
            yield return(new WaitForSeconds(1.0f));
        }
        // game start and the like is handled by NetworkHost
        // keep room open for next play
        while (host.GameStarted)
        {
            www = UnityWebRequest.Post($"{server}/refresh/{createResponse.user_id}", "");
            yield return(www.SendWebRequest());

            MatchmakingRefreshResponse refreshResponse = null;
            if (www.isNetworkError || www.isHttpError)
            {
                throw new UnityException(www.error);
            }
            else
            {
                refreshResponse = JsonConvert.DeserializeObject <MatchmakingRefreshResponse>(www.downloadHandler.text);
                if (refreshResponse.expired)
                {
                    throw new UnityException("Lost spot in matchmaking server.");
                }
            }
            // refresh only enough to keep our spot
            yield return(new WaitForSeconds(8f));
        }
    }