/// <summary> /// 是否存在用户 /// </summary> /// <param name="info"></param> /// <returns></returns> public bool IsExistUser(ChatUserInfo info) { foreach (ChatUserInfo item in UserList) { if (info.Name == item.Name && info.ID == item.ID) { return(true); } } return(false); }
/// <summary> /// 供客户端调用的【通知用户在线】方法 /// </summary> /// <param name="message"></param> public void SendOnLine(Message msg) { var userInfo = new ChatUserInfo() { ID = msg.UserID, Name = msg.Name }; userInfo.TimeOutEvent += () => { //用户20s无心跳包应答,则视为掉线,会抛出事件,这里会接住,然后处理用户掉线动作。 SendOffLine(msg); }; if (!IsExistUser(userInfo)) { UserList.Add(userInfo); } _chatHandler.Clients.All.onLine(UserList); SendMessage(new Message() { UserID = msg.UserID, Name = msg.Name, Content = "" }); }