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

80 lines
2.4 KiB
C#

5 months ago
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方法
/// <summary>
/// 建立连接
/// </summary>
/// <returns></returns>
public override async Task OnConnectedAsync()
{
//await AddOnline();
await base.OnConnectedAsync();
}
/// <summary>
/// 断开连接
/// </summary>
/// <param name="exception">异常信息</param>
/// <returns></returns>
public override async Task OnDisconnectedAsync(Exception exception)
{
await RemoveOnline();
await base.OnDisconnectedAsync(exception);
}
#endregion
private static readonly ConcurrentDictionary<string, string> clientIdMap =
new ConcurrentDictionary<string, string>();
#region 客户端操作
///// <summary>
///// 添加在线用户
///// </summary>
//public async Task AddOnline()
//{
//}
/// <summary>
/// 移除在线用户
/// </summary>
public async Task RemoveOnline()
{
string clientId = Context.ConnectionId;
clientIdMap.TryGetValue(clientId, out string userId);
await Groups.RemoveFromGroupAsync(clientId, userId);
}
/// <summary>
/// 发送消息
/// </summary>
/// <param name="myUserId">我的UserId</param>
/// <param name="toUserId">对方UserId</param>
/// <param name="msg">消息</param>
/// <param name="isSystem">是否系统消息0不是1是</param>
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
}
}