aspnetcore/samples/ChatSample/Hubs/Chat.cs

48 lines
1.4 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.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
namespace ChatSample.Hubs
{
// TODO: Make this work
[Authorize]
public class Chat : HubWithPresence
{
public Chat(IUserTracker<Chat> userTracker)
: base(userTracker)
{
}
public override async Task OnConnectedAsync()
{
if (!Context.User.Identity.IsAuthenticated)
{
Context.Connection.Dispose();
return;
}
await Clients.Client(Context.ConnectionId).InvokeAsync("SetUsersOnline", await GetUsersOnline());
await base.OnConnectedAsync();
}
public override Task OnUsersJoined(UserDetails[] users)
{
return Clients.Client(Context.ConnectionId).InvokeAsync("UsersJoined", new[] { users });
}
public override Task OnUsersLeft(UserDetails[] users)
{
return Clients.Client(Context.ConnectionId).InvokeAsync("UsersLeft", new[] { users });
}
public async Task Send(string message)
{
await Clients.All.InvokeAsync("Send", Context.User.Identity.Name, message);
}
}
}