Skip to content

Instantly share code, notes, and snippets.

@tocalai
Created September 5, 2019 08:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tocalai/e63b99591d62035a5f9e9933fea620fa to your computer and use it in GitHub Desktop.
Save tocalai/e63b99591d62035a5f9e9933fea620fa to your computer and use it in GitHub Desktop.
Demonstrate sending message to specific client(s) according group.
using Microsoft.AspNetCore.SignalR;
using Newtonsoft.Json;
using SignalR.Lab.Web.Hubs;
using SignalR.Lab.Web.Models;
using System;
using System.Timers;
namespace SignalR.Lab.Web.Services
{
public class EventReceiverService
{
// .....
private void MockEventGenTimer_Elapsed(object sender, ElapsedEventArgs e)
{
_mockEventGenTimer.Enabled = false;
var mockEvent = new EventModel()
{
Content = $"{new Random().Next()}"
};
// target to line1 or line2 or line3
var target = new Random().Next(1, 4);
       // implement and invoke send message to group   
_eventHub.Clients.Group($"Line{target}").SendAsync(nameof(IEventHub.SendMessageToGroup),
$"Line{target}", JsonConvert.SerializeObject(mockEvent)).GetAwaiter().GetResult();
_mockEventGenTimer.Enabled = true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment