|
- using System;
- using System.IO;
- using System.Net;
-
- namespace TBConsole
- {
- public class WebServer
- {
- private bool _running;
- private readonly HttpListener _listener = new HttpListener();
- private Func<string, string> _receiver;
- public WebServer(Func<string, string> receiver) => _receiver = receiver;
-
- public void Start()
- {
- _running = true;
- KeepListening();
- }
-
- public void Stop()
- {
- _running = false;
- _listener.Stop();
- }
-
- private async void KeepListening()
- {
- _listener.Prefixes.Add("http://localhost:8019/");
- _listener.Start();
- while (_running)
- {
- var context = await _listener.GetContextAsync();
- string resp = _receiver(await new StreamReader(context.Request.InputStream).ReadToEndAsync());
- var sw = new StreamWriter(context.Response.OutputStream);
- await sw.WriteLineAsync(resp);
- sw.Close();
- }
- }
- }
- }
|