我打算使用Smack通过Firebase CCS发送消息。我为我的Web App修改了一个简单的CCS客户端,但是当我尝试建立连接时,它会导致异常。 我正在使用 Smack 4.2.0
这是连接过程。
1)我客户端的连接方法:
public void connect() throws XMPPException{
try{
config = XMPPTCPConnectionConfiguration.builder()
.setPort(Config.FCM_PORT)
.setHost("fcm-xmpp.googleapis.com")
.setXmppDomain("googleapis.com")
.setSecurityMode(/*Default; Explicit setting for emphasis*/SecurityMode.ifpossible)
.setSendPresence(true)
.setUsernameAndPassword(fcmServerUsername, mApiKey)
.setSocketFactory(SSLSocketFactory.getDefault())
.setDebuggerEnabled(mDebuggable)/* Launch a window with info about packets sent and received */
.build();
}
catch(XmppStringprepException e){
e.printStackTrace();
}
connection = new XMPPTCPConnection(config);
// Configuring Automatic reconnection
ReconnectionManager manager = ReconnectionManager.getInstanceFor(connection);
manager.setReconnectionPolicy(ReconnectionManager.ReconnectionPolicy.RANDOM_INCREASING_DELAY);
manager.enableAutomaticReconnection();
// Connect now then login
try{
connection.connect();
connection.login();
}
// TODO: Handle the exceptions if possible appropriately
catch(SmackException sme){
logger.severe(sme.getMessage());
sme.printStackTrace();
}
catch(IOException ioe){
logger.severe(ioe.getMessage());
ioe.printStackTrace();
}
catch(InterruptedException ie){
logger.severe("Connection got interrupted!!");
ie.printStackTrace();
}
}
2)我跟踪了异常,我在这里得到了它:( Smack的来源)
行 - HostAddress hostAddress = DNSUtil.getDNSResolver().lookupHostAddress(config.host, config.port, failedAddresses, config.getDnssecMode());
// AbstractXMPPConnection.java
protected List<HostAddress> populateHostAddresses() {
List<HostAddress> failedAddresses = new LinkedList<>();
if (config.hostAddress != null) {
hostAddresses = new ArrayList<>(1);
HostAddress hostAddress = new HostAddress(config.port, config.hostAddress);
hostAddresses.add(hostAddress);
}
else if (config.host != null) {
hostAddresses = new ArrayList<HostAddress>(1);
HostAddress hostAddress = DNSUtil.getDNSResolver().lookupHostAddress(config.host, config.port, failedAddresses, config.getDnssecMode());
if (hostAddress != null) {
hostAddresses.add(hostAddress);
}
} else {
// N.B.: Important to use config.serviceName and not AbstractXMPPConnection.serviceName
hostAddresses = DNSUtil.resolveXMPPServiceDomain(config.getXMPPServiceDomain().toString(), failedAddresses, config.getDnssecMode());
}
// Either the populated host addresses are not empty *or* there must be at least one failed address.
assert(!hostAddresses.isEmpty() || !failedAddresses.isEmpty());
return failedAddresses;
}
异常是 NullPointerException ,我发现getDNSResolver()
返回null。在我引用的所有资源中,没有任何与DNS解析器相关的内容,因为它应该由Smack内部处理。所以我的问题是,我是否错过了一些重要的配置或步骤来建立连接?
编辑:我在这里问,因为Smack是庞大的lib,可能有一些配置有人知道我可能错过了。我无法直接设置DNSResolver
答案 0 :(得分:3)
在4.2.0中不确定但在4.2.2(及更新版本)中,你需要在你的类路径中使用smack-resolver-dnsjava-4.2.2.jar,smack调用包含在包中的DNSUtil,如果该类不存在它返回NullPointerException。
希望这有帮助! 大卫
答案 1 :(得分:2)
这不是Smack源代码中的错误,因为他们的4.2.0升级指南明确提到:
**
API更改
**
警告:此列表可能不完整
介绍
ConnectionConfiguration.setHostAddress(InetAddress)
在先前版本的Smack中,
ConnectionConfiguration.setHost(String)
可用于设置 XMPP服务的主机IP地址。由于这个原因,这已不再可能 添加了DNSSEC支持。您必须使用新的连接配置 而是ConnectionConfiguration.setHostAddress(InetAddress)
。
这似乎是一个错误,因为 我通过提供主机地址(应该从{Host,Domain}推断)来解决它。那么,我怎么知道提供主机地址?
诀窍在于:( Smack&#39;来源)
// AbstractXMPPConnection.java
if (config.hostAddress != null) {
hostAddresses = new ArrayList<>(1);
HostAddress hostAddress = new HostAddress(config.port, config.hostAddress);
hostAddresses.add(hostAddress);
}
else if (config.host != null) {
hostAddresses = new ArrayList<HostAddress>(1);
HostAddress hostAddress = DNSUtil.getDNSResolver().lookupHostAddress(config.host, config.port, failedAddresses, config.getDnssecMode());
if (hostAddress != null) {
hostAddresses.add(hostAddress);
}
} else {
// N.B.: Important to use config.serviceName and not AbstractXMPPConnection.serviceName
hostAddresses = DNSUtil.resolveXMPPServiceDomain(config.getXMPPServiceDomain().toString(), failedAddresses, config.getDnssecMode());
}
您可以在此处看到if,else-if块,并且由于else if (config.host != null)
块中出现异常,我提供了hostAddress,以便它永远不会进入该块并且它可以正常工作。
我知道这有点像实际问题,但这似乎是Smack 4.2.0中的一个错误,除非有人反驳我。
奖励信息:如果在纠正此问题后,您在登录期间获得Base 64编码的另一个例外,请参阅此内容 - XMPP client using Smack 4.1 giving NullPointerException during login