Xmpp Smack Chat无法发送和接收消息

时间:2016-06-01 14:14:13

标签: xmpp chat openfire smack

我尝试使用xmpp和OpenFire在两个用户之间建立聊天连接。但我无法发送和接收消息。我已粘贴下面的代码供参考。任何帮助都会非常有帮助。

我通过

与Smack建立了联系
XMPPTCPConnectionConfiguration.Builder config = XMPPTCPConnectionConfiguration.builder();
                config.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);              
                config.setUsernameAndPassword("admin", "admin");               
                config.setServiceName("172.21.4.199");              
                config.setHost("172.21.4.199");              
                config.setPort(5222);
                config.setDebuggerEnabled(true);
                config.setConnectTimeout(50000);
XMPPTCPConnection connection = new XMPPTCPConnection(config.build());

XMPPTCPConnection.setUseStreamManagementResumptiodDefault(true);
XMPPTCPConnection.setUseStreamManagementDefault(true);

        try {
                    connection.setPacketReplyTimeout(50000);
                    connection.connect();
                    Log.d(TAG, "SetupDefaults -- Connected");
                } catch (Exception e) {
                    e.printStackTrace();
                    Log.d(TAG, "SetupDefaults -- Connection failed exc: "+e);
                }

 and its gets succesfully connected. And i try to send a chat by using 

 ChatManager  chatManager = ChatManager.getInstanceFor(connection);
        Chat chat = chatManager.createChat("user2@server.local", new ChatMessageListener() {
            @Override
            public void processMessage(Chat chat, Message message) {
                System.out.println("processMessage -- Sent message: " + message);
            }
        });

        try {
            chat.sendMessage("Hai.. Lets we chat!");
        }catch (Exception e){
            Log.d(TAG, "sendChat  Exc: "+e.toString());
        }

但是我找不到processMessage被触发。因为S.O.P没有被触发。但我得到了

SMACK:SENT(0):Hai ..让我们聊聊!

SMACK:RECV(0):Hai ..让我们聊聊吧!

发送聊天时在我的控制台中

Simillarly我用,

PacketListener packetListener = new PacketListener() {
                       @Override
                       public void processPacket(Stanza packet) throws SmackException.NotConnectedException {
                           Message message = (Message)packet;
                           String from  = message.getFrom();
                           String body = message.getBody();
                           System.out.println("Message from: " + from + " " + body);
                       }
                    };
                    connection.addPacketListener(packetListener, filter);

接收聊天。但是processPacket也没有被触发。

1 个答案:

答案 0 :(得分:0)

PacketListner它是处理节的更通用的东西,它不是你真正需要的东西。你只需要一个ChatMessageListner

ChatManager chatManager;
chatManager = ChatManager.getInstanceFor(connection);       
chatManager.addChatListener(
                **new ChatManagerListener() {
                    @Override
                    public void chatCreated(Chat chat, boolean createdLocally)
                    {
                        if (!createdLocally)
                        {

                            chat.addMessageListener(new IncomingMessageListener());;
                        }
                    }
                })**;

基本实施:

class IncomingMessageListener implements ChatMessageListener {
    @Override
    public void processMessage(Chat arg0, Message message) {
        String from = message.getFrom();
        String body = message.getBody();

        if (body != null)
        {
            System.out.println(String.format("============ Received message '%1$s' from %2$s\n============", body, from));
            guiUpdate.displayMessage(body); /* custom method */
        }
        else
        {
            System.out.println("SYSTEM: ping");
        }
    }