Пример #1
0
        /// <summary>
        /// Handles the "closed" state change.
        /// </summary>
        /// <param name="ex">The ex.</param>
        /// <returns></returns>
        private async Task HubConnection_Closed(Exception ex)
        {
            SignalRConnectionStateChanged?.Invoke(this, new SignalRConnectionStateChangedEventArgs(SignalRConnectionState.Disconnected));
            await this.hubConnection.DisposeAsync();

            this.logger.LogTrace("SignalR connection to host server has closed." + ex?.Message);
        }
Пример #2
0
        /// <summary>
        /// Handles the "connected" state change.
        /// </summary>
        /// <returns></returns>
        private Task HubConnection_Connected()
        {
            return(Task.Run(async() =>
            {
                SignalRConnectionStateChanged?.Invoke(this, new SignalRConnectionStateChangedEventArgs(SignalRConnectionState.Connected));
                this.logger.LogTrace("Connected to host server.");

                try
                {
                    using (var webClient = new HttpClient())
                    {
                        // Send the contest data to the server to store
                        webClient.BaseAddress = serverUrl;

                        var content = JsonConvert.SerializeObject(App.ContestEngine.Contest);
                        var byteContent = new ByteArrayContent(Encoding.UTF8.GetBytes(content));
                        byteContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");

                        var response = await webClient.PostAsync($"{serverUrl.AbsoluteUri}{ContestHostApis.ContestsRoute}", byteContent);

                        if (response.StatusCode != HttpStatusCode.OK)
                        {
                            logger.LogTrace("bad stuff man.");
                        }

                        // Send the Flight Matrix for the contest as well
                        var flightMatrix = JsonConvert.SerializeObject(App.ContestEngine.FlightMatrix);
                        byteContent = new ByteArrayContent(Encoding.UTF8.GetBytes(flightMatrix));
                        byteContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");

                        response = await webClient.PostAsync($"{serverUrl.AbsoluteUri}{ContestHostApis.FlightMatrixRoute}", byteContent);

                        if (response.StatusCode != HttpStatusCode.OK)
                        {
                            logger.LogTrace("bad stuff man.");
                        }
                    }
                }
                catch (Exception ex)
                {
                    this.logger.LogException(ex);
                }
            }));
        }