예제 #1
0
    public void StartMatchmaking()
    {
        PlayfabUtils.OnSuccess(feedbackText, "Matchmaking in progress...");

        PlayFabMultiplayerAPI.ListQosServersForTitle(new ListQosServersForTitleRequest(), qosRes =>
        {
            var qosServer = qosRes.QosServers[0].ServerUrl;
            var qosRegion = qosRes.QosServers[0].Region;
            Debug.Log($"Pinging QoS Server {qosServer} at {qosRegion}");
            Debug.Log(qosRes.ToJson());

            var sw = System.Diagnostics.Stopwatch.StartNew();

            var udpPort = 5600;
            var done    = false;
            while (!done || udpPort > 5610)
            {
                try
                {
                    UdpClient client = new UdpClient(udpPort);
                    client.Connect(qosServer, 3075);
                    byte[] sendBytes = BitConverter.GetBytes(0xFFFF);
                    client.Send(sendBytes, sendBytes.Length);

                    IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, 3075);
                    byte[] receiveBytes       = client.Receive(ref remoteEndPoint);
                    client.Close();
                    done = true;
                }
                catch (Exception e)
                {
                    Debug.LogError($"[QoS Ping Error]: {e.Message}");
                    udpPort++;
                    Debug.Log($"Retrying with port {udpPort}");
                }
            }
            var pingTime = sw.ElapsedMilliseconds;
            Debug.Log($"Ping success with {pingTime}ms");
            if (udpPort > 5610)
            {
                StartMatchmaking();
                return;
            }

            var request = new CreateMatchmakingTicketRequest
            {
                Creator = new MatchmakingPlayer
                {
                    Entity = new PlayFab.MultiplayerModels.EntityKey
                    {
                        Id   = loginManager.playerData.accountInfo.entityId,
                        Type = PlayfabUtils.ENTITY_TYPE
                    },
                    Attributes = new MatchmakingPlayerAttributes
                    {
                        DataObject = new LatenciesData
                        {
                            Latencies = new List <Latency>
                            {
                                { new Latency {
                                      region = qosRegion, latency = pingTime
                                  } }
                            }
                        }
                    }
                },
                GiveUpAfterSeconds = PlayfabUtils.MATCHMAKING_TIMEOUT,
                QueueName          = PlayfabUtils.MATCHMAKING_NAME
            };

            PlayFabMultiplayerAPI.CreateMatchmakingTicket(request, OnTicketCreated, OnError);
        }, OnError);
    }