Exemplo n.º 1
0
        private async Task<WebResponse> ApiPage(WebRequest request)
        {
            // Prepare the response object
            var response = new WebResponse
            {
                // Create a new dictionary for headers - this could be done using a more advanced class for 
                // WebResponse object - I just used a simple structure
                Header = new Dictionary<string, string>
                {
                    // Add content type header
                    {
                        "Content-Type",
                        "application/json'"
                    }
                }
            };

            Stream responseText = new MemoryStream();

            if (request.Uri.IndexOf('?') > -1)
            {
                var queryString = request.Uri.Substring(request.Uri.IndexOf('?'));

                var decoder = new WwwFormUrlDecoder(queryString);

                foreach (var color in decoder.Where(param => param.Name == "color").Select(param => Color.FromArgb(255,
                    Convert.ToByte(param.Value.Substring(0, 2), 16),
                    Convert.ToByte(param.Value.Substring(2, 2), 16),
                    Convert.ToByte(param.Value.Substring(4, 2), 16))))
                {
                    try
                    {
                        await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
                            () =>
                            {
                                var colorBrush = new SolidColorBrush(color);
                                LightColor = colorBrush;

                                var contentWriter = new StreamWriter(responseText);
                                contentWriter.WriteLine(
                                    "{\"status\":200,\"message\":\"Color was set to " + LightColor.Color + "\"}"
                                    );
                                contentWriter.Flush();
                            }
                            );
                    }
                    catch (Exception e)
                    {
                        Debug.WriteLine(e);
                    }
                }
            }

            // Assign the response
            response.Content = responseText;

            // Return the response
            return response;
        }
Exemplo n.º 2
0
        private async Task<WebResponse> HomePage(WebRequest request)
        {
            // Prepare the response object
            var response = new WebResponse
            {
                // Create a new dictionary for headers - this could be done using a more advanced class for 
                // WebResponse object - I just used a simple structure
                Header = new Dictionary<string, string>
                {
                    // Add content type header
                    {
                        "Content-Type",
                        "application/json'"
                    }
                }
            };

            // Build the response content
            Stream responseText = new MemoryStream();
            var contentWriter = new StreamWriter(responseText);
            contentWriter.WriteLine(
                "{\"status\":200,\"message\":\"The API is available at http://" + IpAddress + ":" + Port +
                "/?color=######\"}"
                );
            contentWriter.Flush();

            // Assign the response
            response.Content = responseText;

            // Return the response
            return response;
        }