WebSocketHandlerMiddleware.cs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. using System.Threading.Tasks;
  2. using MediaBrowser.Controller.Net;
  3. using Microsoft.AspNetCore.Http;
  4. namespace Jellyfin.Api.Middleware;
  5. /// <summary>
  6. /// Handles WebSocket requests.
  7. /// </summary>
  8. public class WebSocketHandlerMiddleware
  9. {
  10. private readonly RequestDelegate _next;
  11. /// <summary>
  12. /// Initializes a new instance of the <see cref="WebSocketHandlerMiddleware"/> class.
  13. /// </summary>
  14. /// <param name="next">The next delegate in the pipeline.</param>
  15. public WebSocketHandlerMiddleware(RequestDelegate next)
  16. {
  17. _next = next;
  18. }
  19. /// <summary>
  20. /// Executes the middleware action.
  21. /// </summary>
  22. /// <param name="httpContext">The current HTTP context.</param>
  23. /// <param name="webSocketManager">The WebSocket connection manager.</param>
  24. /// <returns>The async task.</returns>
  25. public async Task Invoke(HttpContext httpContext, IWebSocketManager webSocketManager)
  26. {
  27. if (!httpContext.WebSockets.IsWebSocketRequest)
  28. {
  29. await _next(httpContext).ConfigureAwait(false);
  30. return;
  31. }
  32. await webSocketManager.WebSocketRequestHandler(httpContext).ConfigureAwait(false);
  33. }
  34. }