namespace Flawless.Server.Middlewares; public class WebSocketHandoffMiddleware(RequestDelegate next) { public async Task InvokeAsync(HttpContext context) { // Is a WebSocket call and no ws presents. if (context.Request.Path.StartsWithSegments("/ws") && !context.WebSockets.IsWebSocketRequest) throw new InvalidOperationException("Connection is not a WebSocket!"); await next(context); } } public static class WebSocketHandoffMiddlewareExtensions { public static IApplicationBuilder UseWebSocketHandoffMiddleware(this IApplicationBuilder builder) { return builder.UseMiddleware(); } }