Exemple #1
0
        private void ProcessReceivedItem(GSObject response, GSConnection connection)
        {
            string theType = response.Type;

            if (theType.EndsWith("Response"))
            {
                if (theType.Equals(".AuthenticationResponse"))
                {
                    SetUserId(response.GetString("userId"));
                }

                ProcessReceivedResponse(response, connection);
            }
            else if (theType.EndsWith("Message"))
            {
                GSPlatform.ExecuteOnMainThread(() =>
                {
                    try
                    {
                        GSMessageHandler.HandleMessage(this, response);
                    }
                    catch (Exception e)
                    {
                        GSPlatform.DebugMsg(e.ToString());
                    }
                });
            }
        }
Exemple #2
0
        private void ProcessReceivedResponse(GSObject response, GSConnection connection)
        {
            String    requestId = response.GetString("requestId");
            GSRequest request   = connection.GetAndRemovePending(requestId);

            if (request == null)
            {
                return;
            }

            if (request.RequestExpiresAt > 0)
            {
                //It's durable request, if it's a ClientError do nothing as it will be retried
                if (response.ContainsKey("@class") && !response.GetString("@class").Equals("ClientError"))
                {
                    _durableQueueDirty = _persistantQueue.Remove(request);

                    request.Complete(this, response);
                }
            }
            else
            {
                request.Complete(this, response);
            }
        }
Exemple #3
0
        private void SendHandshake(GSObject response, GSConnection connection)
        {
            GSRequest handshakeRequest = new GSRequest(this, "AuthenticatedConnectRequest");

            if (OnGameSparksNonce != null)
            {
                handshakeRequest.AddString("hmac", OnGameSparksNonce(response.GetString("nonce")));
            }
            else
            {
                handshakeRequest.AddString("hmac", GSPlatform.MakeHmac(response.GetString("nonce"), GSPlatform.ApiSecret));
            }

            handshakeRequest.AddString("os", GSPlatform.DeviceOS);
            handshakeRequest.AddString("platform", GSPlatform.Platform);
            handshakeRequest.AddString("deviceId", GSPlatform.DeviceId);

            if (GSPlatform.AuthToken != null && !GSPlatform.AuthToken.Equals("0"))
            {
                handshakeRequest.AddString("authToken", GSPlatform.AuthToken);
            }

            if (_sessionId != null)
            {
                handshakeRequest.AddString("sessionId", _sessionId);
            }

            connection.SendImmediate(handshakeRequest);
        }
Exemple #4
0
        private void CancelRequest(GSConnection connection, GSRequest request)
        {
            /*if (request.Durable)
             * {
             *      return;
             * }*/

            GSObject error = new GSObject("ClientError");

            error.AddObject("error", new GSRequestData().AddString("error", "timeout"));
            error.AddString("requestId", request.GetString("requestId"));

            ProcessReceivedResponse(error, connection);
        }
Exemple #5
0
        internal void WebSocketClient_OnError(String errorMessage, GSConnection connection)
        {
            if (errorMessage.Contains("INVALID LOCATION"))
            {
                Disconnect();
            }
            else if (errorMessage.Contains("UNKNOWN SERVICE"))
            {
                GSPlatform.DebugMsg("ERROR: UNKNOWN SERVICE");

                Disconnect();
            }

            _currentSocketUrl = BuildServiceUrl(GSPlatform);

            if (TraceMessages)
            {
                GSPlatform.DebugMsg("ERROR:" + errorMessage);
            }
        }
Exemple #6
0
        internal void OnMessageReceived(String message, GSConnection connection)
        {
            GSPlatform.DebugMsg("RECV:" + message);

            GSObject response = GSObject.FromJson(message);

            if (response.ContainsKey("connectUrl"))
            {
                _currentSocketUrl = response.GetString("connectUrl");

                connection.Stop();

                CalcNewReconnectionTimeout(0);

                NewConnection();
            }

            if (response.ContainsKey("authToken"))
            {
                GSPlatform.ExecuteOnMainThread(() =>
                {
                    GSPlatform.AuthToken = response.GetString("authToken");
                });
            }

            if (response.Type != null)
            {
                if (".AuthenticatedConnectResponse".Equals(response.Type))
                {
                    Handshake(response, connection);
                }
                else
                {
                    ProcessReceivedItem(response, connection);
                }
            }
        }
Exemple #7
0
        private void Handshake(GSObject response, GSConnection connection)
        {
            if (response.ContainsKey("error"))
            {
                GSPlatform.DebugMsg(response.GetString("error"));

                ShutDown(null);
            }
            else if (response.ContainsKey("nonce"))
            {
                SendHandshake(response, connection);
            }
            else
            {
                if (response.ContainsKey("sessionId"))
                {
                    _sessionId = response.GetString("sessionId");

                    connection.SessionId = _sessionId;

                    if (response.ContainsKey("authToken"))
                    {
                        GSPlatform.ExecuteOnMainThread(() =>
                        {
                            GSPlatform.AuthToken = response.GetString("authToken");
                        });
                    }
                    else
                    {
                        GSPlatform.ExecuteOnMainThread(() =>
                        {
                            GSPlatform.AuthToken = "0";
                            GSPlatform.UserId    = "";
                        });
                    }

                    if (response.ContainsKey("clientConfig"))
                    {
                        GSData clientConfig = response.GetGSData("clientConfig");

                        RetryBase                 = clientConfig.GetInt("retryBase").GetValueOrDefault(GS.RetryBase);
                        RetryMax                  = clientConfig.GetInt("retryMax").GetValueOrDefault(GS.RetryMax);
                        RequestTimeout            = clientConfig.GetInt("requestTimeout").GetValueOrDefault(GS.RequestTimeout);
                        DurableConcurrentRequests = clientConfig.GetInt("durableConcurrentRequests").GetValueOrDefault(GS.DurableConcurrentRequests);
                        DurableDrainInterval      = clientConfig.GetInt("durableDrainInterval").GetValueOrDefault(GS.DurableDrainInterval);
                        HandshakeOffset           = clientConfig.GetInt("handshakeOffset").GetValueOrDefault(GS.HandshakeOffset);
                    }
                    else
                    {
                        RetryBase                 = GS.RetryBase;
                        RetryMax                  = GS.RetryMax;
                        RequestTimeout            = GS.RequestTimeout;
                        DurableConcurrentRequests = GS.DurableConcurrentRequests;
                        DurableDrainInterval      = GS.DurableDrainInterval;
                        HandshakeOffset           = GS.HandshakeOffset;
                    }

                    //We want availability to be triggered before authenticated
                    GSPlatform.DebugMsg("Available");

                    connection.Ready = true;

                    setAvailability(true);

                    if (response.ContainsKey("userId"))
                    {
                        SetUserId(response.GetString("userId"));
                    }

                    CalcNewReconnectionTimeout(0);
                }
            }
        }