Skip to content

Commit

Permalink
midsave
Browse files Browse the repository at this point in the history
  • Loading branch information
SpazElectro committed Sep 5, 2023
1 parent 9f002e4 commit 63e3f77
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions experiments/adminpanel/WebSocketHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,20 @@ public async Task Start()
}
}

public delegate Task BroadcastDelegate(WebSocket client);
public async Task Broadcast(BroadcastDelegate delg)
{
foreach (var client in _clients.Values)
{
if (client.State == WebSocketState.Open)
{
await delg(client);
// await client.SendAsync(msg,
// WebSocketMessageType.Text, true, CancellationToken.None);
}
}
}

private async Task HandleWebSocketAsync(HttpListenerContext context)
{
if (context.Request.IsWebSocketRequest)
Expand Down Expand Up @@ -99,17 +113,7 @@ private async Task HandleWebSocketAsync(HttpListenerContext context)
Console.WriteLine($"Content: {content}");
if (type == "message")
{
// Broadcast the message to all connected clients
foreach (var client in _clients.Values)
{
if (client.State == WebSocketState.Open)
{
await client.SendAsync(StringToArraySegment("message:User: " + content),
WebSocketMessageType.Text, true, CancellationToken.None);
}
}
}
await Broadcast(async (client) => await client.SendAsync(StringToArraySegment("message:User: " + content), WebSocketMessageType.Text, true, CancellationToken.None));
}
}
}
Expand Down

0 comments on commit 63e3f77

Please sign in to comment.