using CTS.Signalr.Server.Cores;
using CTS.Signalr.Server.Dtos;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.Logging;
using System;
using System.Linq;
using System.Threading.Tasks;
namespace CTS.Signalr.Server.Hubs
{
///
/// 服务端接口
///
public interface IServerNotifyHub
{
}
///
/// 客户端使用的接口
///
public interface IClientNotifyHub
{
Task OnNotify(object data);
Task OnLine(object data);
Task OffLine(object data);
}
[Authorize]
public class NotifyHub : Hub,IServerNotifyHub
{
private readonly SignalrRedisHelper _signalrRedisHelper;
private readonly ILogger _logger;
public NotifyHub(SignalrRedisHelper signalrRedisHelper, ILogger logger)
{
_signalrRedisHelper = signalrRedisHelper;
_logger = logger;
}
public override async Task OnConnectedAsync()
{
//await Clients.All.OnNotify(new { UserId= Context.User.Identity.Name, Name=Context.User.Identity.Name, ConnectId = Context.ConnectionId });
var userId= Context.User.Identity.Name;
var groups=Context.GetHttpContext().Request.Query["group"].FirstOrDefault();
_logger.LogDebug($"OnConnectedAsync----userId:{userId},groups:{groups},connectionId:{ Context.ConnectionId}");
if (!string.IsNullOrWhiteSpace(userId))
{
await _signalrRedisHelper.AddConnectForUserAsync(userId, Context.ConnectionId);
await JoinToGroup(userId, Context.ConnectionId, groups?.Split(','));
await DealOnLineNotify(userId, Context.ConnectionId);
}
await base.OnConnectedAsync();
}
public override async Task OnDisconnectedAsync(Exception exception)
{
var userId = Context.User.Identity.Name;
var groups = Context.GetHttpContext().Request.Query["group"].FirstOrDefault();
_logger.LogDebug($"OnDisconnectedAsync----userId:{userId},groups:{groups},connectionId:{ Context.ConnectionId}");
if (!string.IsNullOrWhiteSpace(userId))
{
await _signalrRedisHelper.RemoveConnectForUserAsync(userId, Context.ConnectionId);
await DealOffLineNotify(userId,Context.ConnectionId);
}
await LeaveFromGroup(Context.ConnectionId, groups?.Split(','));
await base.OnDisconnectedAsync(exception);
}
///
/// 加入组
///
///
///
private async Task JoinToGroup(string userId,string connectionId,params string[] groups)
{
if (!string.IsNullOrWhiteSpace(userId)&& groups!=null&&groups.Length>0)
{
foreach (var group in groups)
{
await Groups.AddToGroupAsync(connectionId, group);
await _signalrRedisHelper.AddUserForGroupAsync(group, connectionId, userId);
// await Clients.Group(group).OnJoinGroup(new { ConnectId = connectionId, UserId = userId, GroupName = group });
}
}
}
///
/// 从组中移除
///
///
///
private async Task LeaveFromGroup(string connectionId,params string[] groups)
{
if (groups != null && groups.Length > 0)
{
foreach (var group in groups)
{
await Groups.RemoveFromGroupAsync(connectionId, group);
await _signalrRedisHelper.RemoveConnectFromGroupAsync(group,connectionId);
// await Clients.Group(group).OnLeaveGroup(new { ConnectId = connectionId, GroupName = group });
}
}
}
///
/// 处理上线通知(只有用户第一个连接才通知)
///
///
///
///
private async Task DealOnLineNotify(string userId,string connectionId)
{
var userConnectCount = await _signalrRedisHelper.GetConnectsCountByUserAsync(userId);
await Clients.All.OnLine(new OnLineData()
{
UserId = userId,
ConnectionId = connectionId,
IsFirst = userConnectCount == 1
});
}
///
/// 处理下线通知(只有当用户一个连接都没了 才算下线)
///
///
///
///
private async Task DealOffLineNotify(string userId,string connectionId)
{
var userConnectCount = await _signalrRedisHelper.GetConnectsCountByUserAsync(userId);
await Clients.All.OffLine(new OffLineData()
{
UserId = userId,
ConnectionId = connectionId,
IsLast = userConnectCount == 0
});
}
}
}
{
GroupIds:'', // [可空] 组id集合,多个用,隔开
UserIds:'',// [可空] 用户id集合,多个用,隔开
ExcludeUsers:boolean, // 是否排除用户列表中的用户
NotifyObj:Object // 通知的对象,任意类型(总大小不要超过36k)
}
* ExcludeUsers=false
推送给组中指定(UserIds中指定的)的这些用户
* ExcludeUsers=false
推送给指定用户(UserIds中指定的用户)
{
Connects:'', // 连接Id集合,多个用,隔开
NotifyObj:Object // 通知的对象,任意类型(总大小不要超过36k)
}
为了方便分析和定位问题,使用log4net来作为日志记录器。
public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHsts();
loggerFactory.AddLog4Net();
...
}