[xmpp] SMACK提供的MessageListener接口的processMessage方法疑问

jade_phenix 2010-08-09
在我的程序里用的是SMACK包来实现的XMPP协议的实现。需求是根据对方发来的文本格式为:@<命令> <参数> <消息内容> 的信息来处理把信息转发给特写的一个或多个人。

我的实现方法是:每当好友的在线情况发生变化时,同步维护一个在线好友的Chat列表。当MessageListener接口的processMessage方法中解析命令,并用chat.sendMessage()把消息发给其他的一个或多个人。

现在的问题是,在几个人轮流说话以后,从我这里发出去的消息也会被MessageListener接口的processMessage方法给接收到。程序进入了一个无限循环当中。

这个问题我GOOGLE过,查过手册和API,也到JIVE论坛上发了贴,但还是找不到答案。真是绝望了。
    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");
        }
       
    }
Global site tag (gtag.js) - Google Analytics