All the code
This commit is contained in:
43
Gameboard.ShogiUI.Sockets/Extensions/LogMiddleware.cs
Normal file
43
Gameboard.ShogiUI.Sockets/Extensions/LogMiddleware.cs
Normal file
@@ -0,0 +1,43 @@
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Gameboard.ShogiUI.Sockets.Extensions
|
||||
{
|
||||
public class LogMiddleware
|
||||
{
|
||||
private readonly RequestDelegate next;
|
||||
private readonly ILogger logger;
|
||||
|
||||
public LogMiddleware(RequestDelegate next, ILoggerFactory factory)
|
||||
{
|
||||
this.next = next;
|
||||
logger = factory.CreateLogger<LogMiddleware>();
|
||||
}
|
||||
|
||||
public async Task Invoke(HttpContext context)
|
||||
{
|
||||
try
|
||||
{
|
||||
await next(context);
|
||||
}
|
||||
finally
|
||||
{
|
||||
logger.LogInformation("Request {method} {url} => {statusCode}",
|
||||
context.Request?.Method,
|
||||
context.Request?.Path.Value,
|
||||
context.Response?.StatusCode);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static class IApplicationBuilderExtensions
|
||||
{
|
||||
public static IApplicationBuilder UseRequestResponseLogging(this IApplicationBuilder builder)
|
||||
{
|
||||
builder.UseMiddleware<LogMiddleware>();
|
||||
return builder;
|
||||
}
|
||||
}
|
||||
}
|
||||
24
Gameboard.ShogiUI.Sockets/Extensions/WebSocketExtensions.cs
Normal file
24
Gameboard.ShogiUI.Sockets/Extensions/WebSocketExtensions.cs
Normal file
@@ -0,0 +1,24 @@
|
||||
using System;
|
||||
using System.Net.WebSockets;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace AspShogiSockets.Extensions
|
||||
{
|
||||
public static class WebSocketExtensions
|
||||
{
|
||||
public static async Task SendTextAsync(this WebSocket self, string message)
|
||||
{
|
||||
await self.SendAsync(Encoding.UTF8.GetBytes(message), WebSocketMessageType.Text, true, CancellationToken.None);
|
||||
}
|
||||
|
||||
public static async Task<string> ReceiveTextAsync(this WebSocket self)
|
||||
{
|
||||
var buffer = new ArraySegment<byte>(new byte[2048]);
|
||||
var receive = await self.ReceiveAsync(buffer, CancellationToken.None);
|
||||
return Encoding.UTF8.GetString(buffer.Slice(0, receive.Count));
|
||||
// TODO: Make this robust to multi-frame messages.
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user