using Microsoft.AspNetCore.SignalR; using System.Security.Claims; using System.Threading.Tasks; public class MyHub : Hub { public override async Task OnConnectedAsync() { var userId = Context.User.FindFirstValue(ClaimTypes.NameIdentifier); await Groups.AddToGroupAsync(Context.ConnectionId, userId); await base.OnConnectedAsync(); } } //////////////////////////////////////////////////////////////////// public async Task SendMessageToUser(string userId, string message) { await Clients.Group(userId).SendAsync("ReceiveMessage", message); }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter