CryptoHubService.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. using Application.Abstractions.Crypto;
  2. using Microsoft.AspNetCore.SignalR;
  3. namespace Web.Api.Hubs;
  4. public sealed class CryptoHubService(
  5. IHubContext<CryptoHub, ICryptoHubClient> hubContext
  6. ) : ICryptoHubService
  7. {
  8. public async Task SendTickerAsync(CryptoHubData.TickerData ticker, CancellationToken ct)
  9. {
  10. await hubContext.Clients.Group($"ticker:{ticker.Market.ToLower()}").ReceiveTicker(ticker);
  11. }
  12. public async Task SendTickersAsync(IReadOnlyList<CryptoHubData.TickerData> tickers, CancellationToken ct)
  13. {
  14. if (tickers.Count == 0)
  15. {
  16. return;
  17. }
  18. var quote = tickers[0].Market.Split('-')[0].ToLower();
  19. await hubContext.Clients.Group($"tickers:{quote}").ReceiveTickers(tickers);
  20. await hubContext.Clients.Group("tickers").ReceiveTickers(tickers);
  21. }
  22. public async Task SendTradeAsync(CryptoHubData.TradeData trade, CancellationToken ct)
  23. {
  24. await hubContext.Clients.Group($"trade:{trade.Market.ToLower()}").ReceiveTrade(trade);
  25. }
  26. public async Task SendOrderbookAsync(CryptoHubData.OrderbookData orderbook, CancellationToken ct)
  27. {
  28. await hubContext.Clients.Group($"orderbook:{orderbook.Market.ToLower()}").ReceiveOrderbook(orderbook);
  29. }
  30. public async Task SendCandleAsync(CryptoHubData.CandleData candle, CancellationToken ct)
  31. {
  32. await hubContext.Clients.Group($"candle:{candle.Market.ToLower()}").ReceiveCandle(candle);
  33. }
  34. }