public IEnumerator SendRequests_WithValidUrls_AllCompleted()
        {
            var worker            = new UnityHttpWorker();
            int serverErrorCount  = 0;
            int networkErrorCount = 0;

            worker.ServerErrorOccured  += req => serverErrorCount++;
            worker.NetworkErrorOccured += req => networkErrorCount++;
            var runner = new CoroutineRunner();

            IHttpResponse[] responses = new IHttpResponse[15];

            for (int i = 0; i < responses.Length; i++)
            {
                int index = i;

                var request = HttpRequestBuilder.CreateGet(string.Format("http://www.example.com/?id={0}", i))
                              .GetResult();

                runner.Run(worker.SendRequest(request, rsp => responses[index] = rsp));
            }

            yield return(new WaitUntil(() => responses.All(req => req != null)));

            TestHelper.Assert.That(serverErrorCount, Is.EqualTo(0));
            TestHelper.Assert.That(networkErrorCount, Is.EqualTo(0));
            Assert.That(responses.Count(req => req != null), Is.EqualTo(15));
        }
        private static void Init()
        {
#endif
#if (UNITY_WEBGL || UNITY_PS4 || UNITY_XBOXONE || UNITY_SWITCH) && !UNITY_EDITOR
            Utf8Json.Resolvers.CompositeResolver.RegisterAndSetAsDefault(
                new [] {
                Utf8Json.Formatters.PrimitiveObjectFormatter.Default
            },
                new[] {
                Utf8Json.Resolvers.GeneratedResolver.Instance,
                Utf8Json.Resolvers.BuiltinResolver.Instance,
                Utf8Json.Resolvers.EnumResolver.Default,
                // for unity
                Utf8Json.Unity.UnityResolver.Instance
            }
                );
#endif

            var configFile = Resources.Load("AccelByteSDKConfig");

            if (configFile == null)
            {
                throw new Exception("'AccelByteSDKConfig.json' isn't found in the Project/Assets/Resources directory");
            }

            string wholeJsonText = ((TextAsset)configFile).text;

            AccelBytePlugin.config = wholeJsonText.ToObject <Config>();
            AccelBytePlugin.config.Expand();
            AccelBytePlugin.coroutineRunner = new CoroutineRunner();
            AccelBytePlugin.httpWorker      = new UnityHttpWorker();
            ILoginSession loginSession = new LoginSession(
                AccelBytePlugin.config.LoginServerUrl,
                AccelBytePlugin.config.Namespace,
                AccelBytePlugin.config.ClientId,
                AccelBytePlugin.config.ClientSecret,
                AccelBytePlugin.config.RedirectUri,
                AccelBytePlugin.httpWorker,
                AccelBytePlugin.coroutineRunner, AccelBytePlugin.config.UseSessionManagement);


            AccelBytePlugin.user = new User(
                loginSession,
                new UserAccount(
                    AccelBytePlugin.config.IamServerUrl,
                    AccelBytePlugin.config.Namespace,
                    loginSession,
                    AccelBytePlugin.httpWorker),
                AccelBytePlugin.coroutineRunner,
                AccelBytePlugin.config.UseSessionManagement);

            ServicePointManager.ServerCertificateValidationCallback = AccelBytePlugin.OnCertificateValidated;
        }
Exemplo n.º 3
0
        internal ServerOauthLoginSession(string baseUrl, string clientId, string clientSecret, UnityHttpWorker httpWorker, CoroutineRunner coroutineRunner)
        {
            Assert.IsNotNull(baseUrl, "Creating " + GetType().Name + " failed. Parameter baseUrl is null");
            Assert.IsNotNull(clientId, "Creating " + GetType().Name + " failed. ClientId parameter is null!");
            Assert.IsNotNull(clientSecret, "Creating " + GetType().Name + " failed. ClientSecret parameter is null!");
            Assert.IsNotNull(httpWorker, "Creating " + GetType().Name + " failed. Parameter httpWorker is null");
            Assert.IsNotNull(coroutineRunner, "Creating " + GetType().Name + " failed. Parameter httpWorker is null");

            this.baseUrl         = baseUrl;
            this.clientId        = clientId;
            this.clientSecret    = clientSecret;
            this.httpWorker      = httpWorker;
            this.coroutineRunner = coroutineRunner;
        }
        public IEnumerator SendRequest_GotError400_RaiseHttpError()
        {
            var worker = new UnityHttpWorker();

            //Mocky is free third party mock HTTP server. If some day, it disappears, we should use mock http request
            //Response is BadRequest 400
            var request = HttpRequestBuilder.CreateGet("http://www.mocky.io/v2/5cd44dee350000aa2c7a54a4")
                          .WithContentType(MediaType.TextPlain)
                          .Accepts(MediaType.TextPlain)
                          .GetResult();

            IHttpResponse response = null;

            yield return(worker.SendRequest(request, req => response = req));

            Assert.That(response.Code, Is.EqualTo(400));
        }
        private static void Init()
        {
#endif
#if UNITY_WEBGL && !UNITY_EDITOR
            Utf8Json.Resolvers.CompositeResolver.RegisterAndSetAsDefault(
                new [] {
                Utf8Json.Formatters.PrimitiveObjectFormatter.Default
            },
                new[] {
                Utf8Json.Resolvers.GeneratedResolver.Instance,
                Utf8Json.Resolvers.BuiltinResolver.Instance,
                Utf8Json.Resolvers.EnumResolver.Default,
                // for unity
                Utf8Json.Unity.UnityResolver.Instance
            }
                );
#endif

            var configFile = Resources.Load("AccelByteServerSDKConfig");

            if (configFile == null)
            {
                throw new Exception(
                          "'AccelByteServerSDKConfig.json' isn't found in the Project/Assets/Resources directory");
            }

            string wholeJsonText = ((TextAsset)configFile).text;

            AccelByteServerPlugin.config = wholeJsonText.ToObject <ServerConfig>();
            AccelByteServerPlugin.config.Expand();
            AccelByteServerPlugin.coroutineRunner = new CoroutineRunner();
            AccelByteServerPlugin.httpWorker      = new UnityHttpWorker();

            AccelByteServerPlugin.session = new ServerOauthLoginSession(
                AccelByteServerPlugin.config.IamServerUrl,
                AccelByteServerPlugin.config.ClientId,
                AccelByteServerPlugin.config.ClientSecret,
                AccelByteServerPlugin.httpWorker,
                AccelByteServerPlugin.coroutineRunner);

            AccelByteServerPlugin.server = new DedicatedServer(AccelByteServerPlugin.session,
                                                               AccelByteServerPlugin.coroutineRunner);
        }
        public IEnumerator SendRequest_NoResponse_RequestTimedOut()
        {
            var  worker           = new UnityHttpWorker();
            int  serverErrorCount = 0;
            bool isNetworkError   = false;

            worker.ServerErrorOccured  += req => serverErrorCount++;
            worker.NetworkErrorOccured += req => isNetworkError = true;

            //Mocky is free third party mock HTTP server. If some day, it disappears, we should use mock http request and response
            //Sucess 200, delayed by 9s
            var request = HttpRequestBuilder.CreateGet("http://www.mocky.io/v2/5cd459f335000050407a54e6?mocky-delay=9s")
                          .WithContentType(MediaType.TextPlain)
                          .Accepts(MediaType.TextPlain)
                          .GetResult();

            worker.SetRetryParameters(2000, 250, 500);

            yield return(worker.SendRequest(request, null));

            TestHelper.Assert.That(serverErrorCount, Is.EqualTo(0));
            TestHelper.Assert.IsTrue(isNetworkError);
        }
        public IEnumerator SendRequest_NoConnection_RaiseNetworkErrorImmediately()
        {
            var  worker           = new UnityHttpWorker();
            int  serverErrorCount = 0;
            bool isNetworkError   = false;

            worker.ServerErrorOccured  += req => serverErrorCount++;
            worker.NetworkErrorOccured += req => isNetworkError = true;

            var request = HttpRequestBuilder.CreateGet("http://accelbyte.example")
                          .WithContentType(MediaType.TextPlain)
                          .Accepts(MediaType.TextPlain)
                          .GetResult();

            IHttpResponse response = null;

            worker.SetRetryParameters(2000, 2500, 1000);

            yield return(worker.SendRequest(request, req => response = req));

            TestHelper.Assert.That(serverErrorCount, Is.EqualTo(0));
            TestHelper.Assert.IsTrue(isNetworkError);
        }
        public IEnumerator SendRequest_GotError500Twice_RetryTwice()
        {
            var worker           = new UnityHttpWorker();
            int serverErrorCount = 0;

            worker.ServerErrorOccured += req => serverErrorCount++;

            //Mocky is free third party mock HTTP server. If some day, it disappears, we should use mock http request
            //Response is BadGateway 502
            var request = HttpRequestBuilder.CreateGet("http://www.mocky.io/v2/5cd45103350000de307a54b7")
                          .WithContentType(MediaType.TextPlain)
                          .Accepts(MediaType.TextPlain)
                          .GetResult();

            IHttpResponse response = null;

            worker.SetRetryParameters(5000, 1000, 2000);

            yield return(worker.SendRequest(request, req => response = req));

            TestHelper.Assert.That(serverErrorCount, Is.GreaterThan(0));
            TestHelper.Assert.That(response.Code, Is.EqualTo(502));
        }
        public IEnumerator SendRequests_WithSomeTimeout_AllCompleted()
        {
            int serverErrorCount  = 0;
            int networkErrorCount = 0;
            var worker            = new UnityHttpWorker();

            worker.ServerErrorOccured  += req => serverErrorCount++;
            worker.NetworkErrorOccured += req => networkErrorCount++;
            var otherWorker = new UnityHttpWorker();

            otherWorker.ServerErrorOccured  += req => serverErrorCount++;
            otherWorker.NetworkErrorOccured += req => networkErrorCount++;
            var runner = new CoroutineRunner();

            IHttpResponse[] responses = new IHttpResponse[15];

            for (int i = 0; i < 15; i++)
            {
                IHttpRequest request;
                int          index = i;


                switch (i % 5)
                {
                case 0:
                    request = HttpRequestBuilder.CreateGet(string.Format("http://www.example.com/?id={0}", i)).GetResult();
                    runner.Run(worker.SendRequest(request, req => responses[index] = req));

                    break;

                case 1:
                    request = HttpRequestBuilder.CreateGet(string.Format("http://accelbyte.example/?id={0}", i)).GetResult();
                    runner.Run(worker.SendRequest(request, req => responses[index] = req));

                    break;

                case 2:
                    request = HttpRequestBuilder.CreateGet(
                        string.Format("http://www.mocky.io/v2/5c38bc153100006c00a991ed?mocky-delay=10s&id={0}", i)).GetResult();
                    runner.Run(worker.SendRequest(request, req => responses[index] = req));

                    break;

                case 3:
                    request = HttpRequestBuilder.CreateGet(
                        string.Format("http://www.mocky.io/v2/5c38bc153100006c00a991ed?mocky-delay=10s&id={0}", i)).GetResult();
                    otherWorker.SetRetryParameters(5000);
                    runner.Run(otherWorker.SendRequest(request, req => responses[index] = req));

                    break;

                case 4:
                    request = HttpRequestBuilder.CreateGet(
                        string.Format("http://www.mocky.io/v2/5c37fc0330000054001f659d?mocky-delay=15s&id={0}", i)).GetResult();
                    runner.Run(worker.SendRequest(request, req => responses[index] = req));

                    break;
                }
            }

            yield return(new WaitForSeconds(3));

            Assert.That(responses.Count(req => req != null), Is.EqualTo(6));

            yield return(new WaitForSeconds(5));

            Assert.That(responses.Count(req => req != null), Is.EqualTo(9));

            yield return(new WaitForSeconds(5));

            Assert.That(responses.Count(req => req != null), Is.EqualTo(12));

            yield return(new WaitForSeconds(5));

            Assert.That(responses.Count(req => req != null), Is.EqualTo(15));

            Assert.That(serverErrorCount, Is.EqualTo(0));
            Assert.That(networkErrorCount, Is.EqualTo(6));
        }
 public ServerGameTelemetryApi(string baseUrl, UnityHttpWorker httpWorker)
 {
     this.baseUrl    = baseUrl;
     this.httpWorker = httpWorker;
 }