add connection logging example code
This commit is contained in:
parent
50bb60e3e5
commit
6b5665f415
1 changed files with 35 additions and 0 deletions
|
@ -2,6 +2,8 @@
|
|||
using System.IO;
|
||||
using System.Security.Cryptography.X509Certificates;
|
||||
using System.Threading.Tasks;
|
||||
using System.Net.Sockets;
|
||||
using System.Net;
|
||||
using Uwaa.HTTP.Websockets;
|
||||
using Uwaa.HTTP.Responses;
|
||||
using Uwaa.HTTP.Routing;
|
||||
|
@ -19,6 +21,12 @@ static class Program
|
|||
Router router = CreateRouter();
|
||||
HttpServer httpServer = new HttpServer(80, null, router);
|
||||
HttpServer httpsServer = new HttpServer(443, cert, router);
|
||||
|
||||
httpServer.OnConnectionBegin += LogConnect;
|
||||
httpServer.OnConnectionEnd += LogDisconnect;
|
||||
httpsServer.OnConnectionBegin += LogConnect;
|
||||
httpsServer.OnConnectionEnd += LogDisconnect;
|
||||
|
||||
_ = httpServer.Start();
|
||||
_ = httpsServer.Start();
|
||||
|
||||
|
@ -37,6 +45,7 @@ static class Program
|
|||
subpath.Add("bar", new StaticEndpoint(new OK("A \"bar\" example sub page")));
|
||||
|
||||
Router router = new Router();
|
||||
router.Add(LogRequest);
|
||||
router.Add(new CORS());
|
||||
router.Add("custom", new CustomRoute());
|
||||
router.Add("subpath", subpath);
|
||||
|
@ -47,6 +56,32 @@ static class Program
|
|||
return router;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Logs a new connection to the console.
|
||||
/// </summary>
|
||||
static void LogConnect(TcpClient client)
|
||||
{
|
||||
Console.WriteLine($"{client.Client.RemoteEndPoint} connected");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Logs a disconnection to the console.
|
||||
/// </summary>
|
||||
static void LogDisconnect(IPEndPoint endpoint)
|
||||
{
|
||||
Console.WriteLine($"{endpoint} disconnected");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Logs a request to the console.
|
||||
/// </summary>
|
||||
static Task<HttpResponse?> LogRequest(HttpRequest req)
|
||||
{
|
||||
Console.WriteLine($"{req.Endpoint.Address}: {req.Method} {req.Path}");
|
||||
|
||||
return Task.FromResult<HttpResponse?>(null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Root endpoint: /
|
||||
/// </summary>
|
||||
|
|
Loading…
Reference in a new issue