如何使用 smack(java) 创建、发送和接收 iq 数据包 [英] How Can I create,send and receive iq packets using smack(java)

查看:34
本文介绍了如何使用 smack(java) 创建、发送和接收 iq 数据包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已连接到服务器(Xmpp)但无法在我的 psi 客户端发送和接收数据包

I am connected to server(Xmpp) but unable to send and receive packets at my psi client

这是我的代码片段

POSClientIQ posclientiq = new POSClientIQ();
    posclientiq.connectXMPPServer();
    posclientiq.processMessage();
   }

   public void processMessage()
   {  try{

      final  IQ iq1 = new IQ() {
     public String getChildElementXML() {
      return "<iq type='get' from ='sam'><query xmlns='jabber:iq:roster'></query></iq>";
    }
  };

  iq1.setType(IQ.Type.GET);
 // PacketCollector collector = connection.createPacketCollector(new PacketIDFilter(iq1.getPacketID()));
  connection.sendPacket(iq1);

  System.out.println("Message send");

推荐答案

如果你有一个自定义查询并且你想使用你的 IQ 实现,那么:

If you have a custom query and you would like to use your IQ implementation then:

final IQ iq = new IQ() {
public String getChildElementXML() { 
return "<query xmlns='http://jabber.org/protocol/disco#info'/>"; // here is your query
//this returns "<iq type='get' from='User@YourServer/Resource' id='info1'> <query xmlns='http://jabber.org/protocol/disco#info'/></iq>";
 }};
// set the type
iq.setType(IQ.Type.GET);
// send the request
connection.sendPacket(iq); 

如您所见,这里有您的自定义查询,您可以使用 Smack 设置其余的 IQ,例如设置类型.请注意,Smack 会根据您登录的 JID 为您填写发件人".

As you can see you have here your custom query and you use Smack to set the rest of your IQ e.g. setting the type. Please note that Smack fills the "from" for you based on the JID your are logged into.

这篇关于如何使用 smack(java) 创建、发送和接收 iq 数据包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆