|
@@ -1,19 +1,49 @@
|
|
using Microsoft.AspNetCore.SignalR;
|
|
using Microsoft.AspNetCore.SignalR;
|
|
using OASystem.API.OAMethodLib.Hub.HubClients;
|
|
using OASystem.API.OAMethodLib.Hub.HubClients;
|
|
|
|
+using OASystem.API.OAMethodLib.SignalR.Hubs;
|
|
using OASystem.API.OAMethodLib.SignalR.HubService;
|
|
using OASystem.API.OAMethodLib.SignalR.HubService;
|
|
|
|
+using System.DirectoryServices.Protocols;
|
|
|
|
+using System.Text.RegularExpressions;
|
|
|
|
+using static OASystem.API.OAMethodLib.Hub.Hubs.ChatHub;
|
|
|
|
+using static OASystem.API.OAMethodLib.JWTHelper;
|
|
|
|
|
|
namespace OASystem.API.OAMethodLib.Hub.Hubs
|
|
namespace OASystem.API.OAMethodLib.Hub.Hubs
|
|
{
|
|
{
|
|
- [Authorize]
|
|
|
|
public class ChatHub : Hub<IChatClient>
|
|
public class ChatHub : Hub<IChatClient>
|
|
{
|
|
{
|
|
- ILogger<ChatHub> _logger;
|
|
|
|
- public ChatHub(ILogger<ChatHub> logger, CommonService common)
|
|
|
|
|
|
+ private readonly ILogger<ChatHub> _logger;
|
|
|
|
+ private readonly IHttpContextAccessor _accessor;
|
|
|
|
+ private readonly CommonService _common;
|
|
|
|
+
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// 已登录的用户信息
|
|
|
|
+ /// </summary>
|
|
|
|
+ public static List<UserModel> OnlineUser { get; set; } = new List<UserModel>();
|
|
|
|
+
|
|
|
|
+ public ChatHub(ILogger<ChatHub> logger, CommonService common, IHttpContextAccessor accessor)
|
|
{
|
|
{
|
|
_logger = logger;
|
|
_logger = logger;
|
|
_common = common;
|
|
_common = common;
|
|
|
|
+ _accessor = accessor;
|
|
}
|
|
}
|
|
- readonly CommonService _common;
|
|
|
|
|
|
+
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// SignalR登录验证
|
|
|
|
+ /// </summary>
|
|
|
|
+ public async Task SignalRLogin(int userId)
|
|
|
|
+ {
|
|
|
|
+ string connid = Context.ConnectionId;
|
|
|
|
+ bool status = false;
|
|
|
|
+ if (!OnlineUser.Exists(u => u.ConnectionId == connid))
|
|
|
|
+ {
|
|
|
|
+ status = true;
|
|
|
|
+ OnlineUser.Add( new UserModel() { UserId = userId,ConnectionId = connid,GroupName = "FMGJ-OASystem" });
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ //给当前连接返回消息
|
|
|
|
+ await Clients.Client(connid).SendAsync("SignalRLoginResponse", status);
|
|
|
|
+ }
|
|
|
|
+
|
|
/// <summary>
|
|
/// <summary>
|
|
/// 客户端连接服务端
|
|
/// 客户端连接服务端
|
|
/// </summary>
|
|
/// </summary>
|
|
@@ -22,8 +52,15 @@ namespace OASystem.API.OAMethodLib.Hub.Hubs
|
|
{
|
|
{
|
|
var id = Context.ConnectionId;
|
|
var id = Context.ConnectionId;
|
|
_logger.LogInformation($"Client ConnectionId=> [[{id}]] Already Connection Server!");
|
|
_logger.LogInformation($"Client ConnectionId=> [[{id}]] Already Connection Server!");
|
|
|
|
+
|
|
|
|
+ //验证Token
|
|
|
|
+ //var token = _accessor.HttpContext.Request.Query["access_token"];
|
|
|
|
+ //var user = JwtHelper.SerializeJwt(token);
|
|
|
|
+
|
|
|
|
+
|
|
return base.OnConnectedAsync();
|
|
return base.OnConnectedAsync();
|
|
}
|
|
}
|
|
|
|
+
|
|
/// <summary>
|
|
/// <summary>
|
|
/// 客户端断开连接
|
|
/// 客户端断开连接
|
|
/// </summary>
|
|
/// </summary>
|
|
@@ -35,6 +72,9 @@ namespace OASystem.API.OAMethodLib.Hub.Hubs
|
|
_logger.LogInformation($"Client ConnectionId=> [[{id}]] Already Close Connection Server!");
|
|
_logger.LogInformation($"Client ConnectionId=> [[{id}]] Already Close Connection Server!");
|
|
return base.OnDisconnectedAsync(exception);
|
|
return base.OnDisconnectedAsync(exception);
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* 测试
|
|
* 测试
|
|
* */
|
|
* */
|
|
@@ -47,5 +87,15 @@ namespace OASystem.API.OAMethodLib.Hub.Hubs
|
|
Console.WriteLine("Have one Data!");
|
|
Console.WriteLine("Have one Data!");
|
|
await Clients.All.SendAll(_common.SendAll(data));
|
|
await Clients.All.SendAll(_common.SendAll(data));
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ public class UserModel
|
|
|
|
+ {
|
|
|
|
+ public int UserId { get; set; }
|
|
|
|
+
|
|
|
|
+ public string ConnectionId { get; set; }
|
|
|
|
+
|
|
|
|
+ public string GroupName { get; set; }
|
|
|
|
+ }
|
|
}
|
|
}
|
|
}
|
|
}
|