mirror of
https://github.com/yawaflua/WebSockets.git
synced 2025-12-16 01:46:19 +02:00
adds comments, OOP incapsulation and publish package to nuget
This commit is contained in:
24
yawaflua.WebSockets/Models/Interfaces/IWebSocket.cs
Normal file
24
yawaflua.WebSockets/Models/Interfaces/IWebSocket.cs
Normal file
@@ -0,0 +1,24 @@
|
||||
using System.Net.WebSockets;
|
||||
|
||||
namespace yawaflua.WebSockets.Models.Interfaces;
|
||||
|
||||
public interface IWebSocket : IDisposable
|
||||
{
|
||||
WebSocketState State { get; }
|
||||
WebSocketCloseStatus? CloseStatus { get; }
|
||||
string? SubProtocol { get; }
|
||||
string? CloseStatusDescription { get; }
|
||||
string? Message { get; }
|
||||
WebSocketMessageType? MessageType { get; }
|
||||
IWebSocketClient Client { get; }
|
||||
|
||||
Task SendAsync(string message,
|
||||
WebSocketMessageType messageType = WebSocketMessageType.Text,
|
||||
CancellationToken cancellationToken = default);
|
||||
|
||||
Task CloseAsync(WebSocketCloseStatus closeStatus = WebSocketCloseStatus.NormalClosure,
|
||||
string? reason = null,
|
||||
CancellationToken cancellationToken = default);
|
||||
|
||||
void Abort();
|
||||
}
|
||||
@@ -5,11 +5,33 @@ namespace yawaflua.WebSockets.Models.Interfaces;
|
||||
|
||||
public interface IWebSocketClient
|
||||
{
|
||||
/// <summary>
|
||||
/// ID of user
|
||||
/// </summary>
|
||||
public Guid Id { get; }
|
||||
/// <summary>
|
||||
/// Path, that user connects
|
||||
/// </summary>
|
||||
public string Path { get; }
|
||||
/// <summary>
|
||||
/// Connection info
|
||||
/// </summary>
|
||||
public ConnectionInfo? ConnectionInfo { get; }
|
||||
/// <summary>
|
||||
/// You can provides Items, like in auth middleware or any
|
||||
/// </summary>
|
||||
public IDictionary<object, object>? Items { get; set; }
|
||||
/// <summary>
|
||||
/// HttpRequest data
|
||||
/// </summary>
|
||||
public HttpRequest? HttpRequest { get; }
|
||||
/// <summary>
|
||||
/// You should`nt use it, but, its full work with user
|
||||
/// </summary>
|
||||
internal WebSocket webSocket { get; }
|
||||
/// <summary>
|
||||
/// Kick client
|
||||
/// </summary>
|
||||
/// <returns>null</returns>
|
||||
public Task Abort();
|
||||
}
|
||||
@@ -5,5 +5,11 @@ namespace yawaflua.WebSockets.Models.Interfaces;
|
||||
|
||||
internal interface IWebSocketController
|
||||
{
|
||||
Task OnMessageAsync(WebSocket webSocket, HttpContext httpContext);
|
||||
/// <summary>
|
||||
/// Example of working with IWebSocketController
|
||||
/// </summary>
|
||||
/// <param name="webSocket"></param>
|
||||
/// <param name="httpContext"></param>
|
||||
/// <returns></returns>
|
||||
Task OnMessageAsync(IWebSocket webSocket, HttpContext httpContext);
|
||||
}
|
||||
@@ -5,7 +5,28 @@ namespace yawaflua.WebSockets.Models.Interfaces;
|
||||
|
||||
public interface IWebSocketManager
|
||||
{
|
||||
/// <summary>
|
||||
/// Broadcast message to all users
|
||||
/// </summary>
|
||||
/// <param name="selector">selector of users</param>
|
||||
/// <param name="message">message, broadcasted to all users</param>
|
||||
/// <param name="messageType">type of message</param>
|
||||
/// <param name="cts">cancellation token</param>
|
||||
/// <returns></returns>
|
||||
public Task Broadcast(Func<IWebSocketClient, bool> selector,string message, WebSocketMessageType messageType = WebSocketMessageType.Text, CancellationToken cts = default);
|
||||
/// <summary>
|
||||
/// Provides list of clients connected to host
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public List<IWebSocketClient> GetAllClients();
|
||||
/// <summary>
|
||||
/// Send to specific user data
|
||||
/// Analog of <c>Broadcast(k => k.Id == ID, message)</c>
|
||||
/// </summary>
|
||||
/// <param name="id">Id of user</param>
|
||||
/// <param name="message">message to user</param>
|
||||
/// <param name="messageType">type of message</param>
|
||||
/// <param name="cts">cancellation token</param>
|
||||
/// <returns></returns>
|
||||
public Task SendToUser(Guid id, string message, WebSocketMessageType messageType, CancellationToken cts);
|
||||
}
|
||||
Reference in New Issue
Block a user