static void Main(string[] args)
        {
            using (var server = new NHttp.HttpServer())
            {
                // New requests are signaled through the RequestReceived
                // event.

                server.RequestReceived += Server_RequestReceived;


                // Start the server on a random port. Use server.EndPoint
                // to specify a specific port, e.g.:
                //
                server.EndPoint = new IPEndPoint(IPAddress.Loopback, Port);
                //

                server.Start();

                // Start the default web browser.
                // Process.Start(String.Format("http://{0}/", server.EndPoint));

                //Console.WriteLine("Press any key to continue...");
                //Console.ReadKey();

                while (true)
                {
                    System.Threading.Thread.Sleep(1);
                }
                // When the HttpServer is disposed, all opened connections
                // are automatically closed.
            }
        }
        void ThreadFunc()
        {
            //listener = new HttpListener();

            ////listener.Prefixes.Add("http://+:8081/");
            //listener.Prefixes.Add("http://localhost:8081/");
            //listener.Prefixes.Add("http://127.0.0.1:8081/");

            //listener.Start();
            //while (working)
            //{
            //    try
            //    {
            //        var context = listener.GetContext();
            //        ThreadPool.QueueUserWorkItem(o => HandleRequest(context));
            //    }
            //    catch (Exception)
            //    {
            //        // Ignored for this example
            //    }
            //}

            //listener.Close();



            //TinyWeb.WebServer webServer = new TinyWeb.WebServer();

            //webServer.EndPoint = new IPEndPoint(0, 8081);
            //webServer.ProcessRequest += new TinyWeb.ProcessRequestEventHandler(this.webServer_ProcessRequest);
            //webServer.IsStarted = true;


            using (var server = new NHttp.HttpServer())
            {
                server.RequestReceived += (s, e) =>
                {
                    // The response must be written to e.Response.OutputStream.
                    // When writing text, a StreamWriter can be used.
                    string cmd = e.Request.RawUrl;


                    using (System.IO.BinaryWriter writer = new BinaryWriter(e.Response.OutputStream))
                    {
                        if (cmd == "/ver")
                        {
                            UpgradeHelper helper = new UpgradeHelper();
                            string        baseVersion;
                            string        latestVersion;
                            string[]      vers;
                            string        currentVersion;
                            helper.ShowVersion(out baseVersion, out latestVersion, out vers, out currentVersion);


                            VersionJson json = new VersionJson();
                            json.baseVersion    = baseVersion;
                            json.latestVersion  = latestVersion;
                            json.vers           = vers;
                            json.currentVersion = currentVersion;

                            string jsonString;
                            jsonString = JsonMapper.ToJson(json);

                            var data = Encoding.UTF8.GetBytes(jsonString);
                            writer.Write(data, 0, data.Length);
                        }
                        else if (cmd == "/upgrade")
                        {
                            Thread thread1 = new Thread(() =>
                            {
                                UpgradeHelper helper = new UpgradeHelper();
                                helper.Upgrade("");
                            });
                            thread1.Start();

                            var data = Encoding.UTF8.GetBytes("Launched");
                            writer.Write(data, 0, data.Length);
                        }
                        else if (cmd == "/progress")
                        {
                            string jsonString;
                            jsonString = JsonMapper.ToJson(currentProgress);
                            var data = Encoding.UTF8.GetBytes(jsonString);
                            writer.Write(data, 0, data.Length);
                        }
                    }
                };

                server.EndPoint = new IPEndPoint(IPAddress.Any, 8081);

                server.Start();
                Console.ReadKey();
            }
        }
Exemple #3
0
        void ThreadFunc()
        {
            //listener = new HttpListener();

            ////listener.Prefixes.Add("http://+:8081/");
            //listener.Prefixes.Add("http://localhost:8081/");
            //listener.Prefixes.Add("http://127.0.0.1:8081/");

            //listener.Start();
            //while (working)
            //{
            //    try
            //    {
            //        var context = listener.GetContext();
            //        ThreadPool.QueueUserWorkItem(o => HandleRequest(context));
            //    }
            //    catch (Exception)
            //    {
            //        // Ignored for this example
            //    }
            //}

            //listener.Close();



            //TinyWeb.WebServer webServer = new TinyWeb.WebServer();

            //webServer.EndPoint = new IPEndPoint(0, 8081);
            //webServer.ProcessRequest += new TinyWeb.ProcessRequestEventHandler(this.webServer_ProcessRequest);
            //webServer.IsStarted = true;

            using (var server = new NHttp.HttpServer())
            {
                server.RequestReceived += (s, e) =>
                {
                    using (System.IO.BinaryWriter writer = new BinaryWriter(e.Response.OutputStream))
                    {
                        string cmd = e.Request.RawUrl;


                        try
                        {
                            // The response must be written to e.Response.OutputStream.
                            // When writing text, a StreamWriter can be used.



                            if (cmd == "/ver")
                            {
                                UpgradeHelper helper = new UpgradeHelper();
                                string        baseVersion;
                                string        latestVersion;
                                string[]      vers;
                                string        currentVersion;
                                helper.ShowVersion(out baseVersion, out latestVersion, out vers, out currentVersion);


                                VersionJson json = new VersionJson();
                                json.baseVersion    = baseVersion;
                                json.latestVersion  = latestVersion;
                                json.vers           = vers;
                                json.currentVersion = currentVersion;
                                json.state          = "success";

                                string jsonString;
                                jsonString = JsonMapper.ToJson(json);

                                var data = Encoding.UTF8.GetBytes(jsonString);
                                writer.Write(data, 0, data.Length);
                            }
                            else if (cmd == "/upgrade")
                            {
                                string progressName = "\nu准备升级";
                                AsyncServer.Instance.SetProgress(progressName, 0);

                                StateJson json = new StateJson();
                                if (upgrading)
                                {
                                    json.state = "already launched";
                                }
                                else
                                {
                                    Thread thread1 = new Thread(() =>
                                    {
                                        upgrading            = true;
                                        UpgradeHelper helper = new UpgradeHelper();
                                        helper.Upgrade("");
                                        upgrading = false;
                                    });
                                    thread1.Start();

                                    json.state = "success";
                                }

                                string jsonString;
                                jsonString = JsonMapper.ToJson(json);
                                var data = Encoding.UTF8.GetBytes(jsonString);
                                writer.Write(data, 0, data.Length);
                            }
                            else if (cmd == "/progress")
                            {
                                string jsonString;
                                currentProgress.state = "success";
                                jsonString            = JsonMapper.ToJson(currentProgress);
                                var data = Encoding.UTF8.GetBytes(jsonString);
                                writer.Write(data, 0, data.Length);
                            }
                        }
                        catch (Exception ex)
                        {
                            AsyncServer.Instance.SetProgress("Idle", 0);

                            string jsonString = "";
                            if (cmd == "/ver")
                            {
                                VersionJson json = new VersionJson();
                                json.state = "net error";
                                jsonString = JsonMapper.ToJson(json);
                            }
                            else if (cmd == "/upgrade")
                            {
                                StateJson json = new StateJson();
                                json.state = "net error";
                                jsonString = JsonMapper.ToJson(json);
                            }
                            else if (cmd == "/progress")
                            {
                                ProgressJson json = new ProgressJson();
                                json.state = "net error";
                                jsonString = JsonMapper.ToJson(json);
                            }

                            var data = Encoding.UTF8.GetBytes(jsonString);
                            writer.Write(data, 0, data.Length);

                            Console.WriteLine("Http Request Exception:" + ex.Message);
                        }
                    }
                };

                server.EndPoint = new IPEndPoint(IPAddress.Any, 8081);

                server.Start();

                while (true)
                {
                    Thread.Sleep(1000);
                }
            }
        }