问题
I'm working on a multiuser chatting application on adhoc network, and one of its features is to allow the user to chat with more than one friend at the same time. Can anybody tell me when to start? Can i use socket programming for this... Is there any other way to do it?
回答1:
You would need to implement a publisher subscriber model.Basically have each of your users implement an interface
interface chatWithUser {
public String getMessage();
public List<ChatWithUser> getFriends();
}
All these users are subscribers to the chat feature. Now we will have a publisher which would have a list of subscribers. Now when a request for chat comes from a users socket the publisher will check the list of users
public class PublishChattMessage
{
List<chatWithUser> userChatList;
public void setUpChat(ChatUser mainUser )
{
for(ChatWithUser user:userChatList)
{
List<ChatUser> userList = user.getFriends()
// set up connections with all friends
}
}
/**
* Method to chat between two users. u
**/
public void chat(ChatUser userOne,ChatUser user2)
{
}
}
Once the connection is set up with all friends then call the chat method and display the messages
来源:https://stackoverflow.com/questions/19080636/multiuser-chat-application-on-adhoc-network-in-java