// Method: Create new chat room. public void CreateChatRoom(ClientConnection client) { // Create new local chat room, assign room name, add owner of chat room. string chatRoomName = "S2R" + _chatRoomCount.ToString(); _chatRoomCount++; LocalChatRoom newChatRoom = new LocalChatRoom(this, chatRoomName, client.ThisUser); // Update chat room list. _localCR.Add(newChatRoom); // Update user's current chat room. client.ThisUser.CurrentChatRoom = newChatRoom; // Remove user from main hall. _mainHall.RemoveUser(client.ThisUser); Console.WriteLine("Update chat room list: " + _localCR.ToString()); // Preparing the packet to echo back to client. _packetSent.title = MsgTitle.confirm_created.ToString(); _packetSent.time = _now; _packetSent.content = "created " + newChatRoom.RoomName + " you can start chatting now"; _packetSent.IP = client.ClientSocket.LocalEndPoint.ToString(); _packetSent.sender = this.ServerName; // Sending packet. client.sendMsg(_packetSent); if (_peerServerDict.Count != 0) { // Preparing packet to send to other servers. _packetSent.title = MsgTitle.add_chatroom.ToString(); _packetSent.content = chatRoomName; foreach (var entry in _peerServerDict.Values) { _packetSent.IP = entry.ClientSocket.LocalEndPoint.ToString(); entry.sendMsg(_packetSent); } } }
// Method: Add an user to a chat room when they request to join. public void JoinChatroom(ClientConnection client) { // Start preparing common features of packet. _packetSent.time = _now; _packetSent.IP = client.ClientSocket.LocalEndPoint.ToString(); string chatRoom = null; bool roomAlreadyExisted = false; foreach (ChatRoom room in _localCR) { // Check if the room requested exists. if (_packetReceived.content.Equals(room.RoomName)) { roomAlreadyExisted = true; // Remove user from main hall. _mainHall.RemoveUser(client.ThisUser); // Adding user to the room. room.AddUser(client.ThisUser); // Updating user's current room. client.ThisUser.CurrentChatRoom = room; // Preparing packet to be sent. _packetSent.content = "Joined " + room.RoomName + ". You can start sending message now"; _packetSent.title = MsgTitle.confirm_joined.ToString(); _packetSent.sender = this.ServerName; chatRoom = room.RoomName; break; } } // If room does not exist if (!roomAlreadyExisted) { // Create new room and assign name. string roomName = "S2R" + _chatRoomCount.ToString(); LocalChatRoom newChatRoom = new LocalChatRoom(this, roomName, client.ThisUser); // Add chat room to server's list of chat room. _localCR.Add(newChatRoom); // Remove user from main hall. _mainHall.RemoveUser(client.ThisUser); // Adding user to newly created room. newChatRoom.AddUser(client.ThisUser); // Updating user's current room. client.ThisUser.CurrentChatRoom = newChatRoom; // Preparing packet to be sent. _packetSent.content = "Room does not exist. Created a new room: " + roomName; _packetSent.title = MsgTitle.confirm_created.ToString(); _packetSent.sender = this.ServerName; chatRoom = roomName; } client.sendMsg(_packetSent); if (_peerServerDict.Count != 0) { // Preparing packet to send to other servers. _packetSent.title = MsgTitle.client_to_chatroom.ToString(); _packetSent.content = chatRoom + " " + client.ThisUser.UserName; foreach (var entry in _peerServerDict.Values) { _packetSent.IP = entry.ClientSocket.LocalEndPoint.ToString(); entry.sendMsg(_packetSent); } } }