2

我在 localhost 上使用 SMACK api 和 OpenFire 创建了一个简单的客户端。到目前为止,我有两个用户说 X 和 Y。每当有人登录到客户端时,都会有一个发送按钮向用户 Y 发送消息。我已经检查了 XMPP 调试管理器,我可以发送/在那里接收消息。但是,我无法在分配用于显示消息的 JLabel 中显示这些消息。如果用户 Y 点击发送按钮,它会根据功能向自己发送一条消息,该消息会出现在 JLabel 上。但是当用户 X 点击发送按钮时,会收到消息,但是它不会在 Y 的 JLabel 上显示/更新。

有趣的是,在 gtalk 配置下一切正常。

结构如下:

WelcomeUser.java: 是带有用户名和密码提示的欢迎表单。在点击登录按钮时:它将 ChatBoard 类设置为可见并将用户名密码传递给 ChatBoard 的构造函数。代码:

chBoard=new ChatBoard(username.getText(),password.getText());
                chBoard.setVisible(true);
                setVisible(false);

这是 ChatBoard.java 的代码:

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Collection;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.EmptyBorder;

import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;

public class ChatBoard extends JFrame implements MessageListener {

    private static final long serialVersionUID = 1L;
    private JPanel contentPane;
    static String username, password;
    static XMPPConnection connection;
    private JTextField textField;
    static JLabel board = new JLabel("");
    static Chat chat;

    public void sendChat() {
        try {
            chat = connection.getChatManager().createChat(
                    "userY@my-pc/Smack", this);
            chat.sendMessage("Hello, This is " + username);
        } catch (XMPPException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }


    /**
     * Create the frame.
     */
    public ChatBoard(String user, String pass) {

        username = user;
        password = pass;

        ////  
                   ///JFrame creation code ommited for this thread.
        // ////////////////////////////////////////////

        XMPPConnection.DEBUG_ENABLED = true;

        ConnectionConfiguration config = new ConnectionConfiguration(
                "127.0.0.1");


        connection = new XMPPConnection(config);

        try {
            connection.connect();
            System.out.println("Connected...");
        } catch (XMPPException e) {

            e.printStackTrace();
            System.out.println("Not Connected. Error :" + e.getMessage());
        }

        try {
            connection.login(username, password);
            System.out.println("Logged in...");
        } catch (XMPPException e) {

            e.printStackTrace();
            System.out.println("Not Logged in. Error :" + e.getMessage());
        }

        Roster roster = connection.getRoster();
        Collection<RosterEntry> entries = roster.getEntries();

        System.out.println("\n\n" + entries.size() + " buddy(ies):");
        for (RosterEntry r : entries) {
            System.out.println(r.getUser());
        }

        // /////////////////////////////////

    }

    @Override
    public void processMessage(Chat arg0, Message arg1) {
        // TODO Auto-generated method stub

        board.setText(board.getText() + arg1.getBody());
        System.out.println(arg1.getFrom() + ":" + arg1.getBody().toString());

    }

}

我是新手,如果有任何愚蠢的错误,请原谅我。您的帮助将不胜感激。

杂项。详细信息:本地主机上的 OpenFire 3.7.1 和 SMACK 3.2.2

4

3 回答 3

1

看看http://www.igniterealtime.org/builds/smack/docs/latest/documentation/messaging.html的底部。对于来自其他用户的传入聊天,您必须覆盖 ChatManagerListener

你可以在 processMessage 上设置一个断点,它是否命中了该断点?如果是这样,可能只是确保:

board.setText()

发生在Swing 事件调度线程上

所以试着让你的 processMessage 像这样:

@Override
public void processMessage(Chat arg0, Message arg1) {
    // TODO Auto-generated method stub

    try
    {
        SwingUtilities.invokeAndWait(new Runnable()
        {

            @Override
            public void run()
            {
                board.setText(board.getText() + arg1.getBody());
            }
        });
    }
    catch (InvocationTargetException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    catch (InterruptedException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    System.out.println(arg1.getFrom() + ":" + arg1.getBody().toString());

}
于 2012-12-13T11:01:34.677 回答
0

我在我的代码中提到的构造函数的末尾添加了这段代码,现在它似乎可以工作了:-) 谢谢 alex.p!

connection.getChatManager().addChatListener(new ChatManagerListener() {



                public void chatCreated(final Chat chat, final boolean createdLocally) {

                    chat.addMessageListener(new MessageListener() {



                        public void processMessage(Chat chat, Message message) {

                            //JOptionPane.showMessageDialog(null, "Rec: For " + chat.getParticipant() + " from " + message.getFrom() + "\n" + message.getBody());

                            String sender=message.getFrom();

                            String senderNames=sender;

                            System.out.println("Received message: " + (message != null ? message.getBody() : "NULL"));

                            board.setText(board.getText()+"\n "+ message.getFrom()+" : "+ message.getBody());
                            //PrivateChatManager.receiveMessage(sender, senderNames, message.getBody());



                        }

                    });

                }
        });
于 2012-12-13T12:51:56.490 回答
0

希望这段代码可以帮助你:

public void processMessage(Chat aChat, Message aMessage) {

    final XmppBot bot = this.xmppBot;
    final Chat chat = aChat;
    final Message message = aMessage;

    log.debug("message received, type {}", message.getType().toString());


    if (message.getBody() != null) {

        Runnable runnable = new Runnable() {

            public void run() {

                Thread.currentThread().setName("Chat: "+chat.getParticipant()+ " Message: "+ message.getBody());

                PrintWriter threadPrintWriter = null;

                threadPrintWriter = new PrintWriter(new ChatWriter(chat));

                bot.getContext().setChat(chat);
                bot.getContext().setPrintWriter(threadPrintWriter);

                bot.processCommand(message.getBody());

                bot.getContext().removeMultiUserChat();
                bot.getContext().removePrintWriter();

            }
        };

        Thread t = new Thread(runnable);
        t.start();
    }

}
于 2018-06-26T02:36:17.543 回答