/// <summary> /// Handle Chat User Whisper /// <param name="toUser">The whisper target</param> /// <param name="Message">The message written by Chat User</param> /// <param name="connID">Connection ID provided by server</param> /// </summary> public MethodResponse Whisper(string toUser, string Message, int connID) { MethodResponse MethodResponse = new MethodResponse(); if (GetUserByName(toUser) != null) //Check if whisper target is online { //Create a list with Connection ID to which the server will send the Packet List <int> toUsers = new List <int>(); toUsers.Add(GetUserByName(toUser).connID); Packet MessageResponse = new Packet("WHISPER_RESPONSE", false, toUsers); //Create a new Packet WHISPER_RESPONSE providing the list with Connections ID MessageResponse.AddString(GetUserByConnID(connID).Name); //Add the whisper sender name MessageResponse.AddString(Message); //Add the message written by sender MethodResponse.AddPacket(MessageResponse); //Add Packet to MethodResponse } else { Packet MessageResponse = new Packet("WHISPER_RESPONSE"); //Create a new Packet WHISPER_RESPONSE MessageResponse.AddString(toUser); //Add the name of whisper target MessageResponse.AddString("currently offline"); MethodResponse.AddPacket(MessageResponse); //Add Packet to MethodResponse } return(MethodResponse); //Return MethodResponse to Server }
/// <summary> /// Handle Chat User Login /// <param name="username">Username given by Chat User</param> /// <param name="password">Password given by Chat User</param> /// <param name="connID">Connection ID provided by server</param> /// </summary> //Public method must return a result of type MethodResponse public MethodResponse Login(object username, object password, int connID) { //Create a new MethodResponse MethodResponse MethodResponse = new MethodResponse(); //Check if user exists from mysql //DataRow Row = MysqlConn.ReadDataRow("SELECT * FROM users where username = '******' AND password = '******'"); bool loginFailed = true; if (password.ToString() == "password") { loginFailed = false; } //if (Row != null) //{ //loginFailed = false; //} if (loginFailed) { //Create a new Packet LOGIN_RESPONSE and send Packet to the sender Packet LoginResponse = new Packet("LOGIN_RESPONSE"); //Add a boolean value to Packet. It means login failed LoginResponse.AddBoolean(false); //Add Packet to MethodResponse MethodResponse.AddPacket(LoginResponse); } else { Packet LoginResponse = new Packet("LOGIN_RESPONSE"); LoginResponse.AddBoolean(true);//It means successful login //Add a int value to Packet. It provides client the connection ID for future use LoginResponse.AddInt32(connID); //Announce to all clients a new user joined //Set sendToAll parameter to true (default false) if you want to send Packet to all clients Packet UserJoin = new Packet("USER_JOIN", true); //Add the name of the Chat User joined UserJoin.AddString(username.ToString()); //Add Packets to MethodResponse MethodResponse.AddPacket(LoginResponse); MethodResponse.AddPacket(UserJoin); Users.Add(new User(connID, username.ToString())); //Add the Chat User to a List //Write on server console from dll Logging.WriteLine("User: "******" has joined the chat", LogLevel.Information); } return(MethodResponse); //Return MethodResponse to Server }
/// <summary> /// Handle Chat User New Message /// <param name="Message">The message written by Chat User</param> /// <param name="connID">Connection ID provided by server</param> /// </summary> public MethodResponse Message(string Message, int connID) { MethodResponse MethodResponse = new MethodResponse(); Packet MessageResponse = new Packet("MESSAGE_RESPONSE", true); //Create a new Packet MESSAGE_RESPONSE and set sendToAll to true MessageResponse.AddString(GetUserByConnID(connID).Name); //Add the name of the Chat User using Connection ID MessageResponse.AddString(Message); //Add the message written by Chat User MethodResponse.AddPacket(MessageResponse); //Add Packet to MethodResponse return(MethodResponse); //Return MethodResponse to Server }