Java Multi-Client Instant Messenger concept help
I am try to implement a Java chat program. In this program multiple clients will talk to each other via a server. The server will relay messages from on client to another. It will also provide updates to a buddy list. Conceptually what would be the best route to take to implement this IM chat. The part I am having trouble with is making the server relay messages from one client to another. Each client should also be able to choose who they want to开发者_开发知识库 talk to.Once again the only purpose of the server is to take messages from one client and send it to another client. If one client is connected to the server nothing happens till another client connects. Then both clients begin to communicate to each other.
You'll need to come up with implementing a protocol; check out XMPP. Part of the issue is determining which other user you will be sending the message to. XMPP helps to solve that issue. I would suggest that each user that connects to the server would have a thread to handle the connection. Then you can store all of the threads into a HashMap where the key would be the username. Then just push messages onto the thread implementation which would send the message(s) to the client.
For a basic chat system you will want the following features:
- A client logs into your server. This allows you to identify the client and to keep friend lists for each client.
- A client only ever sends messages to the server.
- A client only ever receives messages from the server.
- Each message must identify the following:
- The recipient of the message.
- The source of the message.
- The text of the message.
- Maybe; time and date the message was sent.
- The server will need to track conversations; basically a list of client pairs.
- If you want to do group chats, then the conversion will be a list of client lists.
- When a client sends a message to the server, the server should forward it to all other clients that are part of the conversation.
- You probably want to have the server log all conversations.
- You will need at least 2 message types; text (i.e. chat) and invite.
精彩评论