smack连接ejabberd笔记二
来源:互联网 发布:佳美迅授课软件 编辑:程序博客网 时间:2024/04/29 01:32
1、和ejabberd服务器建立连接
ConnectionConfiguration config = new ConnectionConfiguration(ConfigUtils.domin,ConfigUtils.port); config.setSecurityMode(SecurityMode.disabled); XMPPConnection.DEBUG_ENABLED = true; XMPPConnection connection = new XMPPConnection(config); connection.connect();
domin:ejabberd服务器域名或IP
port:ejabberd默认端口5222
2、注册账号到ejabberd
smack有两种方式可以注册:
a)
public int registerUserToEjabberd(String userName, String password) { // TODO Auto-generated method stub logger.info("--------registerUserToEjabberd----------" + userName); // ThreadPoolTools.threads.execute(new // RegisterToEjabberdThread(userName, password)); XMPPConnection connection = ConnectionFactory.getConnection(); try { Map<String, String> attributes = new HashMap<String, String>(); attributes.put("username", userName); attributes.put("password", password); attributes.put("name", userName + "@" + ConfigUtils.domin); Registration r = new Registration(); r.setType(IQ.Type.SET); r.setAttributes(attributes); connection.sendPacket(r); //过滤器,用来过滤由服务器返回的信息(即得到注册信息的内容) PacketFilter packetFilter = new AndFilter(new PacketIDFilter( r.getPacketID()), new PacketTypeFilter(IQ.class)); PacketCollector collector = connection .createPacketCollector(packetFilter); IQ result = (IQ) collector.nextResult(); if (result == null) { logger.info("服务器没有返回任何信息"); return ConfigUtils.ERROR; } else { String resultString = result.getType().toString(); if (resultString.equalsIgnoreCase("result")) logger.info(userName + "注册成功"); else if (resultString.equalsIgnoreCase("error")) { if (result.getError().toString() .equalsIgnoreCase("conflict(409)")) { logger.info(userName + "用户名称已存在"); return ConfigUtils.HAVEN; } else { logger.info(userName + "注册失败"); return ConfigUtils.ERROR; } } } } catch (Exception e) { e.printStackTrace(); logger.info(e.getMessage()); return ConfigUtils.ERROR; } return ConfigUtils.OK;}
b)
public boolean regist(String username, String password, XMPPConnection connection) { if (connection == null) return false; try { connection.getAccountManager().createAccount(username, password); } catch (XMPPException xe) { xe.printStackTrace(); return false; } return true; }
2、smack登录ejabberd服务器
public boolean login(String username, String password, XMPPConnection connection) { if (connection == null) return false; try { connection.login(username, password); Presence presence = new Presence(Presence.Type.available); connection.sendPacket(presence); connectionListener = new TaxiConnectionListener(username,password); connection.addConnectionListener(connectionListener); } catch (XMPPException xe) { xe.printStackTrace(); return false; } return true; }
0 0
- smack连接ejabberd笔记二
- SMACK连接ejabberd笔记一
- smack连接ejabberd服务笔记三
- Android smack+ejabberd+spack 实现即时通讯(二)客户端连接
- smack+ejabberd 获取离线消息
- ejabberd笔记
- ejabberd服务端开发笔记
- Smack文档二:Smack入门
- Android smack+ejabberd+spack 实现即时通讯(一)服务器搭建
- Android Studio使用smack进行ejabberd创建用户
- ejabberd分析(二) 用户注册
- ejabberd分析(二) 用户注册
- Smack:管理连接
- Smack 4.1 连接openfire
- Smack文档三:Smack: 连接管理
- ejabberd
- ejabberd
- 学习 Smack(二)
- 面向对象
- 扫描二维码自动识别手机APP下载地址
- hdu 1284
- hdu 5044 Tree(树链剖分)
- UVa 654 - Ratio (枚举)
- smack连接ejabberd笔记二
- 《信息检索》第9讲:文件管理新境界 及 大学两种生活态度
- oracle TNS:协议适配器错误
- leetcode记录-Two Sum
- Codeforces Round #273 (Div. 2) --C Table Decorations
- 【读书心得】 高质量C++/C编程指南-林锐
- zoj 3820(2014牡丹江现场赛B题)
- STM32的IO口的8种配置
- zoj How Many Shortest Path