Skip to content

Instantly share code, notes, and snippets.

@dcomartin
Created January 3, 2019 02:28
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save dcomartin/d8a71b4fb19541f8502f615aa4360b9e to your computer and use it in GitHub Desktop.
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.SignalR;
namespace Practical.AspNetCore.SignalR
{
public class MessageHub : 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 SendMessageToUser(string connectionId, string message)
{
return Clients.Client(connectionId).SendAsync("ReceiveMessage", message);
}
public Task JoinGroup(string group)
{
return Groups.AddToGroupAsync(Context.ConnectionId, group);
}
public Task SendMessageToGroup(string group, string message)
{
return Clients.Group(group).SendAsync("ReceiveMessage", message);
}
public override async Task OnConnectedAsync()
{
await Clients.All.SendAsync("UserConnected", Context.ConnectionId);
await base.OnConnectedAsync();
}
public override async Task OnDisconnectedAsync(Exception ex)
{
await Clients.All.SendAsync("UserDisconnected", Context.ConnectionId);
await base.OnDisconnectedAsync(ex);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment