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

33 lines
924 B
C#
Raw Normal View History

2018-03-30 18:34:42 +08:00
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);
}
2018-04-03 00:19:32 +08:00
public override async Task OnConnectedAsync()
2018-03-30 18:34:42 +08:00
{
2018-04-02 21:20:32 +08:00
await Groups.AddAsync(Context.User.Identity.Name, "SignalR Users");
2018-04-03 00:19:32 +08:00
await base.OnConnectedAsync();
2018-03-30 18:34:42 +08:00
}
}
}