111
This commit is contained in:
26
Service/Application.Web/Plug/ChatHub/ChatHub.cs
Normal file
26
Service/Application.Web/Plug/ChatHub/ChatHub.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
namespace Application.Web
|
||||
{
|
||||
public class ChatHub:Hub
|
||||
{
|
||||
// 客户端调用:发送消息给所有人
|
||||
public async Task SendMessage(string user, string message)
|
||||
{
|
||||
// 推送给所有客户端(Clients.All)
|
||||
await Clients.All.SendAsync("ReceiveMessage", user, message);
|
||||
}
|
||||
|
||||
// 可选:连接/断开事件
|
||||
public override async Task OnConnectedAsync()
|
||||
{
|
||||
await Clients.All.SendAsync("UserConnected", Context.ConnectionId);
|
||||
await base.OnConnectedAsync();
|
||||
}
|
||||
|
||||
public override async Task OnDisconnectedAsync(Exception? exception)
|
||||
{
|
||||
await Clients.All.SendAsync("UserDisconnected", Context.ConnectionId);
|
||||
await base.OnDisconnectedAsync(exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user