public async Task ConnectAsync()
        {
            try
            {
                IsBusy = true;

                string negotiateJson = await client.GetStringAsync($"{Constants.HostName}/api/negotiate");

                NegotiateInfo negotiate = JsonConvert.DeserializeObject <NegotiateInfo>(negotiateJson);

                HubConnection connection = new HubConnectionBuilder()
                                           .AddNewtonsoftJsonProtocol()
                                           .WithUrl(negotiate.Url, options =>
                {
                    options.AccessTokenProvider = async() => negotiate.AccessToken;
                })
                                           .Build();

                connection.Closed += Connection_Closed;
                connection.On <JObject>(Constants.MessageName, AddNewMessage);
                await connection.StartAsync();

                IsConnected = true;
                IsBusy      = false;

                Connected?.Invoke(this, true, "Connection successful.");
            }
            catch (Exception ex)
            {
                ConnectionFailed?.Invoke(this, false, ex.Message);
                IsConnected = false;
                IsBusy      = false;
            }
        }
Пример #2
0
        public async Task ConnectAsync()
        {
            try
            {
                IsBusy = true;

                string negotiateJson = await client.GetStringAsync($"{Constants.HostName}/api/negotiate");

                NegotiateInfo negotiate = JsonConvert.DeserializeObject <NegotiateInfo>(negotiateJson);

                HubConnection connection = new HubConnectionBuilder()
                                           .AddNewtonsoftJsonProtocol()
                                           .WithUrl(negotiate.Url, options =>
                {
                    options.AccessTokenProvider = async() => negotiate.AccessToken;
                })
                                           .Build();

                connection.Closed += Connection_Closed;
                // The ConnectAsync method registers an event handler for the message name defined in the sample application's Constants.cs file, which is "newMessage" by default.
                connection.On <JObject>(Constants.MessageName, AddNewMessage);
                await connection.StartAsync();

                IsConnected = true;
                IsBusy      = false;

                Connected?.Invoke(this, true, "Connection successful.");
            }
            catch (Exception ex)
            {
                ConnectionFailed?.Invoke(this, false, ex.Message);
                IsConnected = false;
                IsBusy      = false;
            }
        }
Пример #3
0
        public async Task ConnectAsync()
        {
            try
            {
                IsBusy = true;
                //https://api.corpemf.com/music-player/v1/NowPlaying/negotiate?Hub=KloveLive

                var response = await client.PostAsync($"{Application.CData.SignalRBaseUrl}{Variables.CurrentChannel.HubName}", null);

                string negotiateJson = await response.Content.ReadAsStringAsync();

                NegotiateInfo negotiate = JsonConvert.DeserializeObject <NegotiateInfo>(negotiateJson);

                connection = new HubConnectionBuilder()
                             .WithAutomaticReconnect()
                             .AddNewtonsoftJsonProtocol()
                             .WithUrl(negotiate.Url, options =>
                {
                    options.AccessTokenProvider = async() => negotiate.AccessToken;
                })
                             .Build();

                connection.Closed += Connection_Closed;
                connection.On <JObject>("songChanged", AddNewMessage);
                await connection.StartAsync();

                IsConnected = true;
                IsBusy      = false;
                Console.WriteLine("Signal R connection started.");
                Connected?.Invoke(this, true, "Connection successful.");
            }
            catch (Exception ex)
            {
                Console.WriteLine("Signal R connection failed.");
                ConnectionFailed?.Invoke(this, false, ex.Message);
                IsConnected = false;
                IsBusy      = false;
            }
        }