private void StartServer()
        {
            var found = false;

            // Get all network adapters
            var adapters = NetworkInformation.GetHostNames();
            if (adapters.Count == 0)
            {
                Debug.WriteLine("Turn on WiFi");
            }
            else
            {
                foreach (
                    var adapter in
                        adapters.Where(
                            adapter =>
                                adapter.IPInformation != null &&
                                (
                                    (
                                        adapter.IPInformation.NetworkAdapter.IanaInterfaceType == 71 || // An IEEE 802.11 wireless network interface
                                        adapter.IPInformation.NetworkAdapter.IanaInterfaceType == 6     // An Ethernet network interface
                                    ) &&
                                    (
                                        adapter.Type == HostNameType.Ipv4 || 
                                        adapter.Type == HostNameType.Ipv6
                                    )
                                )
                            )
                )
                {
                    // If found assign it's IP to a variable
                    found = true;
                    IpAddress = adapter.RawName;
                    break;
                }

                if (found)
                {
                    // create a new dictionary for server rules
                    var rules = new Dictionary<Regex, RuleDeletage>
                    {
                        // add a rule for homepage: URL /
                        // will fire method "homePage" when triggered
                        {
                            new Regex("^/$"),
                            HomePage
                        },

                        // Add the API
                        {
                            new Regex(@"^\/\?color=[a-f0-9]*$", RegexOptions.IgnoreCase),
                            ApiPage
                        }
                    };

                    // With the set of rules and IP of the network adapter create a new web server object and 
                    // assign an error event method
                    _webServer = new WebServer(rules, IpAddress, Port.ToString());
                    _webServer.ErrorOccured += WebServerOnErrorOccured;

                    Debug.WriteLine("Server listening to: http://" + IpAddress + ":" + Port + "/");
                }
                else
                {
                    Debug.WriteLine("Turn on WiFi");
                }
            }
        }
Example #2
0
        private void StartServer()
        {
            var found = false;

            // Get all network adapters
            var adapters = NetworkInformation.GetHostNames();

            if (adapters.Count == 0)
            {
                Debug.WriteLine("Turn on WiFi");
            }
            else
            {
                foreach (
                    var adapter in
                    adapters.Where(
                        adapter =>
                        adapter.IPInformation != null &&
                        (
                            (
                                adapter.IPInformation.NetworkAdapter.IanaInterfaceType == 71 ||         // An IEEE 802.11 wireless network interface
                                adapter.IPInformation.NetworkAdapter.IanaInterfaceType == 6             // An Ethernet network interface
                            ) &&
                            (
                                adapter.Type == HostNameType.Ipv4 ||
                                adapter.Type == HostNameType.Ipv6
                            )
                        )
                        )
                    )
                {
                    // If found assign it's IP to a variable
                    found     = true;
                    IpAddress = adapter.RawName;
                    break;
                }

                if (found)
                {
                    // create a new dictionary for server rules
                    var rules = new Dictionary <Regex, RuleDeletage>
                    {
                        // add a rule for homepage: URL /
                        // will fire method "homePage" when triggered
                        {
                            new Regex("^/$"),
                            HomePage
                        },

                        // Add the API
                        {
                            new Regex(@"^\/\?color=[a-f0-9]*$", RegexOptions.IgnoreCase),
                            ApiPage
                        }
                    };

                    // With the set of rules and IP of the network adapter create a new web server object and
                    // assign an error event method
                    _webServer = new WebServer(rules, IpAddress, Port.ToString());
                    _webServer.ErrorOccured += WebServerOnErrorOccured;

                    Debug.WriteLine("Server listening to: http://" + IpAddress + ":" + Port + "/");
                }
                else
                {
                    Debug.WriteLine("Turn on WiFi");
                }
            }
        }