在最近的安卓项目中需要用到有关于IM的相关东西,采用了基于xmpp协议的openfire+smack的框架。下面记录一些配置openfire和smack的过程中遇到的一些问题。
openfire
openfire是在windows环境下配置,数据库使用的是mysql数据库,在mysql中需要新建一个为openfire使用的数据库,然后导入openfire的脚本,生成一些表格(注意在最新的mysql8.0中脚本运行会出错,所以选用的以前的mysql版本)。
还有一点千万要注意:防火墙!客户端的连接是通过5222端口连接,需要能使连接通过防火墙。
smack
在4.2.0之后有一些跟之前不一样的变化,具体配置可以通过google "smack4.2.0" 来解决
这是我的连接函数:
private AbstractXMPPConnection getConnection() throws XmppStringprepException, UnknownHostException {
XMPPTCPConnectionConfiguration config = XMPPTCPConnectionConfiguration.builder()
.setXmppDomain(getString(R.string.xmpp_domain))
.setHostAddress(InetAddress.getByName(getString(R.string.xmpp_ip)))
.setPort(Integer.parseInt(getString(R.string.xmpp_port)))
.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled)
.setConnectTimeout(3000)
.setDebuggerEnabled(true)
.build();
AbstractXMPPConnection connection = new XMPPTCPConnection(config);
return connection;
}