aspnetcore/samples/ChatSample/Hubs/Chat.cs

40 lines
1.2 KiB
C#

// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.SignalR;
namespace ChatSample.Hubs
{
[Authorize]
public class Chat : HubWithPresence
{
public Chat(IUserTracker<Chat> userTracker)
: base(userTracker)
{
}
public override async Task OnConnectedAsync()
{
await Clients.Client(Context.ConnectionId).SendAsync("SetUsersOnline", await GetUsersOnline());
await base.OnConnectedAsync();
}
public override Task OnUsersJoined(UserDetails[] users)
{
return Clients.Client(Context.ConnectionId).SendAsync("UsersJoined", new[] { users });
}
public override Task OnUsersLeft(UserDetails[] users)
{
return Clients.Client(Context.ConnectionId).SendAsync("UsersLeft", new[] { users });
}
public async Task Send(string message)
{
await Clients.All.SendAsync("Send", Context.User.Identity.Name, message);
}
}
}