using System.Threading.Tasks; using Microsoft.AspNet.SignalR; namespace BasicChat { [Authorize] public class ChatHub : Hub { private readonly static ConnectionMapping _connections = new ConnectionMapping(); public void SendChatMessage(string who, string message) { string name = Context.User.Identity.Name; foreach (var connectionId in _connections.GetConnections(who)) { Clients.Client(connectionId).addChatMessage(name + ": " + message); } } public override Task OnConnected() { string name = Context.User.Identity.Name; _connections.Add(name, Context.ConnectionId); return base.OnConnected(); } public override Task OnDisconnected() { string name = Context.User.Identity.Name; _connections.Remove(name, Context.ConnectionId); return base.OnDisconnected(); } public override Task OnReconnected() { string name = Context.User.Identity.Name; if (!_connections.GetConnections(name).Contains(Context.ConnectionId)) { _connections.Add(name, Context.ConnectionId); } return base.OnReconnected(); } } }