项目中遇到要用xmpp做通讯,原来在的应用跑在linux上用的gloox,现在需要在android上来实现,查了下发现smack最近做了一次大升级,4.0.0发布了,最开始用了smack的库,结果在android上报错,去https://community.igniterealtime.org/ 社区问了下,原来还有个asmack,下了库文件然后写代码:
创新互联公司于2013年成立,是专业互联网技术服务公司,拥有项目成都网站建设、网站设计网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元太白做网站,已为上家服务,为太白各地企业和个人服务,联系电话:13518219792
根据asmack-android-8-4.0.0-rc2.jar 的readme,另外需要DNSjava的库,我用的是dnsjava-2.1.6
最开始发现logcat有一个关于dns的报错,网上查了下发现有说要加下面一句在初始化的时候:
//System.setProperty("sun.net.spi.nameservice.provider.1","dns,dnsjava");
但是后来我发现没有这句,好像也没问题,暂时屏蔽掉了。
asmack的初始化是必须的。
mSmackAndroid = SmackAndroid.init(context);
conf = new ConnectionConfiguration( "wooxonline.com",5222);
SASLAuthentication.supportSASLMechanism("PLAIN",0);
conf.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
conf.setCompressionEnabled(false);
// disable the built-in ReconnectionManager since we handle this
conf.setReconnectionAllowed(false);
conf.setSendPresence(false);
conf.setDebuggerEnabled(true);
XMPPConnection connection = XMPPTCPConnection(conf);
connection.login(g_email, g_password);
最开始少了sasl方式的设置,发现连接没问题,但是login的时候总是返回noresponse error,查了发现asmask对于连接出错抛的异常比较简单,不会细分。
log如下:
connect:
06-17 09:39:09.300 19344-19457/com.wooxclound.homedaemonactivity.app D/SMACK﹕ SENT (0):
06-17 09:39:12.500 19344-19458/com.wooxclound.homedaemonactivity.app D/SMACK﹕ RCV (0):
login:
06-17 09:39:21.500 19344-19457/com.wooxclound.homedaemonactivity.app D/SMACK﹕ SENT (0):
06-17 09:39:21.500 19344-19344/com.wooxclound.homedaemonactivity.app I/Woox HomeDaemon﹕ onStartCommand(): Intent com.wooxclound.homedaemon.action.XMPP.CONNECTION_CHANGED
06-17 09:39:22.090 19344-19458/com.wooxclound.homedaemonactivity.app D/SMACK﹕ RCV (0):
06-17 09:39:22.100 19344-19458/com.wooxclound.homedaemonactivity.app D/SMACK﹕ SENT (0):
06-17 09:39:22.490 19344-19458/com.wooxclound.homedaemonactivity.app D/SMACK﹕ RCV (0):
06-17 09:39:22.540 19344-19346/com.wooxclound.homedaemonactivity.app D/dalvikvm﹕ GC_CONCURRENT freed 311K, 8% free 5028K/5408K, paused 4ms+3ms, total 26ms
06-17 09:39:22.560 19344-19457/com.wooxclound.homedaemonactivity.app D/SMACK﹕ SENT (0):
06-17 09:39:23.130 19344-19458/com.wooxclound.homedaemonactivity.app D/SMACK﹕ RCV (0):
06-17 09:39:23.130 19344-19457/com.wooxclound.homedaemonactivity.app D/SMACK﹕ SENT (0):
06-17 09:39:23.720 19344-19458/com.wooxclound.homedaemonactivity.app D/SMACK﹕ RCV (0):
06-17 09:39:23.730 19344-19454/com.wooxclound.homedaemonactivity.app D/SMACK﹕ User logged (0): test11@wooxonline.com@wooxonline.com:0/Smack
在注册新用户的时候,可以使用accountmanager也可以使用IQ package,方法如下:
public static final int REGISTER_METHOD_ACCOUNT_MANAGER = 0;
public static final int REGISTER_METHOD_IQ_PACKAGE = 1;
public static final int REGISTER_RESULT_SERVER_NO_RESULT = 0;
public static final int REGISTER_RESULT_SUCCESS = 1;
public static final int REGISTER_RESULT_ACCOUNT_EXIST = 2;
public static final int REGISTER_RESULT_FAIL = 4;
public static final int REGISTER_RESULT_NO_CONNECTION = 4;
/**
* register
*
* @param username
* @param password
* @return 1:success 0:server not return 2:account exist 3:fail 4:no connection
*/
public int register(String username, String password) {
if (!((mConnection != null) && mConnection.isConnected())) {
return REGISTER_RESULT_NO_CONNECTION;
} else {
reg_method = REGISTER_METHOD_ACCOUNT_MANAGER;
Map
attributes.put("username", username);
attributes.put("password", password);
//attributes.put("email", "");
//attributes.put("name", "");
if (reg_method == REGISTER_METHOD_ACCOUNT_MANAGER) {
try {
if (mAccountManager == null) {
mAccountManager = AccountManager.getInstance(mConnection);
}
mAccountManager.createAccount(username, password, attributes);
} catch (Exception ex) {
L.e(TAG, ex);
if (ex.toString().equalsIgnoreCase("conflict")) {
L.i(XmppManager.class, " account exist!");
return REGISTER_RESULT_ACCOUNT_EXIST;
} else {
L.i(XmppManager.class, " register fail");
return REGISTER_RESULT_FAIL;
}
}
L.i(XmppManager.class, " register success!");
return REGISTER_RESULT_SUCCESS;
} else if (reg_method == REGISTER_METHOD_IQ_PACKAGE) {
try {
Registration reg = new Registration();
reg.setType(IQ.Type.SET);
reg.setTo(mConnection.getServiceName());
reg.setAttributes(attributes);
PacketFilter filter = new AndFilter(new PacketIDFilter(reg.getPacketID()),
new PacketTypeFilter(IQ.class));
PacketCollector collector = mConnection.createPacketCollector(filter);
mConnection.sendPacket(reg);
IQ result = (IQ) collector.nextResult(SmackConfiguration.getDefaultPacketReplyTimeout());
collector.cancel();
//System.out.println(result);
if (result == null) {
L.i(XmppManager.class, " no value return by server!");
return REGISTER_RESULT_SERVER_NO_RESULT;
} else if (result.getType() == IQ.Type.ERROR) {
//
if (result.getError().toString().equalsIgnoreCase("conflict")) {
L.i(XmppManager.class, " account exist!");
return REGISTER_RESULT_ACCOUNT_EXIST;
} else {
L.i(XmppManager.class, " register fail");
return REGISTER_RESULT_FAIL;
}
} else if (result.getType() == IQ.Type.RESULT) {
L.i(XmppManager.class, " register success!");
//Intent intent = new Intent(DaemonService.ACTION_REGISTER);
return REGISTER_RESULT_SUCCESS;
}
L.e(TAG, ":Should not go there for register process!");
} catch (Exception ex) {
L.e(TAG, ex);
}
}
L.e(TAG, ":Should not go there for register process!");
return REGISTER_RESULT_FAIL;
}
}
}
当账户存在时,返回如下包:
D/SMACK﹕ RCV (0):
但是geterrorcode()接口似乎在4.0的asmack中被去掉了,所以只能判定error是conflict就认为是账户已经存在。
由于服务器强制使用TLS认证,所以对于连接做了新的配置,然后再openfire server上测试没有问题,但是在公司的服务器出问题了,最先我用的是asmak 4.0.0 rc2的库,在认证challenge的时候服务器似乎没有响应,log如下:
D/SMACK﹕ SENT (0):
D/SMACK﹕ RCV (0):
D/SMACK﹕ SENT (0):
D/SMACK﹕ RCV (0):
W/System.err﹕ at org.jivesoftware.smack.tcp.XMPPTCPConnection.initReaderAndWriter(XMPPTCPConnection.java:501)
W/System.err﹕ at org.jivesoftware.smack.tcp.XMPPTCPConnection.proceedTLSReceived(XMPPTCPConnection.java:648)
W/System.err﹕ at org.jivesoftware.smack.tcp.PacketReader.parsePackets(PacketReader.java:223)
W/System.err﹕ at org.jivesoftware.smack.tcp.PacketReader.access$000(PacketReader.java:48)
W/System.err﹕ at org.jivesoftware.smack.tcp.PacketReader$1.run(PacketReader.java:82)
W/System.err﹕ at org.jivesoftware.smack.tcp.XMPPTCPConnection.initReaderAndWriter(XMPPTCPConnection.java:501)
W/System.err﹕ at org.jivesoftware.smack.tcp.XMPPTCPConnection.proceedTLSReceived(XMPPTCPConnection.java:648)
W/System.err﹕ at org.jivesoftware.smack.tcp.PacketReader.parsePackets(PacketReader.java:223)
W/System.err﹕ at org.jivesoftware.smack.tcp.PacketReader.access$000(PacketReader.java:48)
W/System.err﹕ at org.jivesoftware.smack.tcp.PacketReader$1.run(PacketReader.java:82)
D/SMACK﹕ SENT (0):
D/SMACK﹕ RCV (0):
D/SMACK﹕ RCV (0):
D/SMACK﹕ SENT (0):
D/SMACK﹕ RCV (0):
D/SMACK﹕ SENT (0):
W/XMPPConnection﹕ Connection closed with error
java.lang.NullPointerException
at com.novell.sasl.client.DigestMD5SaslClient.DigestCalcHA1(DigestMD5SaslClient.java:447)
at com.novell.sasl.client.DigestMD5SaslClient.createDigestResponse(DigestMD5SaslClient.java:646)
at com.novell.sasl.client.DigestMD5SaslClient.evaluateChallenge(DigestMD5SaslClient.java:356)
at org.jivesoftware.smack.sasl.SASLMechanism.challengeReceived(SASLMechanism.java:190)
at org.jivesoftware.smack.SASLAuthentication.challengeReceived(SASLAuthentication.java:427)
at org.jivesoftware.smack.tcp.PacketReader.parsePackets(PacketReader.java:252)
at org.jivesoftware.smack.tcp.PacketReader.access$000(PacketReader.java:48)
at org.jivesoftware.smack.tcp.PacketReader$1.run(PacketReader.java:82)
翻了一下asmack社区的帖子https://community.igniterealtime.org/message/240015#240015上说rc3的库解决了这个问题,down下来一试,果然,但是认证还是通不过:
D/SMACK﹕ SENT (0):
D/SMACK﹕ RCV (0):
D/SMACK﹕ SENT (0):
D/SMACK﹕ RCV (0):
W/System.err﹕ at org.jivesoftware.smack.tcp.XMPPTCPConnection.initReaderAndWriter(XMPPTCPConnection.java:501)
W/System.err﹕ at org.jivesoftware.smack.tcp.XMPPTCPConnection.proceedTLSReceived(XMPPTCPConnection.java:648)
W/System.err﹕ at org.jivesoftware.smack.tcp.PacketReader.parsePackets(PacketReader.java:223)
W/System.err﹕ at org.jivesoftware.smack.tcp.PacketReader.access$000(PacketReader.java:48)
W/System.err﹕ at org.jivesoftware.smack.tcp.PacketReader$1.run(PacketReader.java:82)
W/System.err﹕ at org.jivesoftware.smack.tcp.XMPPTCPConnection.initReaderAndWriter(XMPPTCPConnection.java:501)
W/System.err﹕ at org.jivesoftware.smack.tcp.XMPPTCPConnection.proceedTLSReceived(XMPPTCPConnection.java:648)
W/System.err﹕ at org.jivesoftware.smack.tcp.PacketReader.parsePackets(PacketReader.java:223)
W/System.err﹕ at org.jivesoftware.smack.tcp.PacketReader.access$000(PacketReader.java:48)
W/System.err﹕ at org.jivesoftware.smack.tcp.PacketReader$1.run(PacketReader.java:82)
D/SMACK﹕ SENT (0):
D/SMACK﹕ RCV (0):
D/SMACK﹕ RCV (0):
D/SMACK﹕ SENT (0):
D/SMACK﹕ RCV (0):
D/SMACK﹕ SENT (0):
D/SMACK﹕ RCV (0):
D/SMACK﹕ SENT (1):
D/SMACK﹕ RCV (1):
D/SMACK﹕ SENT (1):
D/SMACK﹕ RCV (1):
D/SMACK﹕ SENT (1):
D/SMACK﹕ RCV (1):
D/SMACK﹕ RCV (1):
D/SMACK﹕ SENT (1):
D/SMACK﹕ RCV (1):
D/SMACK﹕ SENT (1):
D/SMACK﹕ RCV (1):
继续翻坛子,https://code.google.com/p/gtalksms/issues/detail?id=262有人发现登陆ID不使用@域名就没有问题,太晕了。
程序跑起来以后,使用xmpp客户端聊天消息接收没问题,但是通过CGI网页pass过来的信息接收不到,初步认为应该是packetfilter的配置不对引起的。
先抓包:
From HTML page:
D/SMACK﹕ RCV (0):
From XMPP client:
D/SMACK﹕ RCV (0):
但是从http://xmpp.org/extensions/xep-0201.html xmpp协议上来看似乎,所有的message都应该有type和thread段。
最后将filter的配置改了一下,就能抓到了。
//PacketFilter filter = new MessageTypeFilter(Message.Type.chat);
PacketFilter filter = new AndFilter(new PacketTypeFilter(Message.class));