瀏覽代碼

hotlinehub

xf 1 年之前
父節點
當前提交
691117df54

+ 25 - 0
src/Hotline.Api/Realtimes/HotlineHub.cs

@@ -0,0 +1,25 @@
+using Hotline.Share.Dtos.Realtime;
+using Microsoft.AspNetCore.SignalR;
+using XF.Domain.Authentications;
+
+namespace Hotline.Api.Realtimes
+{
+    public class HotlineHub : Hub
+    {
+        public async Task JoinGroupAsync(JoinGroupDto dto)
+        {
+            await Groups.AddToGroupAsync(Context.ConnectionId, dto.GroupName);
+
+            await Clients.Group(dto.GroupName).SendAsync("Send", $"{Context.ConnectionId} has joined the group {dto.GroupName}.");
+        }
+
+        public async Task LeaveGroupAsync(JoinGroupDto dto)
+        {
+            await Groups.RemoveFromGroupAsync(Context.ConnectionId, dto.GroupName);
+
+            await Clients.Group(dto.GroupName).SendAsync("Send", $"{Context.ConnectionId} has left the group {dto.GroupName}.");
+        }
+
+
+    }
+}

+ 1 - 0
src/Hotline.Api/StartupExtensions.cs

@@ -134,6 +134,7 @@ internal static class StartupExtensions
         app.UseAuthentication();
         app.UseAuthorization();
         app.MapHub<CallCenterHub>("/hubs/callcenter");
+        app.MapHub<HotlineHub>("/hubs/hotline");
 
         //app.UseCloudEvents();
         app.MapControllers()

+ 13 - 0
src/Hotline.Share/Dtos/Realtime/JoinGroupDto.cs

@@ -0,0 +1,13 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Hotline.Share.Dtos.Realtime
+{
+    public class JoinGroupDto
+    {
+        public string GroupName { get; set; }
+    }
+}