public override void OnClientClosed(IClient client) { if (mClients.Any(it => it.client == client)) { AuthSocketInfo cl = mClients.First(it => it.client == client); using (var db = new RaidaContext()) { Members user = db.Members.Include(m => m.MemberInGroup).First(it => it.login.Equals(cl.login)); user.online = false; user.last_use = SystemClock.GetInstance().CurrentTime; db.SaveChanges(); //организовать отправку остальным клиентом SendMessageOfChangeUser(db, user); } mClients.Remove(cl); } }
/* { "execFun": "authorization", * "data": { * "login": "******", * "password": "******" * } * } */ //Замутить обновление статуса онлайн-офлайн public AuthSocketInfo Execute(object data) { AuthSocketInfo output = new AuthSocketInfo(); AuthInfo info; try { info = JsonConvert.DeserializeObject <AuthInfo>(data.ToString()); using (var db = new RaidaContext()) { Members user = db.Members.FirstOrDefault(it => it.login.Equals(info.login.Trim(), StringComparison.CurrentCultureIgnoreCase) && Argon2.Verify(it.pass, info.password, null)); if (user != null) { output.auth = true; output.login = user.login; output.nickName = user.nick_name; output.password = info.password; output.photo = user.photo_fragment; user.online = true; user.last_use = SystemClock.GetInstance().CurrentTime; db.SaveChanges(); } else { output.auth = false; } } } catch (Exception e) { Console.WriteLine(e.Message.ToString()); output.auth = false; } return(output); }
private void NextProcessing(IClient fromClient, InputSocketDataInfo inputObject) { OutputSocketMessage outputSocket; Type findClass = Type.GetType($"RAIDAChatNode.Reflections.{inputObject.execFun}", false, true); if (findClass != null) { if (findClass.Equals(typeof(Authorization))) { AuthSocketInfo info = new Authorization().Execute(inputObject.data); if (info.auth) { info.client = fromClient; if (!mClients.Any(it => it.client.Equals(fromClient))) { mClients.Add(info); } outputSocket = new OutputSocketMessage(inputObject.execFun, inputObject.actId, true, "", new { info.nickName, info.login, img = info.photo }); //Организовать отправку остальным пользователям о подключении клиента using (var db = new RaidaContext()) { Members iam = db.Members.Include(m => m.MemberInGroup) .First(it => it.login.Equals(info.login)); SendMessageOfChangeUser(db, iam); } Console.WriteLine($"{JsonConvert.SerializeObject(outputSocket)}"); SendMessage(fromClient, outputSocket); return; } else { Console.WriteLine("Invalid login or password"); outputSocket = new OutputSocketMessage(inputObject.execFun, Guid.Empty, false, "Invalid login or password", new { }); SendMessage(fromClient, outputSocket); return; } } else { if (mClients.Any(it => it.client == fromClient) || findClass.Equals(typeof(Registration))) { IReflectionActions execClass = (IReflectionActions)Activator.CreateInstance(findClass); AuthSocketInfo currentUser = mClients.FirstOrDefault(it => it.client == fromClient); OutputSocketMessageWithUsers response = execClass.Execute(inputObject.data, currentUser?.login, inputObject.actId); Console.WriteLine($"{JsonConvert.SerializeObject(response.msgForOwner)}"); SendMessage(fromClient, response.msgForOwner); if (response.msgForOwner.success) { Action <AuthSocketInfo> action = delegate(AuthSocketInfo s) { SendMessage(s.client, response.msgForOther); }; mClients.Where(it => response.forUserLogin.Contains(it.login)).ToList().ForEach(it => { Console.WriteLine($"SendFor {it}"); action(it); }); } } else { Console.WriteLine("You are not authorized. To continue working you need to login."); outputSocket = new OutputSocketMessage(inputObject.execFun, Guid.Empty, false, "You are not authorized. To continue working you need to login.", new { }); SendMessage(fromClient, outputSocket); return; } } } else { Console.WriteLine($"Function: '{inputObject.execFun}' not found"); outputSocket = new OutputSocketMessage(inputObject.execFun, Guid.Empty, false, $"Function: '{inputObject.execFun}' not found", new { } ); SendMessage(fromClient, outputSocket); return; } }