Пример #1
0
        public static async Task Listen(string adress = "")
        {
            if (iPAddress.Length > 0)
            {
                adress = adress.Length < 1 ? "http://" + iPAddress[0].ToString() + ":8888/":"http://" + adress + ":8888/";
            }
            HttpListener listener = new HttpListener();

            listener.Prefixes.Add(adress);
            listener.Start();
            Console.WriteLine("Ожидание подключений..." + adress);

            while (true)
            {
                HttpListenerContext context = await listener.GetContextAsync();

                HttpListenerRequest request = context.Request;

                if (request.HttpMethod == "GET")
                {
                    if (request.QueryString.AllKeys.Contains("command"))
                    {
                        switch (request.QueryString["command"])
                        {
                        case "start":
                            Audio audio = Audio.getInstance();
                            audio.AudioPlayAsync("zvon.mp3");

                            break;

                        case "addbell":
                            if (request.QueryString.AllKeys.Contains("time"))
                            {
                                Bell.bellList.Add(new Bell(request.QueryString["time"], DecodertoUTF8(request.QueryString["comment"])));
                            }
                            break;

                        case "stop":
                            return;
                        }
                    }
                }
                HttpListenerResponse response = context.Response;



                string htmlButton = "<a class='button-response' href='" + adress + "?command=start'>ЗВОНОК</a>";

                string htmlBellTable = "<table cellspacing='0' cellpadding='0'>";
                foreach (Bell bell in Bell.bellList)
                {
                    htmlBellTable += "<tr>";
                    htmlBellTable += "<td>" + bell.num + "</td><td>"
                                     + bell.time + "</td><td>"
                                     + bell.comment + "</td>";
                    htmlBellTable += "</tr>";
                }
                htmlBellTable += "</table>";

                string htmlAddBellForm = "<form action='" + adress + "addbell' method='GET'>" +
                                         "<input type = 'hidden' name = 'command' value = 'addbell'>" +
                                         "<div><label>Добавить время для звонка:</label><input type = 'time' name = 'time'></div>" +
                                         "<div><label>Комментарий:</label><input type = 'text' name = 'comment' value=''></div>" +
                                         "<input type = 'hidden' name = 'pass' value = 'DkmfRTY610'>" +
                                         "<input type = 'submit' value = 'Добавить'>" +
                                         "</form>";


                responseString = htmlHead + htmlButton + htmlBellTable + htmlAddBellForm + "</body></html>";
                byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
                response.ContentLength64 = buffer.Length;
                Stream output = response.OutputStream;
                output.Write(buffer, 0, buffer.Length);
                output.Close();
            }
        }