AspNetCore.Docs/aspnetcore/signalr/hubs/sample/ChatHub.cs

33 lines
924 B
C#

using Microsoft.AspNetCore.SignalR;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace SignalRChat
{
public class ChatHub : Hub
{
public Task SendMessageToAll(string message)
{
return Clients.All.SendAsync("ReceiveMessage", message);
}
public Task SendMessageToCaller(string message)
{
return Clients.Caller.SendAsync("ReceiveMessage", message);
}
public Task SendMessageToGroups(string user, string message)
{
List<string> groups = new List<string>() { "SignalR Users" };
return Clients.Groups(groups).SendAsync("ReceiveMessage", message);
}
public override async Task OnConnectedAsync()
{
await Groups.AddAsync(Context.User.Identity.Name, "SignalR Users");
await base.OnConnectedAsync();
}
}
}