|
@@ -1,4 +1,5 @@
|
|
|
using Microsoft.AspNetCore.SignalR;
|
|
|
+using OASystem.API.OAMethodLib.Hub;
|
|
|
using OASystem.API.OAMethodLib.Hub.HubClients;
|
|
|
using OASystem.API.OAMethodLib.SignalR.Hubs;
|
|
|
using OASystem.API.OAMethodLib.SignalR.HubService;
|
|
@@ -12,19 +13,17 @@ namespace OASystem.API.OAMethodLib.Hub.Hubs
|
|
|
public class ChatHub : Hub<IChatClient>
|
|
|
{
|
|
|
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>();
|
|
|
|
|
|
- /// <summary>
|
|
|
- /// 已登录的用户信息
|
|
|
- /// </summary>
|
|
|
- public static List<UserModel> OnlineUser { get; set; } = new List<UserModel>();
|
|
|
-
|
|
|
- public ChatHub(ILogger<ChatHub> logger, CommonService common, IHttpContextAccessor accessor)
|
|
|
+ public ChatHub(ILogger<ChatHub> logger, CommonService common)
|
|
|
{
|
|
|
_logger = logger;
|
|
|
_common = common;
|
|
|
- _accessor = accessor;
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
@@ -33,15 +32,18 @@ namespace OASystem.API.OAMethodLib.Hub.Hubs
|
|
|
public async Task SignalRLogin(int userId)
|
|
|
{
|
|
|
string connid = Context.ConnectionId;
|
|
|
- bool status = false;
|
|
|
- if (!OnlineUser.Exists(u => u.ConnectionId == connid))
|
|
|
+ string result = $"[{connid}]";
|
|
|
+ if (!UserStore.OnlineUser.Exists(u => u.ConnectionId == connid))
|
|
|
{
|
|
|
- status = true;
|
|
|
- OnlineUser.Add( new UserModel() { UserId = userId,ConnectionId = connid,GroupName = "FMGJ-OASystem" });
|
|
|
-
|
|
|
+ result += "上线成功!" ;
|
|
|
+ UserStore.OnlineUser.Add( new UserModel() { UserId = userId,ConnectionId = connid,GroupName = "FMGJ-OASystem" });
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ result += "已上线!";
|
|
|
}
|
|
|
//给当前连接返回消息
|
|
|
- //await Clients.Client(connid).SendAsync("SignalRLoginResponse", status);
|
|
|
+ await Clients.Client(connid).SendAsync("SignalRLoginResponse", result);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
@@ -50,14 +52,9 @@ namespace OASystem.API.OAMethodLib.Hub.Hubs
|
|
|
/// <returns></returns>
|
|
|
public override Task OnConnectedAsync()
|
|
|
{
|
|
|
- var id = Context.ConnectionId;
|
|
|
- _logger.LogInformation($"Client ConnectionId=> [[{id}]] Already Connection Server!");
|
|
|
-
|
|
|
- //验证Token
|
|
|
- //var token = _accessor.HttpContext.Request.Query["access_token"];
|
|
|
- //var user = JwtHelper.SerializeJwt(token);
|
|
|
-
|
|
|
-
|
|
|
+ var connid = Context.ConnectionId;
|
|
|
+ _logger.LogInformation($"Client ConnectionId=> [[{connid}]] Already Connection Server!");
|
|
|
+
|
|
|
return base.OnConnectedAsync();
|
|
|
}
|
|
|
|
|
@@ -68,13 +65,21 @@ namespace OASystem.API.OAMethodLib.Hub.Hubs
|
|
|
/// <returns></returns>
|
|
|
public override Task OnDisconnectedAsync(Exception exception)
|
|
|
{
|
|
|
- var id = Context.ConnectionId;
|
|
|
- _logger.LogInformation($"Client ConnectionId=> [[{id}]] Already Close Connection Server!");
|
|
|
+ var connid = Context.ConnectionId;
|
|
|
+ _logger.LogInformation($"Client ConnectionId=> [[{connid}]] Already Close Connection Server!");
|
|
|
+ var model = UserStore.OnlineUser.Find(u => u.ConnectionId == connid);
|
|
|
+ int count = UserStore.OnlineUser.RemoveAll(u => u.ConnectionId == connid);
|
|
|
+ if (model != null)
|
|
|
+ {
|
|
|
+
|
|
|
+ var onlineUser = UserStore.OnlineUser.FindAll(u => u.GroupName == model.GroupName);
|
|
|
+
|
|
|
+ Clients.Group(model.GroupName).SendAsync("GetUsersResponse", onlineUser);
|
|
|
+ }
|
|
|
return base.OnDisconnectedAsync(exception);
|
|
|
}
|
|
|
|
|
|
|
|
|
-
|
|
|
/**
|
|
|
* 测试
|
|
|
* */
|
|
@@ -86,16 +91,10 @@ namespace OASystem.API.OAMethodLib.Hub.Hubs
|
|
|
{
|
|
|
Console.WriteLine("Have one Data!");
|
|
|
await Clients.All.SendAll(_common.SendAll(data));
|
|
|
+ await Clients.Caller.SendAll(_common.SendCaller(data));
|
|
|
}
|
|
|
|
|
|
|
|
|
- public class UserModel
|
|
|
- {
|
|
|
- public int UserId { get; set; }
|
|
|
-
|
|
|
- public string ConnectionId { get; set; }
|
|
|
-
|
|
|
- public string GroupName { get; set; }
|
|
|
- }
|
|
|
+
|
|
|
}
|
|
|
}
|