public override Response Handle(Request request) { ClientStatus status = ClientStatus.GetInstance(Thread.CurrentThread.ManagedThreadId.ToString()); status.Chat = null; return(new Response(ResponseStatus.Success, onRequest: request.Type)); }
public override Response Handle(Request request) { ClientStatus status = ClientStatus.GetInstance(Thread.CurrentThread.ManagedThreadId.ToString()); try { List <List <string> > data = db.Execute(String.Format("SELECT id FROM users WHERE username = '******' and password = '******'", request.Data["username"], request.Data["password"])); if (data.Count != 0) { status.User = new User(int.Parse(data[0][0])); Dictionary <string, string> ResponseData = new Dictionary <string, string> { ["Username"] = status.User.Username }; return(new Response(status: ResponseStatus.Success, onRequest: request.Type, data: ResponseData)); } else { return(ResponseError.DefineError(request, ResponseErrorType.InvalidUsernamePasswordError)); } } catch (MySqlException) { return(ResponseError.DefineError(request, ResponseErrorType.DatabaseAccessError)); } }
public override Response Handle(Request request) { ClientStatus status = ClientStatus.GetInstance(Thread.CurrentThread.ManagedThreadId.ToString()); return(new Response(status: ResponseStatus.Success, onRequest: request.Type, data: new Dictionary <string, string> { ["Chats"] = status.User.SerializedChats() })); }
private void AcceptIncomingConnections() { while (true) { SocketHandler SocketHandler = new SocketHandler(listenSocket.Accept()); RequestReceiver client = new RequestReceiver(SocketHandler); Thread currentUserThread = new Thread(client.StartReceiving); currentUserThread.Start(); Clients.Add(SocketHandler, ClientStatus.GetInstance(currentUserThread.ManagedThreadId.ToString())); } }
public override Response Handle(Request request) { ClientStatus status = ClientStatus.GetInstance(Thread.CurrentThread.ManagedThreadId.ToString()); Chat chat = new Chat(int.Parse(request.Data["NewChatId"])); if (!UserIsInChat(chat)) { return(ResponseError.DefineError(request, ResponseErrorType.InvalidChatError)); } else { status.Chat = chat; return(new Response(ResponseStatus.Success, onRequest: request.Type)); } }
public override Response Handle(Request request) { try { string user_id = ClientStatus.GetInstance(Thread.CurrentThread.ManagedThreadId.ToString()).User.Id.ToString(); db.Execute(String.Format("INSERT INTO chats (title, creator,creation_date) VALUES ('{0}','{1}',now())", request.Data["Title"], user_id)); List <List <string> > chat = db.Execute(String.Format("SELECT id, title FROM chats WHERE title = '{0}' and creator = '{1}' ORDER BY id DESC LIMIT 1", request.Data["Title"], user_id)); db.Execute(String.Format("INSERT INTO userchat (user_id,chat_id) VALUES ('{0}','{1}')", user_id, chat[0][0])); return(new Response(status: ResponseStatus.Success, onRequest: request.Type, data: new Dictionary <string, string> { ["Chat"] = JsonConvert.SerializeObject(chat[0]) })); } catch (MySqlException) { return(ResponseError.DefineError(request, ResponseErrorType.DatabaseAccessError)); } }
public override Response Handle(Request request) { Chat Chat = new Chat(int.Parse(request.Data["Chat"])); ClientStatus status = ClientStatus.GetInstance(Thread.CurrentThread.ManagedThreadId.ToString()); if (!UserIsInChat(Chat)) { return(ResponseError.DefineError(request, ResponseErrorType.InvalidChatError)); } else { Dictionary <string, string> ResponseData = new Dictionary <string, string> { ["Author"] = status.User.Username, ["Message"] = request.Data["Message"] }; foreach (KeyValuePair <SocketHandler, ClientStatus> ClientData in Server.Clients) { if (ClientData.Value.User.IsParticipant(Chat) && ClientData.Value.Chat != null && ClientData.Value.Chat.Id == Chat.Id) { Console.WriteLine("SENDED TO {0}", ClientData.Value.User.Username); ClientData.Key.Send(new Response(status: ResponseStatus.Success, onRequest: RequestType.SpreadMessage, data: ResponseData)); } } try { Chat.AddMessage(status.User, request.Data["Message"]); } catch (MySqlException) { return(ResponseError.DefineError(request, ResponseErrorType.DatabaseAccessError)); } return(new Response(ResponseStatus.Success, onRequest: request.Type)); } }
protected bool UserIsInChat(Chat chat) { return(ClientStatus.GetInstance(Thread.CurrentThread.ManagedThreadId.ToString()).User.GetChats().Find(_chat => _chat.Id == chat.Id) != null); }