parent
71ed840944
commit
fb1de5a921
@ -0,0 +1,36 @@
|
|||||||
|
using System.Threading.Tasks;
|
||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using WebSocketManager = Emby.Server.Implementations.WebSockets.WebSocketManager;
|
||||||
|
|
||||||
|
namespace Emby.Server.Implementations.Middleware
|
||||||
|
{
|
||||||
|
public class WebSocketMiddleware
|
||||||
|
{
|
||||||
|
private readonly RequestDelegate _next;
|
||||||
|
private readonly ILogger<WebSocketMiddleware> _logger;
|
||||||
|
private readonly WebSocketManager _webSocketManager;
|
||||||
|
|
||||||
|
public WebSocketMiddleware(RequestDelegate next, ILogger<WebSocketMiddleware> logger, WebSocketManager webSocketManager)
|
||||||
|
{
|
||||||
|
_next = next;
|
||||||
|
_logger = logger;
|
||||||
|
_webSocketManager = webSocketManager;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task Invoke(HttpContext httpContext)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Handling request: " + httpContext.Request.Path);
|
||||||
|
|
||||||
|
if (httpContext.WebSockets.IsWebSocketRequest)
|
||||||
|
{
|
||||||
|
var webSocketContext = await httpContext.WebSockets.AcceptWebSocketAsync(null).ConfigureAwait(false);
|
||||||
|
_webSocketManager.AddSocket(webSocketContext);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
await _next.Invoke(httpContext);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,7 @@
|
|||||||
|
namespace Emby.Server.Implementations.WebSocket
|
||||||
|
{
|
||||||
|
public class WebSocketManager
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,22 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Concurrent;
|
||||||
|
using System.Net.WebSockets;
|
||||||
|
|
||||||
|
namespace Emby.Server.Implementations.WebSockets
|
||||||
|
{
|
||||||
|
public class WebSocketManager
|
||||||
|
{
|
||||||
|
private readonly ConcurrentDictionary<Guid, WebSocket> _activeWebSockets;
|
||||||
|
|
||||||
|
public WebSocketManager()
|
||||||
|
{
|
||||||
|
_activeWebSockets = new ConcurrentDictionary<Guid, WebSocket>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void AddSocket(WebSocket webSocket)
|
||||||
|
{
|
||||||
|
var guid = Guid.NewGuid();
|
||||||
|
_activeWebSockets.TryAdd(guid, webSocket);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue