Exemple #1
0
        void OnConnectRequestFinished(HTTPRequest req, HTTPResponse resp)
        {
            // error reason if there is any. We will call the manager's Error function if it's not empty.
            string reason = string.Empty;

            switch (req.State)
            {
            // The request finished without any problem.
            case HTTPRequestStates.Finished:
                if (resp.IsSuccess)
                {
                    HTTPManager.Logger.Information("Transport - " + this.Name, "Connect - Request Finished Successfully! " + resp.DataAsText);

                    OnConnected();

                    IServerMessage msg = TransportBase.Parse(Connection.JsonEncoder, resp.DataAsText);

                    if (msg != null)
                    {
                        Connection.OnMessage(msg);

                        MultiMessage multiple = msg as MultiMessage;
                        if (multiple != null && multiple.PollDelay.HasValue)
                        {
                            PollDelay = multiple.PollDelay.Value;
                        }
                    }
                }
                else
                {
                    reason = string.Format("Connect - Request Finished Successfully, but the server sent an error. Status Code: {0}-{1} Message: {2}",
                                           resp.StatusCode,
                                           resp.Message,
                                           resp.DataAsText);
                }
                break;

            // The request finished with an unexpected error. The request's Exception property may contain more info about the error.
            case HTTPRequestStates.Error:
                reason = "Connect - Request Finished with Error! " + (req.Exception != null ? (req.Exception.Message + "\n" + req.Exception.StackTrace) : "No Exception");
                break;

            // The request aborted, initiated by the user.
            case HTTPRequestStates.Aborted:
                reason = "Connect - Request Aborted!";
                break;

            // Ceonnecting to the server is timed out.
            case HTTPRequestStates.ConnectionTimedOut:
                reason = "Connect - Connection Timed Out!";
                break;

            // The request didn't finished in the given time.
            case HTTPRequestStates.TimedOut:
                reason = "Connect - Processing the request Timed Out!";
                break;
            }

            if (!string.IsNullOrEmpty(reason))
            {
                Connection.Error(reason);
            }
        }
Exemple #2
0
        void OnPollRequestFinished(HTTPRequest req, HTTPResponse resp)
        {
            // When Stop() called on the transport.
            // In Stop() we set the pollRequest to null, but a new poll request can be made after a quick reconnection, and there is a chanse that
            // in this handler function we can null out the new request. So we return early here.
            if (req.State == HTTPRequestStates.Aborted)
            {
                HTTPManager.Logger.Warning("Transport - " + this.Name, "Poll - Request Aborted!");
                return;
            }

            // Set the pollRequest to null, now we can send out a new one
            pollRequest = null;

            // error reason if there is any. We will call the manager's Error function if it's not empty.
            string reason = string.Empty;

            switch (req.State)
            {
            // The request finished without any problem.
            case HTTPRequestStates.Finished:
                if (resp.IsSuccess)
                {
                    HTTPManager.Logger.Information("Transport - " + this.Name, "Poll - Request Finished Successfully! " + resp.DataAsText);

                    IServerMessage msg = TransportBase.Parse(Connection.JsonEncoder, resp.DataAsText);

                    if (msg != null)
                    {
                        Connection.OnMessage(msg);

                        MultiMessage multiple = msg as MultiMessage;
                        if (multiple != null && multiple.PollDelay.HasValue)
                        {
                            PollDelay = multiple.PollDelay.Value;
                        }

                        LastPoll = DateTime.UtcNow;
                    }
                }
                else
                {
                    reason = string.Format("Poll - Request Finished Successfully, but the server sent an error. Status Code: {0}-{1} Message: {2}",
                                           resp.StatusCode,
                                           resp.Message,
                                           resp.DataAsText);
                }
                break;

            // The request finished with an unexpected error. The request's Exception property may contain more info about the error.
            case HTTPRequestStates.Error:
                reason = "Poll - Request Finished with Error! " + (req.Exception != null ? (req.Exception.Message + "\n" + req.Exception.StackTrace) : "No Exception");
                break;

            // Ceonnecting to the server is timed out.
            case HTTPRequestStates.ConnectionTimedOut:
                reason = "Poll - Connection Timed Out!";
                break;

            // The request didn't finished in the given time.
            case HTTPRequestStates.TimedOut:
                reason = "Poll - Processing the request Timed Out!";
                break;
            }

            if (!string.IsNullOrEmpty(reason))
            {
                Connection.Error(reason);
            }
        }
Exemple #3
0
        /// <summary>
        /// Creates an Uri instance from the given parameters.
        /// </summary>
        Uri IConnection.BuildUri(RequestTypes type, TransportBase transport)
        {
            lock (SyncRoot)
            {
                // make sure that the queryBuilder is reseted
                queryBuilder.Length = 0;

                UriBuilder uriBuilder = new UriBuilder(Uri);

                if (!uriBuilder.Path.EndsWith("/"))
                    uriBuilder.Path += "/";

                this.RequestCounter %= UInt64.MaxValue;

                switch (type)
                {
                    case RequestTypes.Negotiate:
                        uriBuilder.Path += "negotiate";
                        goto default;

                    case RequestTypes.Connect:
#if !BESTHTTP_DISABLE_WEBSOCKET
                        if (transport != null && transport.Type == TransportTypes.WebSocket)
                            uriBuilder.Scheme = HTTPProtocolFactory.IsSecureProtocol(Uri) ? "wss" : "ws";
#endif

                        uriBuilder.Path += "connect";
                        goto default;

                    case RequestTypes.Start:
                        uriBuilder.Path += "start";
                        goto default;

                    case RequestTypes.Poll:
                        uriBuilder.Path += "poll";
                        if (this.LastReceivedMessage != null)
                        {
                            queryBuilder.Append("messageId=");
                            queryBuilder.Append(this.LastReceivedMessage.MessageId);
                        }
                        goto default;

                    case RequestTypes.Send:
                        uriBuilder.Path += "send";
                        goto default;

                    case RequestTypes.Reconnect:
#if !BESTHTTP_DISABLE_WEBSOCKET
                        if (transport != null && transport.Type == TransportTypes.WebSocket)
                            uriBuilder.Scheme = HTTPProtocolFactory.IsSecureProtocol(Uri) ? "wss" : "ws";
#endif

                        uriBuilder.Path += "reconnect";

                        if (this.LastReceivedMessage != null)
                        {
                            queryBuilder.Append("messageId=");
                            queryBuilder.Append(this.LastReceivedMessage.MessageId);
                        }

                        if (!string.IsNullOrEmpty(GroupsToken))
                        {
                            if (queryBuilder.Length > 0)
                                queryBuilder.Append("&");

                            queryBuilder.Append("groupsToken=");
                            queryBuilder.Append(GroupsToken);
                        }

                        goto default;

                    case RequestTypes.Abort:
                        uriBuilder.Path += "abort";
                        goto default;

                    case RequestTypes.Ping:
                        uriBuilder.Path += "ping";

                        queryBuilder.Append("&tid=");
                        queryBuilder.Append(this.RequestCounter++.ToString());

                        queryBuilder.Append("&_=");
                        queryBuilder.Append(Timestamp.ToString());

                        break;

                    default:
                        if (queryBuilder.Length > 0)
                            queryBuilder.Append("&");

                        queryBuilder.Append("tid=");
                        queryBuilder.Append(this.RequestCounter++.ToString());

                        queryBuilder.Append("&_=");
                        queryBuilder.Append(Timestamp.ToString());

                        if (transport != null)
                        {
                            queryBuilder.Append("&transport=");
                            queryBuilder.Append(transport.Name);
                        }

                        queryBuilder.Append("&clientProtocol=");
                        queryBuilder.Append(ClientProtocol);

                        if (NegotiationResult != null && !string.IsNullOrEmpty(this.NegotiationResult.ConnectionToken))
                        {
                            queryBuilder.Append("&connectionToken=");
                            queryBuilder.Append(this.NegotiationResult.ConnectionToken);
                        }

                        if (this.Hubs != null && this.Hubs.Length > 0)
                        {
                            queryBuilder.Append("&connectionData=");
                            queryBuilder.Append(this.ConnectionData);
                        }

                        break;
                }

                // Query params are added to all uri
                if (this.AdditionalQueryParams != null && this.AdditionalQueryParams.Count > 0)
                    queryBuilder.Append(this.QueryParams);

                uriBuilder.Query = queryBuilder.ToString();

                // reset the string builder
                queryBuilder.Length = 0;

                return uriBuilder.Uri;
            }
        }