Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Multithreaded Chat server(Rooms Creation) help....

 
DeepakB
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Friends .
Can any body explain me how to create Private Room in java Chat server application . actually i am making a chat server application which has a 5 (five ) chat room ,but i couldn't understand how i can do that. This five chat room will display of total no of USERS Currently present in each rooms, i am using TCP/IP connection for connect to server.

Thanx in Advance
Deepak
 
Tanveer Rameez
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
First if you are not familiar with the applet-servlwet communucation which you will need to have a chat applet talking with servlet you can go through the book-servlet programming from O'Reilly.However the books contains code for only 1 room.
If you want N number of rooms(and you can increase the funtionality to have any number of rooms you wants and the users can also create rooms), I suggest some approach somehting like this:
First create Room class say something like this
<code>
class Room{
String RoomName,RoomDescription;
Vector UserList;
Vector Messages;
Int MaxUserAllowed;
Date WhenCreated;
public Room(String name,String desc,int maxusers,Date Created)
{
RoomName=name; RoomDescription=desc;
MaxUserAllowed=maxusers;
WhenCreated=Created;
UserList=new Vector();
Messages=new Vector();
.....
}
public void getMessagesFromRoom(HttpServletResponse res, String username)
{
.....
}
public void postMessage(String message)
{
.....
}
}
}
</code>
In your main class 'ChatServlet', in the init have a HashTable to hold the rooms with the roomname as the key.
Say:
RoomHashTable.put("Mumbai",new Room("Mumbai","Room for the Mumbaikars",100,new Date()));
RoomHashTable.put("Delhi",new Room("Mumbai","Room for the Delhiites",100,new Date()));
you can as many rooms as you want to.
The user can recieve messages from the rooms by using the doGet method.
The applet/html page can pass name=value parameters for example: roomname=Mumbai username=Raj
In The servlet's doGet you can obtain the roomname and direct the response to that room:
Room CurrentRoom=(Room)RoomHashTable.get(request.getParameter("roomname"));
if(CurrentRoom==null) response.write("sorry, no such room");
else CurrentRoom.getMessagesFromRoom(response,response.getParameter("username"));
The CurrentRoom.getMessagesFromRoom can insert the messages from its own Message vector into the response and laso insert the username into ints UserList vector if it has not been entered before.
The user can post messages using the doPost method.
In the servlet's doPost method you can obtain the roomname and direct the response to that room:
Room CurrentRoom=(Room)RoomHashTable.get(request.getParameter("roomname"));
if(CurrentRoom==null) response.write("sorry, no such room");
else CurrentRoom.postMessage(response.getParameter("username")+":"+response.getParameter("message"))
The CurrentRoom.postMessages can insert the message into its Message vector.
The CurrentRoom.UserList.size() gives the size of users in that room. The sum of all the users in all the room present in the RoomHashTable gives the total number of users in the chat servlet.
Hope this note helps you. Actually I used this approach a chat server. You can visit my site and go to my java page: http://tanveer.freeservers.com/java.htm
to see some more codes and notes on more professional applet chatting and html-chatting with more options like user created rooms, administrative services, private messaging(like in Yahoo), image/file exchange between users etc.
regards
Tanveer
------------------
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic