mirror of
https://github.com/yawaflua/WebSockets.git
synced 2026-02-04 05:54:12 +02:00
Create project files
This commit is contained in:
16
yawaflua.WebSockets/Models/Abstracts/WebSocketController.cs
Normal file
16
yawaflua.WebSockets/Models/Abstracts/WebSocketController.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
15
yawaflua.WebSockets/Models/Interfaces/IWebSocketClient.cs
Normal file
15
yawaflua.WebSockets/Models/Interfaces/IWebSocketClient.cs
Normal 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();
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
11
yawaflua.WebSockets/Models/Interfaces/IWebSocketManager.cs
Normal file
11
yawaflua.WebSockets/Models/Interfaces/IWebSocketManager.cs
Normal 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);
|
||||
}
|
||||
34
yawaflua.WebSockets/Models/WebSocketClient.cs
Normal file
34
yawaflua.WebSockets/Models/WebSocketClient.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user