using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.AspNet.SignalR;
using SignalRChat.Common;
namespace SignalRChat
{
/// <summary>
/// 聊天处理类
/// </summary>
public class ChatHub : Hub
{
#region 数据对象
/// <summary>
/// 连接用户
/// </summary>
static List<UserDetail> ConnectedUsers = new List<UserDetail>();
/// <summary>
/// 聊天消息内容
/// </summary>
static List<MessageDetail> CurrentMessage = new List<MessageDetail>();
#endregion
#region 方法体
/// <summary>
/// 用户连接
/// </summary>
/// <param name="userName">用户名称</param>
public void Connect(string userName)
{
var id = Context.ConnectionId;
if (ConnectedUsers.Count(x => x.ConnectionId == id) == 0)//将新登陆用户记录到缓存中
{
ConnectedUsers.Add(new UserDetail { ConnectionId = id, UserName = userName });
// 向登陆用户发送相关信息(当前用户、历史消息)
Clients.Caller.onConnected(id, userName, ConnectedUsers, CurrentMessage);
// 除开当前登陆用户,向其他客户端用户发送信息(有新的用户登陆提示信息)
Clients.AllExcept(id).onNewUserConnected(id, userName);
}
}
/// <summary>
/// 发送消息给所有用户
/// </summary>
/// <param name="userName">发送者用户名称</param>
/// <param name="message">消息内容</param>
public void SendMessageToAll(string userName, string message)
{
// 向缓存中加入此消息
AddMessageinCache(userName, message);
// 让消息界面上显示
Clients.All.messageReceived(userName, message);
}
/// <summary>
/// 给指定用户发送消息
/// </summary>
/// <param name="toUserId">接收者编号</param>
/// <param name="message">消息内容</param>
public void SendPrivateMessage(string toUserId, string message)
{
///当前发送者连接编号
string fromUserId = Context.ConnectionId;
var toUser = ConnectedUsers.FirstOrDefault(x => x.ConnectionId == toUserId) ;
var fromUser = ConnectedUsers.FirstOrDefault(x => x.ConnectionId == fromUserId);
if (toUser != null && fromUser!=null)
{
//指定用户发送消息界面显示方法
Clients.Client(toUserId).sendPrivateMessage(fromUserId, fromUser.UserName, message);
// 发送者界面显示消息
Clients.Caller.sendPrivateMessage(toUserId, fromUser.UserName, message);
}
}
/// <summary>
/// 退出
/// </summary>
/// <param name="userID">用户连接编号</param>
public void donExit(string userID)
{
var id = Context.ConnectionId;
OnDisconnected();
Clients.Caller.onConnected(id, userID, "");
Clients.Client(id).donExit(id, userID);
}
/// <summary>
/// 退出方法
/// </summary>
/// <returns></returns>
public override System.Threading.Tasks.Task OnDisconnected()
{
var item = ConnectedUsers.FirstOrDefault(x => x.ConnectionId == Context.ConnectionId);
if (item != null)
{
ConnectedUsers.Remove(item);
var id = Context.ConnectionId;
Clients.All.onUserDisconnected(id, item.UserName);
}
return base.OnDisconnected();
}
/// <summary>
/// 向缓存中加入一条消息(只记录最后100条消息)
/// </summary>
/// <param name="userName">发送者</param>
/// <param name="message">消息内容</param>
private void AddMessageinCache(string userName, string message)
{
CurrentMessage.Add(new MessageDetail { UserName = userName, Message = message });
if (CurrentMessage.Count > 100)
CurrentMessage.RemoveAt(0);
}
#endregion
}
}
转载地址:http://www.gycehuags.com/study/672.html
源代码下载:http://www.gycehuags.com/upimg/file/20230312/2023031210430117117.rar
标签:userName,string,处理,ConnectedUsers,ConnectionId,聊天,message,id,SignalRChat From: https://www.cnblogs.com/gycehuags/p/17207757.html