Browse Source

track websocket connections; filter lunotifications

tags/v0.1.0
Robin Thoni 8 years ago
parent
commit
68329b4a8c

+ 4
- 9
Luticate2.Utils/Business/LuCrudBusiness.cs View File

@@ -21,7 +21,7 @@ namespace Luticate2.Utils.Business
21 21
 
22 22
         private readonly LuNotificationsBusiness _notificationsBusiness;
23 23
 
24
-        protected string EntityType = null;
24
+        protected virtual string EntityType { get; set; }
25 25
 
26 26
         protected LuCrudBusiness(TDataAccess dataAccess, LuNotificationsBusiness notificationsBusiness)
27 27
         {
@@ -29,11 +29,6 @@ namespace Luticate2.Utils.Business
29 29
             _notificationsBusiness = notificationsBusiness;
30 30
         }
31 31
 
32
-        protected virtual string GetEntityType()
33
-        {
34
-            return EntityType;
35
-        }
36
-
37 32
         protected virtual LuResult<TDboCreate> CheckAdd(TDboCreate obj)
38 33
         {
39 34
             return LuResult<TDboCreate>.Ok(obj);
@@ -92,7 +87,7 @@ namespace Luticate2.Utils.Business
92 87
             var newEntity = DataAccess.AddDbo(res.Data);
93 88
             if (newEntity)
94 89
             {
95
-                _notificationsBusiness.NotifyCreate(GetEntityType(), newEntity.Data);
90
+                _notificationsBusiness.NotifyCreate(EntityType, newEntity.Data);
96 91
                 return LuResult<T>.Ok(returnFunc(newEntity.Data));
97 92
             }
98 93
             return newEntity.To<T>();
@@ -158,7 +153,7 @@ namespace Luticate2.Utils.Business
158 153
             var editedEntity = DataAccess.EditSingleByIdDbo(id, obj.Data);
159 154
             if (editedEntity)
160 155
             {
161
-                _notificationsBusiness.NotifyUpdate(GetEntityType(), originalEntity.Data, editedEntity.Data);
156
+                _notificationsBusiness.NotifyUpdate(EntityType, originalEntity.Data, editedEntity.Data);
162 157
                 return LuResult<T>.Ok(returnFunc(editedEntity.Data));
163 158
             }
164 159
             return editedEntity.To<T>();
@@ -187,7 +182,7 @@ namespace Luticate2.Utils.Business
187 182
             var res = DataAccess.DeleteSingleById(id, returnFunc);
188 183
             if (res)
189 184
             {
190
-                _notificationsBusiness.NotifyDelete(GetEntityType(), originalEntity.Data);
185
+                _notificationsBusiness.NotifyDelete(EntityType, originalEntity.Data);
191 186
             }
192 187
             return res;
193 188
         }

+ 16
- 10
Luticate2.Utils/Business/LuNotificationsBusiness.cs View File

@@ -1,4 +1,6 @@
1
-using Luticate2.Utils.Hubs;
1
+using System;
2
+using System.Linq;
3
+using Luticate2.Utils.Hubs;
2 4
 using Microsoft.AspNetCore.SignalR;
3 5
 using Microsoft.AspNetCore.SignalR.Infrastructure;
4 6
 
@@ -6,6 +8,7 @@ namespace Luticate2.Utils.Business
6 8
 {
7 9
     public class LuNotificationsBusiness
8 10
     {
11
+        private readonly LuHubConnectionTracker _connectionTracker;
9 12
         private readonly IHubContext _hubContext;
10 13
 
11 14
         public const string EventCreate = "EVENT_CREATE";
@@ -14,29 +17,32 @@ namespace Luticate2.Utils.Business
14 17
 
15 18
         public const string EventDelete = "EVENT_DELETE";
16 19
 
17
-        public LuNotificationsBusiness(IConnectionManager connectionManager)
20
+        public LuNotificationsBusiness(IConnectionManager connectionManager, LuHubConnectionTracker connectionTracker)
18 21
         {
22
+            _connectionTracker = connectionTracker;
19 23
             _hubContext = connectionManager.GetHubContext<LuNotificationsHub>();
20 24
         }
21 25
 
22
-        public void Notify(string eventName, string entityType, object oldEntity, object newEntity)
26
+        public void Notify(string eventName, string entityType, object oldEntity, object newEntity, Func<string, bool> filter = null)
23 27
         {
24
-            _hubContext.Clients.All.notify(eventName, entityType, oldEntity, newEntity);
28
+            var connectionsIds = _connectionTracker.Get<LuNotificationsHub>();
29
+            var selectedIds = filter != null ? connectionsIds.Where(filter).ToList() : connectionsIds;
30
+            _hubContext.Clients.Clients(selectedIds).notify(eventName, entityType, oldEntity, newEntity);
25 31
         }
26 32
 
27
-        public void NotifyCreate(string entityType, object newEntity)
33
+        public void NotifyCreate(string entityType, object newEntity, Func<string, bool> filter = null)
28 34
         {
29
-            Notify(EventCreate, entityType, null, newEntity);
35
+            Notify(EventCreate, entityType, null, newEntity, filter);
30 36
         }
31 37
 
32
-        public void NotifyUpdate(string entityType, object oldEntity, object newEntity)
38
+        public void NotifyUpdate(string entityType, object oldEntity, object newEntity, Func<string, bool> filter = null)
33 39
         {
34
-            Notify(EventUpdate, entityType, oldEntity, newEntity);
40
+            Notify(EventUpdate, entityType, oldEntity, newEntity, filter);
35 41
         }
36 42
 
37
-        public void NotifyDelete(string entityType, object oldEntity)
43
+        public void NotifyDelete(string entityType, object oldEntity, Func<string, bool> filter = null)
38 44
         {
39
-            Notify(EventDelete, entityType, oldEntity, null);
45
+            Notify(EventDelete, entityType, oldEntity, null, filter);
40 46
         }
41 47
     }
42 48
 }

+ 1
- 0
Luticate2.Utils/Controllers/LuUtilsExtensions.cs View File

@@ -38,6 +38,7 @@ namespace Luticate2.Utils.Controllers
38 38
                 options.Hubs.EnableDetailedErrors = true;
39 39
             });
40 40
 
41
+            services.AddSingleton<LuHubConnectionTracker>();
41 42
             services.AddSingleton<LuNotificationsBusiness>();
42 43
             Options = new LuOptionsDbo();
43 44
             optionsDelegate(Options);

+ 27
- 0
Luticate2.Utils/Hubs/LuHub.cs View File

@@ -0,0 +1,27 @@
1
+using System.Threading.Tasks;
2
+using Microsoft.AspNetCore.SignalR;
3
+
4
+namespace Luticate2.Utils.Hubs
5
+{
6
+    public class LuHub : Hub
7
+    {
8
+        private readonly LuHubConnectionTracker _connectionTracker;
9
+
10
+        public LuHub(LuHubConnectionTracker connectionTracker)
11
+        {
12
+            _connectionTracker = connectionTracker;
13
+        }
14
+
15
+        public override Task OnConnected()
16
+        {
17
+            _connectionTracker.Add(this, Context.ConnectionId);
18
+            return base.OnConnected();
19
+        }
20
+
21
+        public override Task OnDisconnected(bool stopCalled)
22
+        {
23
+            _connectionTracker.Remove(this, Context.ConnectionId);
24
+            return base.OnDisconnected(stopCalled);
25
+        }
26
+    }
27
+}

+ 96
- 0
Luticate2.Utils/Hubs/LuHubConnectionTracker.cs View File

@@ -0,0 +1,96 @@
1
+using System;
2
+using System.Collections.Generic;
3
+
4
+namespace Luticate2.Utils.Hubs
5
+{
6
+    public class LuHubConnectionTracker
7
+    {
8
+        protected IDictionary<string, IList<string>> ConnectionIds { get; set; }
9
+
10
+        public LuHubConnectionTracker()
11
+        {
12
+            ConnectionIds = new Dictionary<string, IList<string>>();
13
+        }
14
+
15
+        public static string GetNameFromType(Type type)
16
+        {
17
+            return type.FullName;
18
+        }
19
+
20
+
21
+
22
+        public void Add(string name, string id)
23
+        {
24
+            GetReal(name).Add(id);
25
+        }
26
+
27
+        public void Add(Type type, string id)
28
+        {
29
+            Add(GetNameFromType(type), id);
30
+        }
31
+
32
+        public void Add(object obj, string id)
33
+        {
34
+            Add(obj.GetType(), id);
35
+        }
36
+
37
+        public void Add<T>(string id)
38
+        {
39
+            Add(typeof(T), id);
40
+        }
41
+
42
+
43
+
44
+        public void Remove(string name, string id)
45
+        {
46
+            GetReal(name).Remove(id);
47
+        }
48
+
49
+        public void Remove(Type type, string id)
50
+        {
51
+            Remove(GetNameFromType(type), id);
52
+        }
53
+
54
+        public void Remove(object obj, string id)
55
+        {
56
+            Remove(obj.GetType(), id);
57
+        }
58
+
59
+        public void Remove<T>(string id)
60
+        {
61
+            Remove(typeof(T), id);
62
+        }
63
+
64
+
65
+
66
+        public IList<string> GetReal(string name)
67
+        {
68
+            if (!ConnectionIds.ContainsKey(name))
69
+            {
70
+                ConnectionIds.Add(name, new List<string>());
71
+            }
72
+            return ConnectionIds[name];
73
+        }
74
+
75
+        public IList<string> Get(string name)
76
+        {
77
+            return new List<string>(GetReal(name));
78
+        }
79
+
80
+        public IList<string> Get(Type type)
81
+        {
82
+            return Get(GetNameFromType(type));
83
+        }
84
+
85
+        public IList<string> Get(object obj)
86
+        {
87
+            return Get(GetNameFromType(obj.GetType()));
88
+        }
89
+
90
+        public IList<string> Get<T>()
91
+        {
92
+            return Get(GetNameFromType(typeof(T)));
93
+        }
94
+
95
+    }
96
+}

+ 5
- 4
Luticate2.Utils/Hubs/LuNotificationsHub.cs View File

@@ -1,9 +1,10 @@
1
-using Microsoft.AspNetCore.SignalR;
2
-
1
+
3 2
 namespace Luticate2.Utils.Hubs
4 3
 {
5
-    public class LuNotificationsHub : Hub
4
+    public class LuNotificationsHub : LuHub
6 5
     {
7
-
6
+        public LuNotificationsHub(LuHubConnectionTracker connectionTracker) : base(connectionTracker)
7
+        {
8
+        }
8 9
     }
9 10
 }

Loading…
Cancel
Save