aspnetcore/samples/SignalRSamples/Hubs/Chat.cs

67 lines
2.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;
using System.Threading.Tasks;
using Microsoft.AspNetCore.SignalR;
namespace SignalRSamples.Hubs
{
public class Chat : Hub
{
public override async Task OnConnectedAsync()
{
await Clients.All.SendAsync("Send", $"{Context.ConnectionId} joined");
}
public override async Task OnDisconnectedAsync(Exception ex)
{
await Clients.Others.SendAsync("Send", $"{Context.ConnectionId} left");
}
public Task Send(string message)
{
return Clients.All.SendAsync("Send", $"{Context.ConnectionId}: {message}");
}
public Task SendToOthers(string message)
{
return Clients.Others.SendAsync("Send", $"{Context.ConnectionId}: {message}");
}
public Task SendToConnection(string connectionId, string message)
{
return Clients.Client(connectionId).SendAsync("Send", $"Private message from {Context.ConnectionId}: {message}");
}
public Task SendToGroup(string groupName, string message)
{
return Clients.Group(groupName).SendAsync("Send", $"{Context.ConnectionId}@{groupName}: {message}");
}
public Task SendToOthersInGroup(string groupName, string message)
{
return Clients.OthersInGroup(groupName).SendAsync("Send", $"{Context.ConnectionId}@{groupName}: {message}");
}
public async Task JoinGroup(string groupName)
{
await Groups.AddToGroupAsync(Context.ConnectionId, groupName);
await Clients.Group(groupName).SendAsync("Send", $"{Context.ConnectionId} joined {groupName}");
}
public async Task LeaveGroup(string groupName)
{
await Clients.Group(groupName).SendAsync("Send", $"{Context.ConnectionId} left {groupName}");
await Groups.RemoveFromGroupAsync(Context.ConnectionId, groupName);
}
public Task Echo(string message)
{
return Clients.Caller.SendAsync("Send", $"{Context.ConnectionId}: {message}");
}
}
}