mirror of
https://github.com/yawaflua/WebSockets.git
synced 2026-02-04 14:04:11 +02:00
Create project files
This commit is contained in:
34
yawaflua.WebSockets/Core/WebSocketManager.cs
Normal file
34
yawaflua.WebSockets/Core/WebSocketManager.cs
Normal file
@@ -0,0 +1,34 @@
|
||||
using System.Linq.Expressions;
|
||||
using System.Net.WebSockets;
|
||||
using System.Text;
|
||||
using yawaflua.WebSockets.Models.Interfaces;
|
||||
|
||||
namespace yawaflua.WebSockets.Core;
|
||||
|
||||
internal class WebSocketManager : IWebSocketManager
|
||||
{
|
||||
public async Task Broadcast(Func<IWebSocketClient, bool> selector, string message,
|
||||
WebSocketMessageType messageType = WebSocketMessageType.Text, CancellationToken cts = default)
|
||||
{
|
||||
foreach (var client in WebSocketRouter.Clients.Where(selector))
|
||||
{
|
||||
await client.webSocket.SendAsync(Encoding.UTF8.GetBytes(message),
|
||||
messageType,
|
||||
true,
|
||||
cts);
|
||||
}
|
||||
}
|
||||
|
||||
public List<IWebSocketClient> GetAllClients()
|
||||
{
|
||||
return WebSocketRouter.Clients;
|
||||
}
|
||||
|
||||
public async Task SendToUser(Guid id, string message, WebSocketMessageType messageType = WebSocketMessageType.Text, CancellationToken cts = default)
|
||||
{
|
||||
await WebSocketRouter.Clients.First(k => k.Id == id).webSocket.SendAsync(Encoding.UTF8.GetBytes(message),
|
||||
messageType,
|
||||
true,
|
||||
cts);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user