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

Tigase客户端Jaxmpp登录退出收发消息

Tigase 云聊IM 43℃

登录退出

可以通过添加监听器,来判断是否登录退出成功

1、对登录的参数进行设置

jaxmpp.getSessionObject().setUserProperty(JaxmppCore.AUTOADD_STANZA_ID_KEY,true);
jaxmpp.getConnectionConfiguration().setUserJID(jid.getBareJid());
jaxmpp.getConnectionConfiguration().setUserPassword(password);
jaxmpp.getProperties().setUserProperty(SocketConnector.SERVER_HOST,"localhost");

2、对登录退出进行监听

//监听登录jaxmpp.login()

jaxmpp.addListener(Jaxmpp.Connected,newListener<JaxmppEvent>() {
        public void handleEvent(JaxmppEvent be) throws JaxmppException {
                System.out.println("---"+ be.getSessionObject().getUserBareJid().getLocalpart() +"登录成功----");
        }
});

//监听退出jaxmpp.disconnect()
jaxmpp.addListener(Jaxmpp.Disconnected,newListener<JaxmppEvent>() {
        public void handleEvent(JaxmppEvent be) throws JaxmppException {
                System.out.println("---"+ be.getSessionObject().getUserBareJid().getLocalpart() + "退出-----");
        }
});

3、持续监听,不退出程序

不执行jaxmpp.disconnect();程序不会退出。实际是通过发送一个结束流的标记</stream:stream>退出程序

发送接收消息

1、简单消息

Elementelement = new DefaultElement("");
Stanzastanza = Stanza.create(element);
jaxmpp.send(stanza);
jaxmpp.sendMessage(JID.jidInstance(""),"主题","消息");

2、通过XmppModule实现类发送(有些发送类需要先进行注册)

jaxmpp.getModulesManager().getModule(MessageModule.class).sendMessage(toJID,subject,message);
jaxmpp.getModulesManager().getModule(JingleModule.class).process($element);

3、对发送接收的消息进行监听

//监听消息接收
jaxmpp.addListener(SocketConnector.StanzaReceived,
new Listener<SocketConnector.ConnectorEvent>() {
        public void handleEvent(ConnectorEvent be) throwsJaxmppException { 
               System.out.println(
                       be.getSessionObject().getUserBareJid().getLocalpart()+"RECEIVED: "+ be.getStanza().getAsString());
        }
});

//监听消息发送
jaxmpp.addListener(SocketConnector.StanzaSending,
new Listener<SocketConnector.ConnectorEvent>() {
        public void handleEvent(ConnectorEvent be) throwsJaxmppException {
                System.out.println(
                        be.getSessionObject().getUserBareJid().getLocalpart()+"SENDING: "+ be.getStanza().getAsString());
        }
});
喜欢 (0)
仿微信聊天软件开发