Sorry , My English is not very good, so I holp you can understand what I will say T_T
in my programe. when I receive a Message, I want to dispath the message to some people. ( I don’t use the group chart, because the message can’t be send to everyone in the group).
private Map userArray;
private Map chatArray; // in RosterListener , I add new user to the chatArray and userArray
@Override
public void presenceChanged(Presence presence)
{
String userEmailAddress = org.jivesoftware.smack.util.StringUtils.parseBareAddress(presence.getFrom());
RosterEntry entry = model.getRoster().getEntry(userEmailAddress);
/*
* online or off line
*/
if (!presence.isAway()) {// // add new online user
if (!this.getUserArray().containsValue(entry)) {
this.getUserArray().put(userEmailAddress, entry);
Chat chat = model.getChatmanager().createChat(userEmailAddress, this.messageListener);
this.getChatArray().put(userEmailAddress, chat);
}
} else {// 用户离线
model.getUserArray().remove(entry.getUser());
Chat chat = model.getChatArray().get(entry.getUser());
chat.removeMessageListener(this.messageListener);// this.chartArray.getChatArray().remove(entry.getUser());//
this.logger.info("we lost a user !");
}
} // in messageListener , when I receive a message, I dispath it to @Override
public void processMessage(Chat chat, Message req)
{
String senderId = chat.getParticipant();
String msgString = req.getBody();
// ignore the empty message
if (StringUtils.isBlank(msgString)) {
return;
}
this.logger.info("we get a message : " + senderId + " : " + msgString);
/**
* get send user entry
*/
try {
// get user name and id
String senderId = chat.getParticipant()
// compose message
StringBuffer sb = new StringBuffer();
sb.append(senderName).append(" Said: ").append(req.getBody);
//get all connected users
Iterator<Entry<String, Chat>> it = this.chartArray.entrySet().iterator();
while (it.hasNext()) {
Entry<String, Chat> e = it.next();
String key = e.getKey();
Chat receiver = e.getValue();
String receiverId = receiver.getParticipant();
if (!senderId.equalsIgnoreCase(receiverId)) {
receiver.sendMessage(sb.toString());
}
}
} catch (Exception e) {
this.logger.error(e.toString());
throw new BaseException(e.toString(), "ROB-ERR-00006");
}
}
I want it work like this way: this programe connect to server as user R. user A, B, C in online, when A send a massage like “Hello” to user R (the programe) , messageListener.processMessage() was called. then the programe send this message like " A said: Hello " to B and C.
now my problem is, at first, this programe run well. but after a while, the method messageListener.processMessage() was also be called when
this programe send the message like " A said: Hello " to B and C.
the console log look like this:
A said: Hello
B said: A said: Hello
C said: B said: A said: Hello
C said: B said: A said: Hello
A said: B said: A said: Hello
A said: B said: A said: Hello
…and so on , the programe drop into an endless loop.
I think the method messageListener.processMessage() will only be called when other user send message to the programe. and I also read the ducument and the API.
But Why in My programe it alse be called when I send message use the function chat.sendMessage() !!!
Can you help me ? I’m waitting for your help,