专业IM即时通讯软件开发,值得信赖!

Tigase客户端Jaxmpp使用之发布订阅

Tigase 云聊IM 38℃

发布订阅

1、修改init.properties配置文件,启用发布订阅组建

--virt-hosts = domain.com
--comp-name-2 = pubsub
--comp-class-2 =tigase.pubsub.PubSubComponent

2、创建一个Node节点

实际上,每个订阅者都是关联到该节点上,发布者将消息发布到创建的该节点上,进而间接的发布给所有的订阅者。

pubSubDomain= pubsub@domain.com;
PubSubModulemodule = jaxmpp.getModulesManager().getModule(PubSubModule.class);
module.createNode(BareJID.bareJIDInstance(pubSubDomain),nodeName,
new PubSubAsyncCallback() {

        public void onTimeout() throws JaxmppException {

        }

        public void onSuccess(Stanza responseStanza) throws JaxmppException {

        }

        protected void onEror(IQ response, ErrorCondition errorCondition,
        PubSubErrorConditionpubSubErrorCondition) throws JaxmppException {

       }

});

3、客户端订阅

//组建name+ @ + domain.com
pubSubJID= BareJID.bareJIDInstance(“pubsub@domain.com”)
nodeName= “nodeName” //创建的节点名
subscriberJID= “test1@domain.com” //订阅者
PubSubModulemodule = jaxmpp.getModulesManager().getModule(PubSubModule.class);
module.subscribe(pubSubJID,nodeName, subscriberJID, new SubscriptionAsyncCallback() {

        public void onTimeout() throwsJaxmppException {

        }

        protected void onEror(IQ response, ErrorCondition errorCondition,
PubSubErrorConditionpubSubErrorCondition) throws JaxmppException {

        }

        protected void onSubscribe(IQ response, String node, JID jid, String subID,
Subscriptionsubscription) {

        }

});

4、发布(必须要有发布权限的帐号,才能发布)

//参考3订阅的pubSubJID
bareJID = BareJID.bareJIDInstance(“pubsub@domain.com”)

//发布到那个Node节点
nodeName = “”; 

//item节点的ID
itemId = “itemId_001”; 

Element entry = new DefaultElement("entry");
Element title = new DefaultElement("title");
title.setValue("发布标题");
Element summary = newDefaultElement("summary");

summary.setValue("发布的消息.....");
entry.addChild(title);
entry.addChild(summary);
jaxmpp.getModulesManager().getModule(PubSubModule.class)
.publishItem(bareJID,nodeName, itemId, entry, new PubSubAsyncCallback() {

        public void onTimeout() throwsJaxmppException {

        }

        public void onSuccess(Stanza responseStanza) throwsJaxmppException {

        }

        protected void onEror(IQ response, ErrorCondition errorCondition,
PubSubErrorCondition pubSubErrorCondition) throws JaxmppException {

        }

});

jaxmpp.disconnect();

5、解除订阅

Jaxmpp().getModulesManager().getModule(PubSubModule.class).unsubscribe

另一种发布订阅方法

1:init.properties添加如下代码,通过二级域名使用发布订阅组件。各参数含义,参考官网配置

http://www.tigase.org/content/tigase-xmpp-server-configuration-properties

–external= pubsub.domain.com:pubsub-pass:connect:5270:localhost:accept

2其余代码同上,不过pubSubDomain需要修改为pubsub.domain.com

发布订阅协议及其他功能,参考官方的扩展协议

http://xmpp.org/extensions/xep-0060.html

发布订阅协议的英文文档

http://wiki.jabbercn.org/XEP-0060

发布订阅协议翻译后的中文文档

喜欢 (0)
仿微信聊天软件开发