使用 smack api 连接到 openfire 服务器
Posted
技术标签:
【中文标题】使用 smack api 连接到 openfire 服务器【英文标题】:Connecting to openfire server using smack api 【发布时间】:2018-08-14 09:23:44 【问题描述】:我一直在尝试使用 smack api 连接到我的本地 openfire 服务器。但我无法连接。
Exception in thread "Smack Packet Reader (0)" java.lang.AssertionError
11:08:23 AM SENT (0): <stream:stream xmlns='jabber:client' to='localhost' xmlns:stream='http://etherx.jabber.org/streams' version='1.0' from='admin@localhost' xml:lang='en'>
11:08:23 AM RECV (0): <?xml version='1.0' encoding='UTF-8'?><stream:stream xmlns:stream="http://etherx.jabber.org/streams" xmlns="jabber:client" from="my-openfire-domain" id="1qpatj8yxs" xml:lang="en" version="1.0"> at org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketReader.parsePackets(XMPPTCPConnection.java:992)
at org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketReader.access$200(XMPPTCPConnection.java:937)
at org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketReader$1.run(XMPPTCPConnection.java:952)
at java.base/java.lang.Thread.run(Thread.java:844)
org.jivesoftware.smack.SmackException$NoResponseException: No response received within reply timeout. Timeout was 5000ms (~5s). Used filter: No filter used or filter was 'null'.
at org.jivesoftware.smack.SmackException$NoResponseException.newWith(SmackException.java:106)
at org.jivesoftware.smack.SmackException$NoResponseException.newWith(SmackException.java:85)
at org.jivesoftware.smack.SynchronizationPoint.checkForResponse(SynchronizationPoint.java:253)
at org.jivesoftware.smack.SynchronizationPoint.checkIfSuccessOrWait(SynchronizationPoint.java:146)
at org.jivesoftware.smack.SynchronizationPoint.checkIfSuccessOrWaitOrThrow(SynchronizationPoint.java:125)
at org.jivesoftware.smack.tcp.XMPPTCPConnection.connectInternal(XMPPTCPConnection.java:837)
at org.jivesoftware.smack.AbstractXMPPConnection.connect(AbstractXMPPConnection.java:360)
at de.daarwin.charls.common.connect.ConnectionManager.login(ConnectionManager.java:25)
at smack.ConnectionTest.testConnectionToLocalhost(ConnectionTest.java:24)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:73)
at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:83)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
有我用来连接的代码。
AbstractXMPPConnection connection;
XMPPTCPConnectionConfiguration config = XMPPTCPConnectionConfiguration.builder()
.setUsernameAndPassword(userName, password)
.setServiceName("localhost")
.setHost("localhost")
.setPort(5222)
.setSecurityMode(SecurityMode.disabled) // Do not disable TLS except for test purposes!
.setDebuggerEnabled(true)
.build();
connection = new XMPPTCPConnection(config);
connection.connect().login();
我使用的是 Smack api 4.15 - 但是使用 4.3 时我仍然无法连接。
我想要实现的目标 - 我想为 openfire 服务器构建某种包装器,但如果我无法连接,那就很难了。
服务器在 Java 1.8 上运行,我很确定这很好 - 我可以使用像 Spark 这样的通信器连接到它。
【问题讨论】:
如果您的问题解决了,请给我的答案投票。谢谢 【参考方案1】:我使用的是 smack v4.3,但我看到了一些新的错误,并将其降级到 4.2.2,所有功能都可以正常工作。
您可以尝试使用IP address
而不是localhost
进行连接,但我建议使用v4.2.2。
XMPPTCPConnectionConfiguration.Builder config = XMPPTCPConnectionConfiguration.builder();
config.setHost("192.168.1.50");
config.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
config.setSendPresence(false);
config.setHostAddress(InetAddress.getByName("192.168.1.50");
config.setPort(5222);
config.setResource("myResource");
config.setDebuggerEnabled(true);
config.setXmppDomain("myDomain");
connection = new XMPPTCPConnection(config.build());
connection.setUseStreamManagement(false);
【讨论】:
【参考方案2】:好吧,我想通了:
XMPPTCPConnectionConfiguration.Builder configBuilder = XMPPTCPConnectionConfiguration.builder();
configBuilder.setUsernameAndPassword(username, password);
configBuilder.setHost(host);
configBuilder.setServiceName(serviceName);
configBuilder.setResource(resource);
configBuilder.setSecurityMode(SecurityMode.disabled);
这对我有用。
【讨论】:
以上是关于使用 smack api 连接到 openfire 服务器的主要内容,如果未能解决你的问题,请参考以下文章
使用 java 连接到 xmpp(openfire) 服务器时出错
我在使用 smack 4.2 Openfire 创建 XMPP 客户端时遇到问题