## Hub的管理
- 重写OnConnectedAsync
从连接信息中获取UserId、Groups,ConnectId,并实现这三者的关系,存放于redis中
[代码请查看](https://github.com/xiexingen/CTS.Signalr/blob/master/CTS.Signalr.Server/Hubs/NotifyHub.cs)
``` C#
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
});
}
}
}
```
## 提供给业务系统调用的API
- [POST] api/notify/post
application/json形式 提交,数据格式如下
``` json
{
GroupIds:‘‘, // [可空] 组id集合,多个用,隔开
UserIds:‘‘,// [可空] 用户id集合,多个用,隔开
ExcludeUsers:boolean, // 是否排除用户列表中的用户
NotifyObj:Object // 通知的对象,任意类型(总大小不要超过36k)
}
```
* 有GroupIds
* ExcludeUsers=true
推送给指定的组中所有用户(排除掉UserIds部分)
* ExcludeUsers=false
推送给组中指定(UserIds中指定的)的这些用户
* 无GroupIds
* ExcludeUsers=true
推送给当前所有连接(排除掉UserIds部分的用户)
* ExcludeUsers=false
推送给指定用户(UserIds中指定的用户)
- [POST] api/notify/postConnects
application/json提交,数据格式如下
``` json
{
Connects:‘‘, // 连接Id集合,多个用,隔开
NotifyObj:Object // 通知的对象,任意类型(总大小不要超过36k)
}
```
* 有UserId
* ExcludeConnectId=true
给改用户除指定的ConnectId外的所有连接端推送
* ExcludeConnectId=false
跟没指定UserId一致
* 无UserId
给指定连接Id推送
- [GET] api/users
获取在线用户Id列表
- [GET] api/groups
获取在线组列表
## 增加日志记录
为了方便分析和定位问题,使用log4net来作为日志记录器。
- nuget 安装log4net
nuget 搜索 log4net,安装
- Config中配置
在Config中注入ILoggerFactory,然后使用添加Log4Net,代码如下所示
``` C#
public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHsts();
loggerFactory.AddLog4Net();
...
}
```
- 添加log4net配置文件
更多配置请自行查找log4net官方配置文档
``` xml
```
至此,log4net配置完毕
更多内容请通过快速导航查看下一篇
## 快速导航
| 标题 | 内容
| :--- | :---
| 索引 | [.net core 3.0 Signalr - 实现一个业务推送系统](/2019/09/20/dotnetcore/signalr/00-introduct/)
| 上一篇 | [.net core 3.0 Signalr - 06 业务实现-业务分析](https://blogs.xxgtalk.cn/2019/10/03/dotnetcore/signalr/06-analysis/)
| 下一篇 | [.net core 3.0 Signalr - 08 业务实现-客户端demo](https://blogs.xxgtalk.cn/2019/10/05/dotnetcore/signalr/08-clientdemo/)
| 源码地址 | [源码](https://github.com/xiexingen/CTS.Signalr)
| 官方文档 | [官方文档](https://docs.microsoft.com/zh-CN/aspnet/core/?view=aspnetcore-3.0)
![二维码](https://img2018.cnblogs.com/blog/394514/201910/394514-20191005215417690-1667450451.gif)
原文地址:https://www.cnblogs.com/xiexingen/p/11625904.html