平安校园
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SubscribeHub.cs 1.2 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. using Microsoft.AspNetCore.SignalR;
  2. using MoYu.InstantMessaging;
  3. namespace SafeCampus.Web.Core.Hub;
  4. /// <summary>
  5. /// 预警消息前端订阅
  6. /// </summary>
  7. [MapHub("/hubs/chathub")]
  8. public class SubscribeHub : Hub<IChatClient>
  9. {
  10. // 定义一个方法供客户端调用
  11. public async Task SendMessage(string user, string message)
  12. {
  13. // 触发客户端定义监听的方法
  14. await Clients.All.ReceiveMessage(user, message);
  15. }
  16. /// <summary>
  17. /// 加入分组
  18. /// </summary>
  19. /// <param name="groupName"></param>
  20. /// <returns></returns>
  21. public async Task JoinGroup(string groupName)
  22. {
  23. await Groups.AddToGroupAsync(Context.ConnectionId, groupName);
  24. }
  25. /// <summary>
  26. /// 给分组发送消息
  27. /// </summary>
  28. /// <param name="groupName"></param>
  29. /// <param name="user"></param>
  30. /// <param name="message"></param>
  31. /// <returns></returns>
  32. public async Task SendMessageToGroup(string groupName, string user, string message)
  33. {
  34. await Clients.Group(groupName).ReceiveMessage(user,message);
  35. }
  36. }
  37. public interface IChatClient
  38. {
  39. Task ReceiveMessage(string user, string message);
  40. }