Example #1
0
        /// <summary>
        /// Create server
        /// </summary>
        private void createServer()
        {
            if (isServer) {
                return;
            }
            isServer = true;

            Dictionary<Regex, RuleDeletage> rules = new Dictionary<Regex, RuleDeletage>();
            rules.Add(new Regex("^/copilot/api/.*$"), (e) =>
            {
                var url = new Uri("http://test" + e.uri);
                var request = url.LocalPath.Replace("/copilot/api/", "");

                switch(request) {
                    case "skin":
                        return this.skin();
                    case "language":
                        return this.language();
                    case "data":
                        return this.data(url.Query.Substring(1));
                    default:
                        return Task.FromResult(new IDCT.webResposne());
                }
            });
            rules.Add(new Regex("^/copilot/.*$"), (e) =>
            {
                var file = e.uri.Replace("/copilot/", "");
                return loadFile(file.Length == 0 ? "index.html" : file);
            });
            rules.Add(new Regex("^/.*$"), (e) =>
            {
                return e404();
            });

            WebServer myWebServer = new WebServer(rules, IPAddress.ToString(), "80");
        }
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            bool found = false;
            string ip = "";

            //get all network adapters
            var adapters = Windows.Networking.Connectivity.NetworkInformation.GetHostNames();
            if (adapters.Count == 0)
            {
                MessageBox.Show("Turn on wifi");
            }
            else
            {
                foreach (var adapter in adapters)
                {
                    //find the Wifi adapter (interface type == 71)
                    if (adapter.IPInformation.NetworkAdapter.IanaInterfaceType == 71 && (adapter.Type == Windows.Networking.HostNameType.Ipv4 || adapter.Type == Windows.Networking.HostNameType.Ipv6))
                    {
                        //if found assign it's ip to a variable
                        found = true;
                        ip = adapter.RawName;
                        break;
                    }
                }

                if (found == true)
                {
                    //create a new dictionary for server rules
                    Dictionary<Regex, RuleDeletage> rules = new Dictionary<Regex, RuleDeletage>();

                    //add a rule for homepage: url /
                    //will fire method "homePage" when triggered
                    Regex rgx = new Regex("^/$");
                    rules.Add(rgx, homePage);

                    //add a rule for file any url under subfolder /files
                    //this is a file download example
                    Regex rgx_file = new Regex("^/files/.*$");
                    rules.Add(rgx_file, getfile);

                    //with the set of rules and IP of the network adapter create a new web server object and assign an error event method
                    simpleWebServer = new WebServer(rules, ip, "80");
                    simpleWebServer.errorOccured += ws_errorOccured;

                    ((Button)sender).IsEnabled = false;
                    ipinfo.Text = "With another device in the network\r\ngo to:\r\nhttp://" + ip + "/";
                }
                else
                {
                    MessageBox.Show("Turn on wifi");
                }
            }
        }