public override Task OnConnected() { var name = LOGIN_ACCOUNT; using (var db = new SignalR_MapUsersModel()) { var user = db.Users .Include(u => u.Connections) .SingleOrDefault(u => u.UserName == name); if (user == null) { user = new User { UserName = name, CreateTime = DateTime.Now, Connections = new List <Connection>() }; db.Users.Add(user); } user.Connections.Add(new Connection { ConnectionID = Context.ConnectionId, UserAgent = Context.Request.Headers["User-Agent"], CreateTime = DateTime.Now, UpdateTime = DateTime.Now, Connected = true }); db.SaveChanges(); } Base_Online(); return(base.OnConnected()); }
/// <summary> /// 获取-App用户 在线ConnectionId集合 /// </summary> /// <param name="app_accounts"></param> private IList <string> Base_GetConnectionIds(IList <string> app_accounts) { var resConnectionIDs = new List <string>(); using (var db = new SignalR_MapUsersModel()) { resConnectionIDs = db.Connections .Where(p => p.Connected == true && app_accounts.Contains(p.User_UserName)) .Select(p => p.ConnectionID).ToList(); } return(resConnectionIDs); }
/// <summary> /// 检测断开原因 /// </summary> /// <param name="stopCalled"></param> /// <returns></returns> public override Task OnDisconnected(bool stopCalled) { if (stopCalled) { Console.WriteLine(String.Format("Client {0} explicitly closed the connection.", Context.ConnectionId)); } else { Console.WriteLine(String.Format("Client {0} timed out .", Context.ConnectionId)); } using (var db = new SignalR_MapUsersModel()) { var connection = db.Connections.Find(Context.ConnectionId); db.Connections.Remove(connection); //connection.Connected = false; //connection.UpdateTime = DateTime.Now; db.SaveChanges(); } Base_Offline(); return(base.OnDisconnected(stopCalled)); }
/// <summary> /// 发送-图片消息 /// </summary> /// <returns></returns> public object Single_SendPicture(ISingle_SendText model) { var msg = new IM_MsgSingle(); var db_result = 0; using (var db = new ModelAC()) { #region db msg.App_Id = APP_GUID; msg.MsgClientId = model.MsgClientId; msg.From_UserId = db.IM_User.SingleOrDefault(p => p.App_Id == APP_GUID && p.UserId == model.From_UserId).Id; msg.To_UserId = db.IM_User.SingleOrDefault(p => p.App_Id == APP_GUID && p.UserId == model.To_UserId).Id; msg.MsgTime = DateTime.Now; msg.IsRead = false; msg.MsgType = model.MsgType; msg.MsgBody = model.MsgBody; db.IM_MsgSingle.Add(msg); db_result = db.SaveChanges(); #endregion } var session_result = Single_updateSession(msg); if (db_result == 1) { var mOSingle_SendText = new OSingle_SendText() { #region 输出 Id = msg.Id.ToString(), MsgClientId = msg.MsgClientId, From_UserId = model.From_UserId, To_UserId = model.To_UserId, MsgTime = msg.MsgTime, IsRead = msg.IsRead, MsgType = msg.MsgType, MsgBody = msg.MsgBody, #endregion }; var jsonResult = CommonJson.camelObject(mOSingle_SendText); #region 消息分发 string to__Account = HubKey.App__Account(app: APP, account: model.To_UserId); using (var db = new SignalR_MapUsersModel()) { var connections = db.Connections.Where(p => p.User_UserName == to__Account && p.Connected == true).ToList(); for (int i = 0; i < connections.Count(); i++) { Clients.Client(connections[i].ConnectionID).Single_SendPicture(jsonResult); } } #endregion return(jsonResult); } else { return(null); } }