如何编写连接到服务器并回答用户的简单 smack 4.2.3 应用程序

Posted

技术标签:

【中文标题】如何编写连接到服务器并回答用户的简单 smack 4.2.3 应用程序【英文标题】:How to write simple smack 4.2.3 app that connects to server and answer to user 【发布时间】:2018-04-05 07:52:37 【问题描述】:

我有一个简单的 smack 3.2.1 应用程序。它连接到 xmpp 服务器 并等待另一个用户对话发起。当用户提问时 通过聊天,应用程序发送响应(答案)。它工作正常。这是代码:

JabberApplication.java:

package jabberapplication;

import org.jivesoftware.smack.XMPPException;

public class JabberApplication 

public static void main(String[] args) throws XMPPException,    InterruptedException 

    String username = "USERNAME";
    String password = "PASSWORD";
    String server = "SERVER";
    int port=5222;

    XmppManager xmppManager = new XmppManager(server, port);
    xmppManager.init();
    xmppManager.performLogin(username, password);
    xmppManager.setStatus(true, "Hello everyone");

    boolean isRunning = true;
    while (isRunning) 
        Thread.sleep(50);
    
    xmppManager.destroy();
    

XmppManager.java:

package jabberapplication;

import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.ChatManager;
import org.jivesoftware.smack.ChatManagerListener;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.packet.Presence.Type;

public class XmppManager 

private final String server;
private final int port;

private XMPPConnection connection;

private ChatManager chatManager;
private MessageListener messageListener;

private ConversationController conversationController;

public XmppManager(String server, int port) 
    this.server = server;
    this.port = port;


public void init() throws XMPPException 

    System.out.println(String.format("Initializing connection to server %1$s port %2$d", server, port));


    ConnectionConfiguration connectionConfiguration = new ConnectionConfiguration(server, port);
    connection = new XMPPConnection(connectionConfiguration);

    connection.connect();
    System.out.println("Connected: " + connection.isConnected());

    chatManager = connection.getChatManager();
    chatManager.addChatListener(new MyChatManagerListener());
    messageListener = new MyMessageListener();

    conversationController = new ConversationController();


public void performLogin(String username, String password) throws XMPPException 
    if (connection != null && connection.isConnected()) 
        connection.login(username, password);
    


public void setStatus(boolean available, String status) 
    Presence.Type type = available ? Type.available : Type.unavailable;
    Presence presence = new Presence(type);
    presence.setStatus(status);
    connection.sendPacket(presence);


public void destroy() 
    if (connection != null && connection.isConnected()) 
        connection.disconnect();
    


public void sendMessage(String message, String buddyJID) throws XMPPException 
    System.out.println(String.format("Sending mesage '%1$s' to user %2$s", message, buddyJID));
    Chat chat = chatManager.createChat(buddyJID, messageListener);
    chat.sendMessage(message);


class MyMessageListener implements MessageListener 
    @Override
    public void processMessage(Chat chat, Message message) 
        String from = message.getFrom();
        String body = message.getBody();
        if (!body.equals("null")) 
            System.out.println(String.format("Received message '%1$s' from %2$s", body, from));
            try 
                chat.sendMessage(conversationController.getAnswer(body));
             catch (XMPPException ex) 
                 System.out.println(ex.getMessage());
            
        
    


class MyChatManagerListener implements ChatManagerListener 
    @Override
    public void chatCreated(Chat chat, boolean bln) 
        int indexAt = chat.getParticipant().indexOf("@");
        String username = chat.getParticipant().substring(0, indexAt);
        chat.addMessageListener(messageListener);
        try 
            chat.sendMessage("Hello " + username + " !");
         catch (XMPPException ex) 
            System.out.println(ex.getMessage());
        
    


好吧,问题是:如何使用 smack 库版本 4.2.3 编写类似的应用程序。看起来在 4.2.3 中没有 MessageListener 和 ChatManagerListener 类。有什么建议吗?

最好的问候。

【问题讨论】:

【参考方案1】:

对于 Smack 4.2.3,ChatManagerListener 接口位于 org.jivesoftware.smack.chat 类下(注意:聊天不是聊天)。 因此,您需要将导入更改为 org.jivesoftware.smack.chat.ChatManagerListener。 此外,MessageListener 现在也在 org.jivesoftware.smack.chat 类下更改为 ChatMessageListener。 因此,您需要将导入更改为 org.jivesoftware.smack.chat.ChatMessageListener。 并将工具重命名为如下: MyMessageListener 类实现 ChatMessageListener

【讨论】:

以上是关于如何编写连接到服务器并回答用户的简单 smack 4.2.3 应用程序的主要内容,如果未能解决你的问题,请参考以下文章

使用 smack 连接到 Google Talk

Smack 连接到服务器失败

如何使用 smack-android 连接到禁用安全功能的 ejabberd?

smack 正在连接到 ejabberd 服务器

简单的 smack android 客户端不起作用?

无法通过 smack 连接到 XMPP 服务器:服务器没有响应