Reputation: 3819
so basically I'm trying to build a simple Client-Server program that allows users to log in and communicate with each other.
I have written the code that allows one user to "whisper" a message to another user without other users, who are logged on, to be able to see the private message.
I am able to allow only "one" user to see the message, but am having trouble allowing more users to see the private message.
For example, here's my code to allow a user to see a private message to him:
string restOfMessage = "/w bob hi bob";
/w bob hi bob
the /w is the command to whisper, bob is the user whispering to, and hi bob is the message
To extract bob, I did this:
StringBuffer userMessage = new StringBuffer();
buffer.append(restOfMessage.substring(restOfMessage.indexOf(' ') + 1,
restOfMessage.length()));
String whisperUser = buffer.substring(0, buffer.indexOf(" "));
Then I follow the same methods to extract hi bob
I have an iterator that iterates through a HashMap of online users, and if that online user matches bob I send him a message.
The 1st problem is, how do I go about extract more than one user to send a message to? i.e. if the code was:
/w bob mike john hi guys
The 2nd problem is, how would I store the 3 users in such a way that allow me to iterate through the HashMap and send it to only those 3 users and not the rest?
Thanks for your help!
Upvotes: 0
Views: 54
Reputation: 6909
Maybe define some separator characters.
"/w bob mike john: hi bob"
The colon could act as the start of the message.
Or define users in a different way:
"/w @bob @mike @john hi bob"
There are many ways to go about this, you need to define a good layout and stick with it. If this is just a casual application, you could define your own random ones. But look into IRC as thats been around for years. A good place to look is the RFC documents. The most relevant are
Upvotes: 0
Reputation: 18633
You could probably split the string around space characters (), iterate over the result, and consider the message to be everything starting from and including the first word that doesn't match a valid user name. Everything before that, it's users who should get the message.
Alternatively, you could have a different syntax e.g.
/w bob,mike,john hi bob
where you know after a ,
you're expecting another user name.
Upvotes: 1