3

我正在尝试使用最新的 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 工作:)

谢谢!

4

3 回答 3

3

已经回答了这个问题:

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

manager = createObject("java", "org.jivesoftware.smackx.pubsub.PubSubManager").init(XMPPConnection,"pubsub.127.0.0.1");
于 2010-05-11T19:58:40.560 回答
0

或许这个例子可以作为你的参考:</p>

 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();
         }

     }
于 2018-06-28T02:27:20.727 回答
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);
    }
}
于 2018-06-28T16:41:28.313 回答