2014-11-22 120 views
0

我使用庫smack創建了一個java項目。另外我在我的電腦上用服務器xmpp openfire創建。 我的服務器的地址爲:192.168.1.4 端口爲:5222smack xmpp庫Java

我的問題是在發送/消息的接收。那是當我嘗試發送消息給另一個用戶時,用戶沒有收到它。現在把我的java程序:

ConnectionConfiguration config; 
    XMPPConnection con; 

    config = new ConnectionConfiguration("192.168.1.4", 5222); 
    con = new XMPPTCPConnection(config); 

    con.connect(); 

    /*This code is inside the button event log-in*/ 
    con.login(txtUsername.getText(), txtPassword.getText()); 


    /*this code is internal event of sending message to another user*/ 
    ChatManager chatmanager = ChatManager.getInstanceFor(con); 
    final Chat newChat = chatmanager.createChat(txtDestUsers.getText(), new MessageListener() { 

         @Override 
          public void processMessage(Chat chat, Message message) { 
           txtRicevuti.setText(message.getBody()); 
          } 
         }); 

         try { 
          newChat.sendMessage(txtMessage.getText()); 
         } 
         catch (XMPPException | NotConnectedException e) { 
          System.out.println("Error Delivering block"); 
         } 

txtDestUsers = contains the name of the recipient type [email protected] 
txtMessage = message to be sent 
txtUsername = myUsername ([email protected]) 
txtPassword = myPassword 
txtRicevuti = received messages 

作爲測試我跑了兩次節目,我登錄時使用的兩個帳戶:[email protected][email protected]

,如果我嘗試給我自己發消息,消息到達。 就好像txtDestUsers插入另一個用戶的名字一樣,消息沒有到達。

是怎麼回事?

回答

-1
Message message=new Message(); 
message.setTo(<JabberId>); 
message.setType(Message.Type.chat); 
message.setBody("Hello World!"); 

con.sendPacket(message); 

這是你如何將消息發送

PacketFilter filter = new MessageTypeFilter(Message.Type.chat); 
con.addPacketListener(new PacketListener() { 
public void processPacket(Packet packet) { 
Message message = (Message) packet; 
String body = message.getBody(); 
String from = message.getFrom(); 
} 
}, filter); 

上面的代碼是如何接收的消息。

如需進一步參考,請參閱此: http://developer.samsung.com/technical-doc/view.do?v=T000000119