HttpServer.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. using BeetleX;
  2. using BeetleX.EventArgs;
  3. using Microsoft.Extensions.Hosting;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Text;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. namespace PlatformBenchmarks
  10. {
  11. public class HttpServer : IHostedService
  12. {
  13. private IServer mApiServer;
  14. public virtual async Task StartAsync(CancellationToken cancellationToken)
  15. {
  16. ArraySegment<byte> date = GMTDate.Default.DATE;
  17. ServerOptions serverOptions = new ServerOptions();
  18. serverOptions.LogLevel = LogType.Error;
  19. serverOptions.DefaultListen.Port = 8080;
  20. serverOptions.Statistical = false;
  21. serverOptions.BufferSize = 2048;
  22. serverOptions.BufferPoolMaxMemory = 1000;
  23. serverOptions.BufferPoolSize = 1024 * 4;
  24. mApiServer = SocketFactory.CreateTcpServer<HttpHandler>(serverOptions);
  25. mApiServer.Open();
  26. System.Net.Http.HttpClient client = new System.Net.Http.HttpClient();
  27. var response = await client.GetAsync("http://localhost:8080/json");
  28. mApiServer.Log(LogType.Info, null, $"Get josn {response.StatusCode}");
  29. response = await client.GetAsync("http://localhost:8080/plaintext");
  30. mApiServer.Log(LogType.Info, null, $"Get plaintext {response.StatusCode}");
  31. mApiServer.Log(LogType.Info, null, $"Debug mode [{Program.Debug}]");
  32. }
  33. public virtual Task StopAsync(CancellationToken cancellationToken)
  34. {
  35. mApiServer.Dispose();
  36. return Task.CompletedTask;
  37. }
  38. }
  39. }