Browse Source

SignalR 6

leiy 1 year ago
parent
commit
a79de2bd02

+ 11 - 13
OASystem/OASystem.Api/OAMethodLib/GeneralMethod.cs

@@ -76,19 +76,17 @@ namespace OASystem.API.OAMethodLib
             if (status)
             {
                 //给在线在用户发送消息
-                List<string> connId = UserStore.OnlineUser.Where(it => userIds.Contains(it.UserId)).Select(it => it.ConnectionId).ToList();
-
-                string notificationTypeStr = GetMsgNotificationType(msgTypeEnum);
-                var msg = JsonConvert.SerializeObject(
-                        new {
-                            NotificationType = notificationTypeStr,
-                            Issuer = "管理员",
-                            Title = title,
-                            Content = content,
-                            ReleaseTime = messageDto.ReleaseTime
-                        }
-                    );
-                await _hubContext.Clients.Clients(connId).ReceiveMessage($"您有一条{msg}相关的消息!");
+                List<string> connIds = UserStore.OnlineUser.Where(it => userIds.Contains(it.UserId)).Select(it => it.ConnectionId).ToList();
+
+                string notificationTypeStr1 = GetMsgNotificationType(msgTypeEnum);
+                string notificationTypeStr = JsonConvert.SerializeObject(
+                       new
+                       {
+                           UserIds = userIds,
+                           Msg = $"您有一条{notificationTypeStr1}相关的消息!"
+                       }
+                   );
+                await _hubContext.Clients.Clients(connIds).ReceiveMessage(notificationTypeStr);
 
                 return true;
             }

+ 4 - 1
OASystem/OASystem.Api/OAMethodLib/SignalR/Hubs/ChatHub.cs

@@ -43,6 +43,7 @@ namespace OASystem.API.OAMethodLib.Hub.Hubs
             if (!UserStore.OnlineUser.Exists(u => u.ConnectionId == connid))
             {
                 result += "上线成功!" ;
+                
                 UserStore.OnlineUser.Add( new UserModel() { UserId = userId,ConnectionId = connid,GroupName = "FMGJ-OASystem" });
             }
             else
@@ -74,6 +75,9 @@ namespace OASystem.API.OAMethodLib.Hub.Hubs
                     {
                         var connId = Context.ConnectionId;
 
+                        //UserStore.OnlineUser.RemoveAll(u => u.UserId == tokenModelJwt.UserId);
+                        
+
                         UserStore.OnlineUser.Add(new UserModel() { UserId = tokenModelJwt.UserId, ConnectionId = connId, GroupName = "FMGJ-OASystem" });
 
                         _logger.LogInformation($"Client ConnectionId=> [[{connId}]] UserId=> [[{tokenModelJwt.UserId}]] Already Connection Server!");
@@ -105,7 +109,6 @@ namespace OASystem.API.OAMethodLib.Hub.Hubs
             int count = UserStore.OnlineUser.RemoveAll(u => u.ConnectionId == connId);
             if (model != null)
             {
-
                 var onlineUser = UserStore.OnlineUser.FindAll(u => u.GroupName == model.GroupName);
 
                 Clients.Clients(connId).ReceiveMessage(_common.ReceiveMessage($"[UserID=>{model.UserId} ConnectionId=> {model.ConnectionId} ] 已下线!"));