-
Notifications
You must be signed in to change notification settings - Fork 23
/
NotifyHub.cs
145 lines (130 loc) · 5.28 KB
/
NotifyHub.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
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
{
/// <summary>
/// 服务端接口
/// </summary>
public interface IServerNotifyHub
{
}
/// <summary>
/// 客户端使用的接口
/// </summary>
public interface IClientNotifyHub
{
Task OnNotify(object data);
Task OnLine(object data);
Task OffLine(object data);
}
[Authorize]
public class NotifyHub : Hub<IClientNotifyHub>,IServerNotifyHub
{
private readonly SignalrRedisHelper _signalrRedisHelper;
private readonly ILogger _logger;
public NotifyHub(SignalrRedisHelper signalrRedisHelper, ILogger<NotifyHub> 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);
}
/// <summary>
/// 加入组
/// </summary>
/// <param name="groupName"></param>
/// <returns></returns>
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 });
}
}
}
/// <summary>
/// 从组中移除
/// </summary>
/// <param name="groupName"></param>
/// <returns></returns>
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 });
}
}
}
/// <summary>
/// 处理上线通知(只有用户第一个连接才通知)
/// </summary>
/// <param name="userId"></param>
/// <param name="connectionId"></param>
/// <returns></returns>
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
});
}
/// <summary>
/// 处理下线通知(只有当用户一个连接都没了 才算下线)
/// </summary>
/// <param name="userId"></param>
/// <param name="connectionId"></param>
/// <returns></returns>
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
});
}
}
}