aspose file tools*
The moose likes Distributed Java and the fly likes Chat application implementation method? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Distributed Java
Bookmark "Chat application implementation method?" Watch "Chat application implementation method?" New topic
Author

Chat application implementation method?

Trevor Keast
Greenhorn

Joined: Apr 05, 2006
Posts: 4
Hi Guys,

I am new here and am hoping that you may be able to give me a few pointers.

I am writing a chat application in Java. I have one server which starts a new thread for each new client that connects. So far all pretty straight forward.

I have a client that connects to the server and sends over a username. The username is stored in the new thread instance. This way the server can distribute the chat name as well as the conversation text to all the clients so everyone knows who is talking.

That general chat bit works great but I want to have a list on every client that is always up to date containing the names of all users currently online. I also want to add extra functionality like text color perhaps.

What approach should I take to achieve this?? What would be the most sensible way of doing it?

I am using text streams to send all this information between server and clients. How can I separate the "conversation text" from all streams that contain system information like a users text colour?

Should I keep a list off all current users on every client and store everyones settings in there? If I do this how can I keep it updated?

Thanks in advance for your comments.

Trevor
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

Hi,

What sort of technology are you using to implement your chat application? RMI? Sockets? or something else?

If you are using RMI - add a new remote method to the server to get the user list, or you could make some sort of RMI callback on the client to add/remove users from the list as they sign in/out.

If you are using Sockets - you will need to define more than one type of message - and have the type you have now be the "text" message, and add a new "user list" message type.


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Trevor Keast
Greenhorn

Joined: Apr 05, 2006
Posts: 4
Thanks Nate,

I'm using sockets. I'm now thinking that I will keep a list of current users on all clients (sent out by the server).

I'm not quite sure I understand how I define two differnt types of message. I'm currently using a buffered reader and printwriter to stram the text, using a statement like:

out.println("message text here");
out.flush();

but i just dont know how to separate these conversation messages from the transfer of the user lists as they all get sent over in a text stream in the same way.

Thanks again,

Trevor
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

You would just pre-pend "headers" to the beginning of your message and pull them off when they are received on the client - something like -



When you receive one of these messages on a client, pull it apart using the separators ":[," (maybe using StringTokenizer). Look at the first "header" to figure out what kind of message it is - if it's a "MESSAGE", the first parameter says what user sent it, look in the user table to get the color, and print the "message text" section to the screen. If it's a "USER_LIST" - pull all the username/color pairs out of the message and update the user table. If it's a USER_ADDED or USER_REMOVED pull the parameter off and adjust the user table accordingly.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Chat application implementation method?