public static void AddUser(TcpClient tcpUser, string curentMessage) { // First add the username and associated connection to both hash tables message = message.Deserialize(curentMessage); ChatServer.htUsers.Add(message.UserName, tcpUser); ChatServer.htConnections.Add(tcpUser, message.UserName); ChatServer.htUserNameSmileName.Add(message.UserName, message.SmileName); OnUserStatusChanged(message.UserName, message.SmileName); // Tell of the new connection to all other users and to the server form SendAdminMessage(htConnections[tcpUser] + userJoinUs, (string)htConnections[tcpUser]); }
public string Serialize(Message message) { lock (sync) { string result = string.Format(format, message.UserName, message.SmileName, message.NewUser, message.PrivateChat, message.FriendName, message.CurrentMessage); return result; } }
// Occures when a new client is accepted private void AcceptClient() { srReceiver = new System.IO.StreamReader(tcpClient.GetStream()); swSender = new System.IO.StreamWriter(tcpClient.GetStream()); // Read the account information from the client string m = srReceiver.ReadLine(); strMessage = strMessage.Deserialize(m); currUser = strMessage.UserName; // srReceiver.ReadLine(); // We got a response from the client if (IsSuccessfullyConnected(m)) { try { // Keep waiting for a message from the user while (!string.IsNullOrEmpty((strResponse = srReceiver.ReadLine()))) { lock (sync) { // If it's invalid, remove the user if (strResponse == null) { ChatServer.RemoveUser(tcpClient); } else { // Otherwise send the message to all the other users strMessage = strMessage.Deserialize(strResponse); ChatServer.SendMessage(currUser, strMessage.CurrentMessage); } } } } catch { // If anything went wrong with this user, disconnect him ChatServer.RemoveUser(tcpClient); } } }
public Message Deserialize(string currentMessage) { lock (sync) { Message message = new Message(); string[] str = currentMessage.Split(splitter.ToCharArray()); if (str.Length == 6) { message.UserName= str[0]; message.SmileName= str[1]; message.NewUser= str[2]; message.PrivateChat= str[3]; message.FriendName= str[4]; message.CurrentMessage = str[5]; } return message; } }