Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Chat application implementation method?

 
Trevor Keast
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Trevor Keast
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic