我正在使用Smack -Xmpp开发聊天应用程序
我想连接 e-jabbered 服务器,所以找到了不同的代码来连接服务器。
有不同的代码连接 Openfire 和 e-jabbered 服务器吗?
1)XMPPTCPConnectionConfiguration
private void initialiseConnection() {
XMPPTCPConnectionConfiguration.Builder config = XMPPTCPConnectionConfiguration
.builder();
config.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
try {
config.setXmppDomain(Constants.XMPP_DOMAIN);
config.setPort(Constants.XMPP_PORT);
config.setHost(Constants.XMPP_HOST);
} catch (XmppStringprepException e) {
e.printStackTrace();
}
try {
InetAddress inetAddress = InetAddress.getByName(Constants.XMPP_HOST);
config.setHostAddress(inetAddress);
} catch (UnknownHostException e) {
e.printStackTrace();
}
// // config.setPort(Constants.XMPP_PORT);
// SmackDebuggerFactory smackDebuggerFactory = new SmackDebuggerFactory() {
// @Override
// public SmackDebugger create(XMPPConnection connection) throws IllegalArgumentException {
// return null;
// }
// };
// SASLAuthentication.unBlacklistSASLMechanism("AuthName");
//SASLAuthentication.blacklistSASLMechanism("AuthName");
config.setDebuggerEnabled(true);
config.setSendPresence(true);
// try {
// config.setResource(Constants.XMPP_RESOURCE);
// } catch (XmppStringprepException e) {
// e.printStackTrace();
// }
XMPPTCPConnection.setUseStreamManagementResumptiodDefault(true);
XMPPTCPConnection.setUseStreamManagementDefault(true);
// if (connection == null) {
connection = new XMPPTCPConnection(config.build());
// }
// connection.removeConnectionListener(mConnectionListener);
connection.addConnectionListener(mConnectionListener);
// StanzaFilter filter = MessageTypeFilter.CHAT;
// connection.removeAsyncStanzaListener(mStanzaListener);
connection.addAsyncStanzaListener(mStanzaListener, new StanzaFilter() {
@Override
public boolean accept(Stanza stanza) {
return true;
}
});
//
Roster.setDefaultSubscriptionMode(Roster.SubscriptionMode.manual);
roster = Roster.getInstanceFor(connection);
roster.addRosterListener(mRoasterListener);
//get Entry
}
2),这里是ConnectionConfiguration
@SuppressLint("TrulyRandom")
private XMPPConnection createConnection() {
Log.e("ad",">getServerHost>>"+PreferenceUtils.getServerHost(context));
ConnectionConfiguration config = new ConnectionConfiguration(PreferenceUtils.getServerHost(context), PORT);
SSLContext sc = null;
MemorizingTrustManager mtm = null;
try {
mtm = new MemorizingTrustManager(context);
sc = SSLContext.getInstance("TLS");
sc.init(null, new X509TrustManager[] { mtm }, new SecureRandom());
} catch (NoSuchAlgorithmException e) {
throw new IllegalStateException(e);
} catch (KeyManagementException e) {
throw new IllegalStateException(e);
}
config.setCustomSSLContext(sc);
config.setHostnameVerifier(mtm.wrapHostnameVerifier(new org.apache.http.conn.ssl.StrictHostnameVerifier()));
config.setSecurityMode(SecurityMode.required);
config.setReconnectionAllowed(false);
config.setSendPresence(false);
return new XMPPTCPConnection(config);
}