如何使用 java smack 库连接 XMPP bosh 服务器? [英] How to connect XMPP bosh server using java smack library?

查看:29
本文介绍了如何使用 java smack 库连接 XMPP bosh 服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 strophe.js 在 webapp 上使用 xmpp 客户端,根据我的用例场景,我必须快速切换到不同的页面

I have working xmpp client on webapp using strophe.js ,as per my use case scenario i have to switch to different pages rapidly

当前的方法并不安全,因为 jid 和密码在 java 脚本中可见,我正在寻找解决方法以在 strophe 客户端中实现安全性,并试图使连接时间(使用 bosh)更短,同时阅读XMPP 编程"一书使用 JavaScript 和 jQuery"by jake moffitt 我遇到了一个解决方案,其中元素我的上述两个问题都是实现会话机制.它说我们可以使用 strophe attach(jid,sid,rid) 连接到现有连接,所以我需要SID 和 RID,我可以从应用服务器获取!!!

Current approach is not secure as jid and password is visible in java script ,I was finding work around to implement security in strophe client an trying to make connection time(with bosh) more shorter ,while going through the book "XMPP Programming with JavaScript and jQuery"by jake moffitt i came across one solution which element both of my above problems is to implement session mechanism.which says that we can use strophe attach(jid,sid,rid) to connect to existing connection,so i need SID and RID ,which i can get from application server!!!

本书给出了用户登录 Web 应用程序时自动连接到 bosh 服务器的示例,作者使用 Python 中的 Django 项目实现了它,因为我使用 java 作为服务器端语言,所以我尝试使用 java 实现相同的示例smcak-4.0.3 和 smack-bosh-4.0.3但无法连接到 bosh 服务器(我使用 ejabberd 作为 xmpp 服务器)

book has given an example of automated connection to bosh server when user logged in the web application,author has implement it using an Django project in python,As I am using java as server side language i tried to implement same example using java smcak-4.0.3 and smack-bosh-4.0.3 but unable to connect to bosh server(i am using ejabberd as xmpp server)

我的代码如下

 BOSHConfiguration config = new BOSHConfiguration(false,"192.168.0.106",5280,"/http-bind/","192.168.0.106");
                XMPPBOSHConnection xbc=new XMPPBOSHConnection(config); 
                xbc.connect();
                xbc.login("admin", "admin");
                System.out.println(xbc.getConnectionID());

堆栈跟踪

java.lang.ClassNotFoundException: org.xmlpull.v1.XmlPullParserFactory
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1720)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571)
    at org.jivesoftware.smack.SmackConfiguration.processConfigFile(SmackConfiguration.java:352)
    at org.jivesoftware.smack.SmackConfiguration.processConfigFile(SmackConfiguration.java:347)
    at org.jivesoftware.smack.SmackConfiguration.<clinit>(SmackConfiguration.java:155)
    at org.jivesoftware.smack.ConnectionConfiguration.<init>(ConnectionConfiguration.java:67)

当我尝试登录 bosh 服务器时,每次都失败,我不确定这里出了什么问题,有人可以解释一下吗?

When i tried to login to bosh server it fails every time,i am not sure what is wrong here can some one explain me?

我发现的另一件事是可以使用xbc.getConnectionID()"获取会话标识符(SID),但如何找到请求标识符?

One more thing i have find is one can get session identifier(SID) using "xbc.getConnectionID()" but how to find request identifier?

对上述问题的任何帮助都将是适用的!!!!

Any help on above problem will be appriciable!!!!

提前致谢!

推荐答案

我遇到了类似的问题.我下载所有 smack github 我从/lib/导入 smack.jar 并添加 3 个 java 文件来自/src/main/java/org/jivesoftware/smack/

I had a similar problem. I donwload all the smack github I import smack.jar from /lib/ and add the 3 java files from /src/main/java/org/jivesoftware/smack/

我试图通过从/target/导入 smack-bosh-3.2.2-jar-with-dependencies.jar 来修复它.我不知道为什么这不起作用.

I tried to fix it by importing smack-bosh-3.2.2-jar-with-dependencies.jar from /target/. I don't know why this diddn't work.

最后,我阅读了这里 你需要下载所有的依赖库:jbosh-0.6.0.jarxlightweb-2.5.jarxSocket-2.4.6.jarxpp3-1.1.3.3.jardom4j-1.6.1.jar

Finally, I read here that you need to download all the dependencies libraries : jbosh-0.6.0.jar xlightweb-2.5.jar xSocket-2.4.6.jar xpp3-1.1.3.3.jar dom4j-1.6.1.jar

所以我将/lib/中的 smack.jar 与所有这些库一起使用,这个问题就解决了.

So I used smack.jar from /lib/ with all thoses libraries and this problem was solved.

正如我在评论中所说,您需要在检索 RID 之后.我使用了 jbosh 源并添加了以下几行:

As i said in comments, you need after to retreive RID. I used jbosh sources and add following lines :

In com.kenai.jbosh.BOSHClient class
//I introduced a new property
private Long rid;

//commented the following code
//long rid = requestIDSeq.getNextRID();
//and at that place added
this.rid = requestIDSeq.getNextRID();

//and finally added a new getter for rid
public Long getRid() {
    return rid;}

然后在 smack-bosh 中:

Then in smack-bosh :

In BOSHConnection.java
public Long getRid() {
    return client.getRid();}
public String getSid() {
    return sessionID;}

现在我被屏蔽了,因为我的会话被断开了.根据我的 openFire 日志,这是因为活动过度.因此,我正在寻找一种减少在线状态消息数量的解决方案.

Now I'm blocked cause my session is disconected. According to my openFire logs, it's because of overactivity. So I'm looking for a solution to reduce the number of presence messages.

这篇关于如何使用 java smack 库连接 XMPP bosh 服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆