|
- using Microsoft.AspNetCore.SignalR;
- using MoYu.InstantMessaging;
-
- namespace SafeCampus.Web.Core.Hub;
- /// <summary>
- /// 预警消息前端订阅
- /// </summary>
- [MapHub("/hubs/chathub")]
- public class SubscribeHub : Hub<IChatClient>
- {
- // 定义一个方法供客户端调用
- public async Task SendMessage(string user, string message)
- {
- // 触发客户端定义监听的方法
- await Clients.All.ReceiveMessage(user, message);
- }
- /// <summary>
- /// 加入分组
- /// </summary>
- /// <param name="groupName"></param>
- /// <returns></returns>
- public async Task JoinGroup(string groupName)
- {
- await Groups.AddToGroupAsync(Context.ConnectionId, groupName);
- }
- /// <summary>
- /// 给分组发送消息
- /// </summary>
- /// <param name="groupName"></param>
- /// <param name="user"></param>
- /// <param name="message"></param>
- /// <returns></returns>
- public async Task SendMessageToGroup(string groupName, string user, string message)
- {
- await Clients.Group(groupName).ReceiveMessage(user,message);
- }
- }
-
- public interface IChatClient
- {
- Task ReceiveMessage(string user, string message);
- }
|