item-not-found(404)尝试使用Smackx pubsub获取节点时

时间:2010-05-11 06:42:00

标签: java coldfusion xmpp openfire smack

我正在尝试使用最新的Smackx主干来获取然后订阅pubsub节点。但是,openfire只是向我发回一个错误:找不到项目(404)。

我正在从ColdFusion实例化java对象,所以我的代码片段可能看起来很有趣但也许有人能够告诉我我忘记了什么。

以下是我创建节点的方法:

    ftype = createObject("java", "org.jivesoftware.smackx.pubsub.FormType");
    cform = createObject("java", "org.jivesoftware.smackx.pubsub.ConfigureForm").init(ftype.submit);
    cform.setPersistentItems(true);
    cform.setDeliverPayloads(true);
    caccess = createObject("java", "org.jivesoftware.smackx.pubsub.AccessModel");
    cform.setAccessModel(caccess.open);
    cpublish = createObject("java", "org.jivesoftware.smackx.pubsub.PublishModel");
    cform.setPublishModel(cpublish.open);
    cform.setMaxItems(99);

    manager = createObject("java", "org.jivesoftware.smackx.pubsub.PubSubManager").init(XMPPConnection);

    myNode = manager.createNode("subber", cform);

以下是我试图解决的问题(在代码的不同部分):

    manager = createObject("java", "org.jivesoftware.smackx.pubsub.PubSubManager").init(XMPPConnection);
    myNode = manager.getNode("subber");

创建节点后,我似乎可以像这样发布它:

    payload = createObject("java", "org.jivesoftware.smackx.pubsub.SimplePayload").init("book","pubsub:test:book","<book xmlns='pubsub:test:book'><title>Lord of the Rings</title></book>");
    item = createObject("java", "org.jivesoftware.smackx.pubsub.Item").init(payload);
    myNode.publish(item);

然而,正是getNode()调用导致我的代码出错。

我已经通过检查我的openfire服务器使用的数据库来验证是否正在创建节点。我可以在那里看到它们,正确地归结为叶节点等。

有什么建议吗? 还有其他人在做XMPP和ColdFusion吗? 我已经取得了巨大成功,发送和接收消息与CF和Smack只是还没有pubsub工作:)

谢谢!

3 个答案:

答案 0 :(得分:3)

已经回答:

PubSubManager类上有第二种方法接受两个参数,一个连接和一个参数。显然Openfire需要这个参数,经过一些实验,我发现它使用pubsub.your.xmpp.address

manager = createObject("java", "org.jivesoftware.smackx.pubsub.PubSubManager").init(XMPPConnection,"pubsub.127.0.0.1");

答案 1 :(得分:0)

也许这个例子可以为您提供参考:

 public void login(String Ip,String username,String passwaord)
     {
         try 
         {
            connConfig = new AndroidConnectionConfiguration(Ip, 5222);
            connection = new XMPPConnection(connConfig);
            connection.connect();  
            connection.login(username, passwaord);
            pubSubAddress = "pubsub."+ connection.getServiceName();
            manager = new PubSubManager(connection,pubSubAddress);
            Log.i("MyError","connection success");  
         }
         catch (XMPPException e) 
         {
             Log.i("MyError","connection failed");      
             e.printStackTrace();
         }

     }

答案 2 :(得分:0)

归一化的示例如下所示:

发布节点:

import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smackx.pubsub.LeafNode;
import org.jivesoftware.smackx.pubsub.PayloadItem;
import org.jivesoftware.smackx.pubsub.PubSubManager;
import org.jivesoftware.smackx.pubsub.SimplePayload;

public class XmppPubsub_Publisher {
    private static XMPPConnection connection = new XMPPConnection("think");
    private static String USRE_NAME = "test1";
    private static String PASSWORD = "1";
    static{
        try {
            connection.connect();
            connection.login(USRE_NAME,PASSWORD);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args)throws Exception{
        try{
            PubSubManager manager = new PubSubManager(connection);
            String nodeId = "test2";
            LeafNode myNode = null;
            try {
                myNode = manager.getNode(nodeId);
            } catch (Exception e) {
                e.printStackTrace();
            }
            if(myNode == null){
                myNode = manager.createNode(nodeId);
            }
            String msg = "fsadfasdfsadfasdfd---";
            SimplePayload payload = new SimplePayload("message","pubsub:test:message", "<message xmlns='pubsub:test:message'><body>"+msg+"</body></message>");
            PayloadItem<SimplePayload> item = new PayloadItem<SimplePayload>("5", payload);
            myNode.publish(item);
            System.out.println("-----publish-----------");
        }
        catch(Exception E)
        {E.printStackTrace();}
    }
}

检索节点:

import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smackx.pubsub.ItemPublishEvent;
import org.jivesoftware.smackx.pubsub.Node;
import org.jivesoftware.smackx.pubsub.PayloadItem;
import org.jivesoftware.smackx.pubsub.PubSubManager;
import org.jivesoftware.smackx.pubsub.listener.ItemEventListener;

public class XmppPubsub_Reciever {
    private static XMPPConnection connection = new XMPPConnection("think");
    private static String USRE_NAME = "test1";
    private static String PASSWORD = "1";
    static {
        try {
            connection.connect();
            connection.login(USRE_NAME, PASSWORD);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) throws Exception {
        String nodeId = "test2";
        PubSubManager manager = new PubSubManager(connection);
        Node eventNode = manager.getNode(nodeId);
        eventNode.addItemEventListener(new ItemEventListener<PayloadItem>() {
            public void handlePublishedItems(ItemPublishEvent evt) {
                for (Object obj : evt.getItems()) {
                    PayloadItem item = (PayloadItem) obj;
                    System.out.println("--:Payload=" + item.getPayload().toString());
                }
            }
        });
        eventNode.subscribe(connection.getUser());
        while(true);
    }
}