예제 #1
0
        public HubConnectionHandler(LeagueConnection league)
        {
            this.league = league;

            // Pass parameters in the URL.
            socket = new WebSocket(
                App.HUB_WS
                + "?token=" + HttpUtility.UrlEncode(Persistence.GetHubToken())
                + "&publicKey=" + HttpUtility.UrlEncode(CryptoHelpers.ExportPublicKey())
                );

            socket.OnMessage += HandleMessage;
            socket.OnClose   += (sender, ev) =>
            {
                // Invoke the close handler unless we explicitly triggered this closure.
                // Note that we invoke the event handler and close ourselves as well.
                // This means that in all cases, anyone listening to the closure event does
                // not have to close us. They only need to call Close on us if we want to
                // terminate a connection that is still stable.
                if (!hasClosed)
                {
                    OnClose?.Invoke();
                    Close();
                }
            };

            socket.Connect();
        }
예제 #2
0
        public HubConnectionHandler(LeagueConnection league)
        {
            this.league = league;

            socket = new WebSocket(Program.HUB_WS);
            socket.CustomHeaders = new Dictionary <string, string>()
            {
                { "Token", Persistence.GetHubToken() },
                { "Public-Key", CryptoHelpers.ExportPublicKey() }
            };

            socket.OnMessage += HandleMessage;
            socket.OnClose   += (sender, ev) =>
            {
                // Invoke the close handler unless we explicitly triggered this closure.
                // Note that we invoke the event handler and close ourselves as well.
                // This means that in all cases, anyone listening to the closure event does
                // not have to close us. They only need to call Close on us if we want to
                // terminate a connection that is still stable.
                if (!hasClosed)
                {
                    OnClose?.Invoke();
                    Close();
                }
            };

            socket.Connect();
        }
예제 #3
0
        /**
         * Connects to the hub. Errors if there is already a hub connection.
         * Will cancel a pending reconnection if there is one. This method is
         * not guaranteed to connect on first try.
         */
        public async void Connect()
        {
            if (hubConnectionHandler != null)
            {
                throw new Exception("Already connected.");
            }
            if (!league.IsConnected)
            {
                return;
            }

            try
            {
                DebugLogger.Global.WriteMessage("Connecting to Rift...");

                // Cancel pending reconnect if there is one.
                if (reconnectCancellationTokenSource != null)
                {
                    DebugLogger.Global.WriteMessage($"Canceling older reconnect to Rift.");
                    reconnectCancellationTokenSource.Cancel();
                    reconnectCancellationTokenSource = null;
                }

                // Ensure that our token is still valid...
                bool valid = false; // in case first startup and hub token is empty
                if (!Persistence.GetHubToken().IsNullOrEmpty())
                {
                    DebugLogger.Global.WriteMessage("Requesting hub token..");
                    var response = await httpClient.GetStringAsync(App.HUB + "/check?token=" + Persistence.GetHubToken());

                    valid = response == "true";
                    DebugLogger.Global.WriteMessage($"Hub token validity: {(valid ? "valid" : "invalid")}.");
                }

                // ... and request a new one if it isn't.
                if (!valid)
                {
                    DebugLogger.Global.WriteMessage($"Requesting hub token..");
                    var payload      = string.Format("{{\"pubkey\":\"{0}\"}}", CryptoHelpers.ExportPublicKey());
                    var responseBlob = await httpClient.PostAsync(App.HUB + "/register", new StringContent(payload, Encoding.UTF8, "application/json"));

                    var response = SimpleJson.DeserializeObject <dynamic>(await responseBlob.Content.ReadAsStringAsync());
                    if (!response["ok"])
                    {
                        throw new Exception("Could not receive JWT from Rift");
                    }

                    Persistence.SetHubToken(response["token"]);
                    DebugLogger.Global.WriteMessage($"Hub token: {response["token"]}.");
                }

                // Connect to hub. Will error if token is invalid or server is down, which will prompt a reconnection.
                hubConnectionHandler          = new HubConnectionHandler(league);
                hubConnectionHandler.OnClose += CloseAndReconnect;

                // We assume to be connected.
                if (isNewLaunch)
                {
                    DebugLogger.Global.WriteMessage($"Creating New Launch popup.");
                    //app.ShowNotification("Connected to League. Click here for instructions on how to control your League client from your phone.");
                    isNewLaunch = false;
                }

                hasTriedImmediateReconnect = false;
            }
            catch (Exception e)
            {
                DebugLogger.Global.WriteError($"Connection to Rift failed, an exception occurred: {e.ToString()}");
                // Something happened that we didn't anticipate for.
                // Just try again in a bit.
                CloseAndReconnect();
            }
        }