39 lines
982 B
C#
39 lines
982 B
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace JSMachine.WMS.Business.SSE
|
|
{
|
|
public static class SseEngine
|
|
{
|
|
private static readonly List<ClientConnection> _clients = [];
|
|
|
|
public static void AddClient(ClientConnection client)
|
|
{
|
|
_clients.Add(client);
|
|
}
|
|
|
|
public static void RemoveClient(ClientConnection client)
|
|
{
|
|
_clients.Remove(client);
|
|
}
|
|
|
|
public static void BrodCast(string message)
|
|
{
|
|
Task.Run(() =>
|
|
{
|
|
|
|
foreach (var client in _clients)
|
|
{
|
|
var data = $"data: {message}\n\n";
|
|
var bytes = Encoding.UTF8.GetBytes(data);
|
|
client.Response.Body.WriteAsync(bytes, 0, bytes.Length);
|
|
client.Response.Body.FlushAsync();
|
|
}
|
|
});
|
|
}
|
|
}
|
|
}
|