//CHANGE ROOM private void action_change_room() { //store old room number int oldroom = room; //Remove the user from the chat room chatserver.RemoveRoomUser(chatserver.RoomUsers[oldroom - 1], nickname); //Assigned to No room first room = 0; //while no room is assigned while (room == 0) { //Get room number from client chatserver.Write(client.GetStream(), ChatProtocolValues.CHOOSE_ROOM(nickname, chatserver.NumRoom)); string temp = chatserver.Read(client.GetStream()); //check for valid room number try { //convert the text message to integer int r_n = int.Parse(temp); //check to make sure that the room number is within range if ((r_n >= 1) && (r_n <= chatserver.NumRoom)) { room = r_n; } } catch {} } //Add user to the assigned room chatserver.AddRoomUser(chatserver.RoomUsers[room - 1], nickname); //Broadcast to old room participants chatserver.Broadcast(ChatProtocolValues.MOVE_TO(nickname, room), oldroom); //Broadcast to new room participants chatserver.Broadcast(ChatProtocolValues.WELCOME(nickname, room), room); }
//The main method that handle all the chat communication with the client private void HandleClient() { try { //Autheticate the user AuthenticationServer auth = new AuthenticationServer(chatserver, client); if (!auth.Authenticate()) { throw(new Exception()); } //Send connection message to client //returning the user id chatserver.Write(client.GetStream(), ChatProtocolValues.CONNECTION_MSG(auth.UserID)); //buffer byte[] bytes = new byte[256]; //set nickname as user id which is alraedy unique nickname = auth.UserID; //Assign a room to connected client room = chatserver.AssignRoom(nickname); Console.WriteLine("room assigned= " + room + " for " + nickname); //Add to Connections try { chatserver.AddConnection(nickname, client); } catch {} //Broadcast to chat room about the new user chatserver.Broadcast(ChatProtocolValues.WELCOME(nickname, room), room); //listen to all client data send while ((readdata = chatserver.Read(client.GetStream())) != "") { readdata = readdata.Trim(); //Print out read data in console Console.WriteLine("Read>" + readdata); //Check if the readdata is a command if (readdata.ToUpper().Substring(0, 1) == ChatProtocolValues.IS_CMD) { //Assign a default action action = new SocketHelperAction(action_default); //Reassign action based on format content if ((readdata.ToUpper() + ":").IndexOf(ChatProtocolValues.GET_PIC_CMD) == 0) { action = new SocketHelperAction(action_get_pic); } if ((readdata.ToUpper() + ":").IndexOf(ChatProtocolValues.SEND_PIC_CMD) == 0) { action = new SocketHelperAction(action_send_pic); } if ((readdata.ToUpper() + ":").IndexOf(ChatProtocolValues.GET_MEDIA_CMD) == 0) { action = new SocketHelperAction(action_get_media); } if ((readdata.ToUpper() + ":").IndexOf(ChatProtocolValues.SEND_MEDIA_CMD) == 0) { action = new SocketHelperAction(action_send_media); } if ((readdata.ToUpper() + " ").IndexOf(ChatProtocolValues.HELP_CMD) == 0) { action = new SocketHelperAction(action_help); } if ((readdata.ToUpper() + " ").IndexOf(ChatProtocolValues.QUIT_CMD) == 0) { action = new SocketHelperAction(action_quit); } if ((readdata.ToUpper() + " ").IndexOf(ChatProtocolValues.CHANGE_ROOM_CMD) == 0) { action = new SocketHelperAction(action_change_room); } if ((readdata.ToUpper() + " ").IndexOf(ChatProtocolValues.WHICH_ROOM_CMD) == 0) { action = new SocketHelperAction(action_which_room); } if ((readdata.ToUpper() + " ").IndexOf(ChatProtocolValues.LIST_CMD) == 0) { action = new SocketHelperAction(action_list); } if ((readdata.ToUpper() + ":").IndexOf(ChatProtocolValues.PRIVATE_MSG_CMD) == 0) { action = new SocketHelperAction(action_private_message); } } else //NON COMMAND { //if not a command assign to a mesage sending action action = new SocketHelperAction(action_send_message); } //COMMANDS //perform the action action(); } //WHILE } catch (Exception e) { //Trapped exception Console.WriteLine("The following error is trapped by the chat server"); Console.WriteLine("*************************************************"); Console.WriteLine(e); Console.WriteLine("*************************************************"); Console.WriteLine("Waiting for Connection..."); } finally { //while loop ended or when there are some other problems //try to inform client to shut down try { chatserver.Write(client.GetStream(), ChatProtocolValues.QUIT_MSG); } catch {} //if the client had belong to a room if ((room != 0) && (nickname != "")) { //remove user from room chatserver.RemoveRoomUser(chatserver.RoomUsers[room - 1], nickname); //inform all that the client has logged out chatserver.Broadcast(ChatProtocolValues.USER_LOG_OUT(nickname, room)); } //remove the client connection try { chatserver.RemoveConnection(nickname, client); } catch {} } }