瀏覽代碼

signalr 调整

leiy 1 年之前
父節點
當前提交
f4c7e496aa

+ 0 - 67
OASystem/OASystem.Api/OAMethodLib/Hubs/MyHub.cs

@@ -1,67 +0,0 @@
-using Microsoft.AspNetCore.SignalR;
-
-namespace OASystem.API.OAMethodLib.Hubs
-{
-    /// <summary>
-    /// 定义集线器
-    /// </summary>
-    public class MyHub : Hub
-    {
-        /// <summary>
-        /// 用户字典
-        /// </summary>
-        private static Dictionary<string, string> dictUsers = new Dictionary<string, string>();
-
-        /// <summary>
-        /// 建立连接回调
-        /// </summary>
-        /// <returns></returns>
-        public override Task OnConnectedAsync()
-        {
-            Console.WriteLine($"ID:{Context.ConnectionId} 已连接");
-            return base.OnConnectedAsync();
-        }
-
-        /// <summary>
-        /// 断开连接回调
-        /// </summary>
-        /// <param name="exception"></param>
-        /// <returns></returns>
-        public override Task OnDisconnectedAsync(Exception? exception)
-        {
-            Console.WriteLine($"ID:{Context.ConnectionId} 已断开");
-            return base.OnDisconnectedAsync(exception);
-        }
-
-
-        /// <summary>
-        /// 登录功能,将用户ID和ConntectionId关联起来
-        /// </summary>
-        /// <param name="userId"></param>
-        public void Login(string userId)
-        {
-            if (!dictUsers.ContainsKey(userId))
-            {
-                dictUsers[userId] = Context.ConnectionId;
-            }
-            Console.WriteLine($"{userId}登录成功,ConnectionId={Context.ConnectionId}");
-            //向所有用户发送当前在线的用户列表
-            Clients.All.SendAsync("Users", dictUsers.Keys.ToList());
-        }
-
-        /// <summary>
-        /// 退出功能,当客户端退出时调用
-        /// </summary>
-        /// <param name="userId"></param>
-        public void Logout(string userId)
-        {
-            if (dictUsers.ContainsKey(userId))
-            {
-                dictUsers.Remove(userId);
-            }
-            Console.WriteLine($"{userId}退出成功,ConnectionId={Context.ConnectionId}");
-        }
-
-    }
-
-}

+ 1 - 2
OASystem/OASystem.Api/OAMethodLib/Hubs/ServerHub.cs

@@ -46,7 +46,7 @@ namespace OASystem.API.OAMethodLib.Hubs
             //连接用户 这里可以存在Redis
             var model = new UserModel
             {
-                Uid = user.Uid,
+                UserId = user.Uid,
                 ConnectionId = connId,
                 Token = token,
                 UserName = user.UserName
@@ -88,7 +88,6 @@ namespace OASystem.API.OAMethodLib.Hubs
                     Data = "true",
                     Msg = "断开连接"
                 });
-
             }
             return base.OnDisconnectedAsync(exception);
         }

+ 1 - 1
OASystem/OASystem.Api/OAMethodLib/Hubs/UserModel.cs

@@ -2,7 +2,7 @@
 {
     public class UserModel
     {
-        public long Uid { get; set; }
+        public long UserId { get; set; }
 
         public string ConnectionId { get; set; }
 

+ 25 - 0
OASystem/OASystem.Api/OAMethodLib/Quartz/Business/TaskNewsFeedJob.cs

@@ -0,0 +1,25 @@
+using Microsoft.AspNetCore.SignalR;
+using OASystem.API.OAMethodLib.Hubs;
+using OASystem.Domain.Entities.PersonnelModule;
+using OASystem.Infrastructure.Repositories.PersonnelModule;
+using ILogger = Microsoft.Extensions.Logging.ILogger;
+
+namespace OASystem.API.OAMethodLib.Quartz.Business
+{
+    public static class TaskNewsFeedJob
+    {
+        private readonly static TaskAllocationRepository _taskAllocationRep = AutofacIocManager.Instance.GetService<TaskAllocationRepository>();
+        private readonly static ILogger _logger;
+        private readonly static IHubContext<ServerHub> _hubContext = (IHubContext<ServerHub>)AutofacIocManager.Instance.GetService<ServerHub>();
+
+        /// <summary>
+        /// 定时任务更改状态
+        /// 每天下午六点定时更新
+        /// </summary>
+        public static async void PostTaskMessagePush()
+        {
+            
+            
+        }
+    }
+}

+ 21 - 7
OASystem/OASystem.Api/OAMethodLib/Quartz/Jobs/TaskNewsFeedJob.cs

@@ -23,9 +23,14 @@ namespace OASystem.API.OAMethodLib.Quartz.Jobs
             _hubContext = hubContext;
         }
 
-        public Task Execute(IJobExecutionContext context)
+        /// <summary>
+        /// 任务消息推送
+        /// </summary>
+        /// <param name="context"></param>
+        /// <returns></returns>
+        public async Task Execute(IJobExecutionContext context)
         {
-            _logger.LogInformation("调用任务消息推送 " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
+            _logger.LogInformation("调用任务消息推送 " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")+"【在线人数】:"+JsonConvert.SerializeObject(ServerHub.OnlineUser));
 
             //在此处编写任务业务代码
             #region 消息处理
@@ -138,19 +143,28 @@ namespace OASystem.API.OAMethodLib.Quartz.Jobs
             //}
             #endregion
 
-            //查询需要提示的消息 Singlr
-            string sql = string.Format(@"Select smra.Id,su.CnName As Issuer,sm.ReleaseTime,sm.Title,
+            string msg = string.Empty;
+            if (ServerHub.OnlineUser.Count  > 0)
+            {
+                //查询需要提示的消息 Singlr
+                string sql = string.Format(@"Select smra.Id,su.CnName As Issuer,sm.ReleaseTime,sm.Title,
 										 sm.Content,smra.ReadableUId,smra.IsRead,smra.CreateTime
 										 From Sys_Message sm
 										 Left Join Sys_MessageReadAuth smra On sm.Id = smra.MsgId
 										 Left Join Sys_Users su On sm.IssuerId = su.Id
 										 Where sm.IsDel = 0 And sm.Type = 6 And smra.IsRead = 0
 										 Order By smra.ReadableUId,smra.CreateTime Desc");
-            //var datas = _taskAllocationRep._sqlSugar.SqlQueryable<MessageReadAuthPushView>(sql).ToList();
+                var datas = _taskAllocationRep._sqlSugar.SqlQueryable<MessageReadAuthPushView>(sql).ToList();
+                msg = JsonConvert.SerializeObject(datas);
 
-             _hubContext.Clients.All.SendAsync("ReceiveMessage", "系统通知", $"最新消息{DateTime.Now}");
+                //_hubContext.Clients.All.SendAsync("ReceiveMessage", "系统通知", $"最新消息{DateTime.Now}");
+                //推送给所有连接ID的第一条数据
+            }
 
-            return Task.CompletedTask;
+            _logger.LogInformation("调用任务消息推送 " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "【在线人数】:" + JsonConvert.SerializeObject(ServerHub.OnlineUser) +"【推送消息】:"+ msg);
+
+            await _hubContext.Clients.Clients(ServerHub.OnlineUser.Select(q => q.ConnectionId).ToList()).SendAsync("SendMessageResponse", $"最新消息{DateTime.Now}");
+            //return Task.CompletedTask;
         }
     }
 }

+ 1 - 1
OASystem/OASystem.Api/OAMethodLib/Quartz/QuartzFactory.cs

@@ -37,7 +37,7 @@ namespace QuzrtzJob.Factory
                             .WithCronSchedule("0 0 18 * * ?")
                             .Build();
             var taskMsgTrigger = TriggerBuilder.Create()
-                            .WithSimpleSchedule(x => x.WithIntervalInSeconds(30).RepeatForever())//每六十秒执行一次
+                            .WithSimpleSchedule(x => x.WithIntervalInSeconds(60).RepeatForever())//每六十秒执行一次
                             //.WithCronSchedule("0 0 14 * * ?")
                             .Build();