using DocumentFormat.OpenXml.InkML; using DocumentFormat.OpenXml.Spreadsheet; using Microsoft.AspNetCore.SignalR; using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace OpenAuth.App.BaseApp.ImMsgManager { public class ChatsHub : Hub { #region 重载Hub方法 /// /// 建立连接 /// /// public override async Task OnConnectedAsync() { //await AddOnline(); await base.OnConnectedAsync(); } /// /// 断开连接 /// /// 异常信息 /// public override async Task OnDisconnectedAsync(Exception exception) { await RemoveOnline(); await base.OnDisconnectedAsync(exception); } #endregion private static readonly ConcurrentDictionary clientIdMap = new ConcurrentDictionary(); #region 客户端操作 ///// ///// 添加在线用户 ///// //public async Task AddOnline() //{ //} /// /// 移除在线用户 /// public async Task RemoveOnline() { string clientId = Context.ConnectionId; clientIdMap.TryGetValue(clientId, out string userId); await Groups.RemoveFromGroupAsync(clientId, userId); } /// /// 发送消息 /// /// 我的UserId /// 对方UserId /// 消息 /// 是否系统消息0不是1是 public async Task SendMsg(string myUserId, string toUserId, string msg, string id, int isSystem) { await Clients.Group(toUserId).SendAsync("RevMsg", myUserId, msg, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), id, isSystem); } public async Task SendInfo(string userId) { string clientId = Context.ConnectionId; clientIdMap.GetOrAdd(clientId, userId); await Groups.AddToGroupAsync(clientId, userId); } #endregion } }