xmpp协议相关资料

xmpp协议详解一:xmpp基本概念

XMPP协议详解二:XMPP出席

XMPP协议详解三:即时消息

XMPP协议分析—具体篇

Tigase Development Guide

openfire+smack添加好友申请 Smack 4.1.8 and 4.2.0-beta2 添加好友请求以及确认

Smack4.20实现连接登陆(1)

Smack4.20实现单聊通讯与监听(2)

Smack4.20实现接收离线消息与花名册操作(3)

Smack4.20实现群聊(4)

tigase 的基本使用 (smack)

Smack4.1.1 聊天推送

Smack开发总结 (三)好友管理专题基本操作

即时通讯smack4.1.0-添加好友遇到的bug

openfire+smack添加好友申请 Smack 4.1.8 and 4.2.0-beta2 添加好友请求以及确认

android下的XMPP对应smack-4.2.1,实现登录,注册,发单聊,加聊天室,发群聊等简单功能

推荐
tigase开发专栏
推荐
tigase开发比笔记

Tigase插件编写——注册用户批量查询

注册用户批量查询

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

import tigase.db.DBInitException;
import tigase.db.NonAuthUserRepository;
import tigase.db.RepositoryFactory;
import tigase.db.UserRepository;
import tigase.server.Iq;
import tigase.server.Packet;
import tigase.util.TigaseStringprepException;
import tigase.xml.Element;
import tigase.xmpp.Authorization;
import tigase.xmpp.BareJID;
import tigase.xmpp.NotAuthorizedException;
import tigase.xmpp.PacketErrorTypeException;
import tigase.xmpp.StanzaType;
import tigase.xmpp.XMPPException;
import tigase.xmpp.XMPPPreprocessorIfc;
import tigase.xmpp.XMPPProcessor;
import tigase.xmpp.XMPPProcessorIfc;
import tigase.xmpp.XMPPResourceConnection;
import tigase.xmpp.impl.roster.RosterAbstract;

public class TalkKingRoster extends XMPPProcessor implements XMPPProcessorIfc, XMPPPreprocessorIfc{
	private static Logger log = Logger.getLogger(TalkKingRoster.class.getName());
	public static final String ID = "talkKing:" + RosterAbstract.XMLNS;
	public static final String XMLNS = "talkKing:roster";
	private static final String[] XMLNSS = {XMLNS};
	private static final String[][] ELEMENTS = {Iq.IQ_QUERY_PATH};
	 private static final Element[] FEATURES = { new Element("roster", new String[] { "xmlns" }, new String[] { XMLNS }) };

	@Override
	public String id() {
		// TODO Auto-generated method stub
		return ID;
	}

	@Override
	public boolean preProcess(Packet packet, XMPPResourceConnection session,
			NonAuthUserRepository repo, Queue<Packet> results,
			Map<String, Object> settings) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void process(Packet packet, XMPPResourceConnection session,
			NonAuthUserRepository repo, Queue<Packet> results,
			Map<String, Object> settings) throws XMPPException {
		// TODO Auto-generated method stub
		if (log.isLoggable(Level.FINEST)) {
            log.finest("Processing packet: " + packet.toString());
        }
		if(session==null){
			if(log.isLoggable(Level.FINE)){
				log.log(Level.FINE,"Session is null, ignoring packet: {0}",packet);
				return;
			}
		}
		if (packet.getStanzaFrom()!= null && session.isUserId(packet.getStanzaFrom().getBareJID())&& !session.isAuthorized()) {
			if ( log.isLoggable( Level.FINE ) ){
                log.log( Level.FINE, "Session is not authorized, ignoring packet: {0}", packet );
            }
            return;
		}
		
		try {
			if (!session.isServerSession() && (packet.getStanzaFrom() != null ) && !session.isUserId(packet.getStanzaFrom().getBareJID())) {
                // RFC says: ignore such request
                log.log( Level.WARNING, "Roster request ''from'' attribute doesn't match "
                    + "session: {0}, request: {1}", new Object[] { session, packet } );
                return;
            }
			
			StanzaType type = packet.getType();
            String xmlns = packet.getElement().getXMLNSStaticStr( Iq.IQ_QUERY_PATH );
            
            if (xmlns == XMLNS && type == StanzaType.get){
            	List<Element> items = packet.getElemChildrenStaticStr(Iq.IQ_QUERY_PATH);
    			
    			if (items!=null) {
    				String uri = System.getProperty( "user-db-uri" );
    				UserRepository userRepository = RepositoryFactory.getUserRepository( null, uri, null );
    				String serverDomain = session.getDomainAsJID().getDomain();
    				Set<BareJID> found = new HashSet<BareJID>();
    				for (Element item : items) {
    					if (!item.getName().equals("item")) {
    						continue;
    					}
    					
    					BareJID jid = BareJID.bareJIDInstance(item.getAttributeStaticStr("jid"));
    					String domain = jid.getDomain();
    					BareJID localJid = BareJID.bareJIDInstance(jid.getLocalpart(),serverDomain);
    					
    					
    					boolean isLocalJid = domain.equals(serverDomain);
    					if (isLocalJid) {
    						if (userRepository.userExists(localJid)) {
    							found.add(jid);
    						}
    					}
    				}
    				
    				Element query = new Element(Iq.QUERY_NAME);
    				query.setXMLNS(XMLNS);
    				
    				for (BareJID bareJID : found) {
    					Element item = new Element("item");
    					item.setAttribute("jid", bareJID.toString());
    					query.addChild(item);
    				}
    				
    				results.offer(packet.okResult(query, 0));
    				
    				packet.processedBy(ID);
    			}
            }
		} catch (NotAuthorizedException e){
			 log.log( Level.WARNING, "Received roster request but user session is not authorized yet: {0}", packet );
	            try {
	                results.offer( Authorization.NOT_AUTHORIZED.getResponseMessage( packet,
	                    "You must authorize session first.", true ) );
	            }
	            catch (PacketErrorTypeException pe) {
	                // ignored
	            }
	            
		} catch (TigaseStringprepException e) {
			// TODO Auto-generated catch block
			
		} catch (DBInitException e) {
			// TODO Auto-generated catch block
			log.log( Level.WARNING, "Database problem, please contact admin:", e );
            try {
                results.offer( Authorization.INTERNAL_SERVER_ERROR.getResponseMessage( packet,
                    "Database access problem, please contact administrator.", true ) );
            }
            catch (PacketErrorTypeException pe) {
                // ignored
            }
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	}
	
	@Override
	public Set<StanzaType> supTypes() {
		return new HashSet<StanzaType>(Arrays.asList(StanzaType.get));
	}

	@Override
	public String[][] supElementNamePaths() {
		// TODO Auto-generated method stub
		return ELEMENTS;
	}

	@Override
	public String[] supNamespaces() {
		// TODO Auto-generated method stub
		return XMLNSS;
	}

	@Override
	public Element[] supStreamFeatures(XMPPResourceConnection session) {
		if (log.isLoggable(Level.FINEST) && (session != null)) {
            log.finest("VHostItem: " + session.getDomain());
        }
        if (session != null) {
            return FEATURES;
        }
        else {
            return null;
        }
	}
	
	
}

一般问题解决方案

1、网上的账号搜索代码异常:

 /**
     * 查询用户
     *
     * @param userName userName
     * @return List<HashMap<String, String>>
     */
    public List<HashMap<String, String>> searchUsers(String userName) {
        if (getConnection() == null)
            return null;
        HashMap<String, String> user;
        List<HashMap<String, String>> results = new ArrayList<>();
        try {
            UserSearchManager usm = new UserSearchManager(getConnection());

//            Form searchForm = usm.getSearchForm(getConnection().getServiceName());

//            Form searchForm = usm.getSearchForm(getConnection().getXMPPServiceDomain());
            DomainBareJid searchDomainBareJid=JidCreate.domainBareFrom("search." + getConnection().getXMPPServiceDomain());


            Form searchForm = usm.getSearchForm(searchDomainBareJid);
            if (searchForm == null)
                return null;

            Form answerForm = searchForm.createAnswerForm();
            answerForm.setAnswer("userAccount", true);
            answerForm.setAnswer("userPhote", userName);
//            DomainBareJid domainBareJid=JidCreate.domainBareFrom("search" + getConnection().getServiceName());

//            ReportedData data = usm.getSearchResults(answerForm, getConnection().getXMPPServiceDomain());
            ReportedData data = usm.getSearchResults(answerForm, searchDomainBareJid);

            List<ReportedData.Row> rowList = data.getRows();
            for (ReportedData.Row row : rowList) {
                user = new HashMap<>();
                user.put("userAccount", row.getValues("userAccount").toString());
                user.put("userPhote", row.getValues("userPhote").toString());
                results.add(user);
                // 若存在,则有返回,UserName一定非空,其他两个若是有设,一定非空
            }
        } catch
        (SmackException | InterruptedException
                        | XmppStringprepException | XMPPException e) {
            e.printStackTrace();
        }
        return results;
    }

注意,网上中

getConnection().getServiceName()

是不存在的。

报错:
在这里插入图片描述

stackoverflow的说法:


XMPP UserSearch using latest smack 4.1.3 got remote server no found error

在这里插入图片描述

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值