WebSocketHandlerMiddleware.cs 1.3 KB

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