Create project files

This commit is contained in:
Dmitri Shimanski
2025-03-29 02:34:08 +03:00
commit abab2be4f4
32 changed files with 1057 additions and 0 deletions

View File

@@ -0,0 +1,16 @@
using Microsoft.AspNetCore.Http;
using yawaflua.WebSockets.Core;
using yawaflua.WebSockets.Models.Interfaces;
using WebSocketManager = yawaflua.WebSockets.Core.WebSocketManager;
namespace yawaflua.WebSockets.Models.Abstracts;
public abstract class WebSocketController : IWebSocketController
{
public IWebSocketManager WebSocketManager => new WebSocketManager();
public virtual Task OnMessageAsync(WebSocket webSocket, HttpContext httpContext)
{
return Task.CompletedTask;
}
}

View File

@@ -0,0 +1,15 @@
using System.Net.WebSockets;
using Microsoft.AspNetCore.Http;
namespace yawaflua.WebSockets.Models.Interfaces;
public interface IWebSocketClient
{
public Guid Id { get; }
public string Path { get; }
public ConnectionInfo? ConnectionInfo { get; }
public IDictionary<object, object>? Items { get; set; }
public HttpRequest? HttpRequest { get; }
internal WebSocket webSocket { get; }
public Task Abort();
}

View File

@@ -0,0 +1,9 @@
using Microsoft.AspNetCore.Http;
using yawaflua.WebSockets.Core;
namespace yawaflua.WebSockets.Models.Interfaces;
internal interface IWebSocketController
{
Task OnMessageAsync(WebSocket webSocket, HttpContext httpContext);
}

View File

@@ -0,0 +1,11 @@
using System.Linq.Expressions;
using System.Net.WebSockets;
namespace yawaflua.WebSockets.Models.Interfaces;
public interface IWebSocketManager
{
public Task Broadcast(Func<IWebSocketClient, bool> selector,string message, WebSocketMessageType messageType = WebSocketMessageType.Text, CancellationToken cts = default);
public List<IWebSocketClient> GetAllClients();
public Task SendToUser(Guid id, string message, WebSocketMessageType messageType, CancellationToken cts);
}

View File

@@ -0,0 +1,34 @@
using System.Net.WebSockets;
using Microsoft.AspNetCore.Http;
using yawaflua.WebSockets.Models.Interfaces;
namespace yawaflua.WebSockets.Models;
internal class WebSocketClient : IWebSocketClient
{
private HttpContext HttpContext { get; set; }
public Guid Id { get; } = Guid.NewGuid();
public string Path { get; }
public ConnectionInfo ConnectionInfo { get => HttpContext.Connection; }
public IDictionary<object, object> Items
{
get => HttpContext.Items;
set => HttpContext.Items = (value);
}
public HttpRequest HttpRequest { get => HttpContext.Request; }
public WebSocket webSocket { get; }
internal WebSocketClient(HttpContext httpContext, WebSocket webSocket, string path)
{
this.webSocket = webSocket;
Path = path;
this.HttpContext = httpContext;
}
public Task Abort()
{
webSocket.Abort();
return Task.CompletedTask;
}
}