public ChatService() { _sessions = new Dictionary <Guid, IChatResult>(); _sessionDetails = new Dictionary <Guid, string[]>(); _clients = new Dictionary <string, IChatResult>(StringComparer.OrdinalIgnoreCase); List <string> clients = ChatDBAccess.GetAllUsers(); foreach (string client in clients) { _clients.Add(client, null); } }
public bool SendMessage(Guid session, string text) { try { //Reciever still online if (_sessions.ContainsKey(session)) { string sender = _sessionDetails[session][0]; string reciever = _sessionDetails[session][1]; Message message = new Message() { Receiver = _sessionDetails[session][1], Sender = _sessionDetails[session][0], SentTime = DateTime.Now, Text = text }; ChatDBAccess.AddMessage(message); Report($"Message fron {sender} to {reciever} added to DB", LogLevel.Information); SendMessagesListToClient(sender, reciever); SendMessagesListToClient(reciever, sender); Report($"Message recieved by {reciever}", LogLevel.Information); return(true); } //Reciever offline { Report($"Message was not recieved - reciever offline", LogLevel.Warning); return(false); } } catch (Exception e) { Report($"Message was not sent", LogLevel.Exception); Report(e.Message, LogLevel.Exception); return(false); } }
public bool Register(string userName, string password) //Add user to DB and log in { try { if (ChatDBAccess.AddUser(userName, password)) { Report($"User {userName} added to DB", LogLevel.Information); _clients.Add(userName, null); Login(userName, password); return(true); } else { Report($"User {userName} already exists in DB. Please choose another name", LogLevel.Information); return(false); } } catch (Exception e) { Report(e.Message, LogLevel.Exception); return(false); } }
private void SendMessagesListToClient(string sender, string reciever) { List <Message> messages = ChatDBAccess.GetListOfMessages(sender, reciever); _clients[sender].GetMessageList(messages, reciever); }