public override async Task OnConnectedAsync() { var mlUser = JsonHelper.JsonToT <ModelLoginUser>(HttpContextCore.GetCookie(ComConst.UserLogin)); if (mlUser != null && mlUser.Staff != null) { var schoolId = mlUser.School.ID; var staffId = mlUser.Staff.ID; string cnnId = Context.ConnectionId; //在线用户,以学校为单位 var mcu = new ModelChatUser { CnnId = cnnId, MLUser = mlUser }; var schoolMcUsers = SchoolOnLineUsersDic.GetValueOrDefault(schoolId); if (schoolMcUsers == null) { schoolMcUsers = new List <ModelChatUser>(); } schoolMcUsers.Add(mcu); SchoolOnLineUsersDic.AddOrUpdate(schoolId, schoolMcUsers, (key, value) => schoolMcUsers); await Clients.All.SendAsync("chatUserChange", GetOnlineData(schoolId));//更新客户端在线状态 //客户端以学校分组 await Groups.AddToGroupAsync(cnnId, "schoolGroup" + schoolId); //发送离线消息 HandleOfflineMsg(schoolId, staffId, cnnId); } await base.OnConnectedAsync(); }
public override async Task OnDisconnectedAsync(Exception ex) { var mlUser = JsonHelper.JsonToT <ModelLoginUser>(HttpContextCore.GetCookie(ComConst.UserLogin)); var schoolId = mlUser.School.ID; string cnnId = Context.ConnectionId; //在线用户,以学校为单位 var schoolMcUsers = SchoolOnLineUsersDic.GetValueOrDefault(schoolId); if (schoolMcUsers != null) { var mcu = schoolMcUsers.Find(x => x.CnnId == cnnId); schoolMcUsers.Remove(mcu); if (schoolMcUsers.Count == 0) { SchoolOnLineUsersDic.TryRemove(schoolId, out schoolMcUsers); await Groups.RemoveFromGroupAsync(cnnId, "schoolGroup" + schoolId);//清理学校组 } else { SchoolOnLineUsersDic.AddOrUpdate(schoolId, schoolMcUsers, (key, value) => schoolMcUsers); } await Clients.All.SendAsync("chatUserChange", GetOnlineData(schoolId));//更新客户端在线状态 } await base.OnDisconnectedAsync(ex); }
//客户端查看是否有某用户消息 public async Task CheckUserMsg() { var mlUser = JsonHelper.JsonToT <ModelLoginUser>(HttpContextCore.GetCookie(ComConst.UserLogin)); if (mlUser != null && mlUser.Staff != null) { var schoolId = mlUser.School.ID; var staffId = mlUser.Staff.ID; var schoolmsgQueue = SchoolMsgQueueDic.GetValueOrDefault(schoolId); if (schoolmsgQueue != null && schoolmsgQueue.Count > 0) { var newMsgCount = 0; foreach (var cObj in schoolmsgQueue) { if (!cObj.GotMsgStaffIds.Contains(staffId))//未收到 { newMsgCount++; } } if (newMsgCount > 0) { await Clients.Caller.SendAsync("haveNewMsg", newMsgCount);//调用客户端方法 } } } }
public BaseService() { if (null == mlUser) { mlUser = JsonHelper.JsonToT <ModelLoginUser>(HttpContextCore.GetCookie(ComConst.UserLogin)); if (mlUser == null) { mlUser = JsonHelper.JsonToT <ModelLoginUser>(HttpContextCore.GetSession(ComConst.UserLogin)); if (mlUser != null) { HttpContextCore.SetSession(ComConst.UserLogin, JsonHelper.ToJson(mlUser)); } } } mjRet = new ModelJsonRet { code = 0, }; }