简体   繁体   English

Smack 在 Java EE 中是否运行良好

[英]Does Smack work well in Java EE

Does Smack function properly in Java EE?? Smack function 在 Java EE 中是否正确? I am having issues with presence.我遇到了存在问题。 I get the credentials from the login form via doPost method..I can able to successfully authenticate as well as connection.getRoster() also works.Next I want to show only users who are online so when I get the presence of user,presence object stores default value "unavailable" for all users even when they are available!!我通过 doPost 方法从登录表单中获取凭据..我可以成功进行身份验证以及 connection.getRoster() 也可以工作。接下来我只想显示在线的用户,这样当我得到用户的存在时,存在object 为所有用户存储默认值“不可用”,即使他们可用!

The whole chat app works without flaw in a normal java class without any change..整个聊天应用程序在正常的 java class 中没有任何变化。

            String userName = request.getParameter("username");
    String password = request.getParameter("password");

        HttpSession session=request.getSession();
        session.setAttribute("username", userName);

    SmackAPIGtalkServlet gtalk = new SmackAPIGtalkServlet();

    ConnectionConfiguration config = new ConnectionConfiguration(
            "talk.google.com", 5222, "gmail.com");
    connection = new XMPPConnection(config);
    config.setSASLAuthenticationEnabled(false);
    try {
        connection.connect();
    } catch (XMPPException e) {
        e.printStackTrace();
    }
    try {
        connection.login(userName, password);
    } catch (XMPPException e) {
        e.printStackTrace();
    }
    System.out.println(connection.isAuthenticated());
    boolean status = connection.isAuthenticated();
    if (status == true) {
        gtalk.displayOnlineBuddyList();
        response.sendRedirect("Roster.jsp");

    }
    else
    {
        response.sendRedirect("Failed.jsp");
    }
}

public void displayOnlineBuddyList() {
    Roster roster = connection.getRoster();
    Collection<RosterEntry> entries = roster.getEntries();
    int count1 = 0;
    int count2 = 0;
    for (RosterEntry r : entries) {
        Presence presence = roster.getPresence(r.getUser());
        if (presence.getType() == Presence.Type.unavailable) {
            // System.out.println(user + "is offline");
            count1++;
        } else {
            System.out.println(name+user + "is online");
            count2++;
        }
    }
    roster.addRosterListener(new RosterListener() {
        // Ignored events public void entriesAdded(Collection<String>
        // addresses) {}
        public void entriesDeleted(Collection<String> addresses) {
        }

        public void entriesUpdated(Collection<String> addresses) {
        }

        public void presenceChanged(Presence presence) {
            System.out.println("Presence changed: " + presence.getFrom()
                    + " " + presence);
        }

        @Override
        public void entriesAdded(Collection<String> arg0) {
            // TODO Auto-generated method stub

        }
    });
}

I am stuck with this and not able to get the code working with servlets..Can anyone help me out??我被困住了,无法让代码与 servlets 一起工作。谁能帮帮我?

You have to implement the RosterListener interface in which you have to override the presenceChanged method in that you can get the presence of the users.您必须实现RosterListener接口,在该接口中您必须覆盖presentChanged方法,这样您才能获得用户的存在。

It works for me.这个对我有用。

When you are getting the rosters of GTalk all will have status as unavailable.当您获得 GTalk 的花名册时,所有状态都将显示为不可用。

But after sometime their presence changes and the presence can be get from the presenceChanged method in the RosterListner but for that you have to implement the RosterListener's presenceChnaged method.但是一段时间后,他们的存在发生变化,并且可以从 RosterListner 中的 presentChanged 方法获取存在,但为此您必须实现 RosterListener 的 presenceChnaged 方法。

And ya it works well in Java EE, Android as well as WAP.而且它在 Java EE、Android 以及 WAP 中运行良好。

Will Smack work inside of Java EE, yes and no. Smack 是否会在 Java EE 内部工作,是的,不是的。 Smack will work inside of a web container, but since it creates its own threads it will NOT work inside of an EJB container. Smack 将在 web 容器内工作,但由于它创建自己的线程,因此无法在 EJB 容器内工作。 So it will work depending on where you are running it.所以它会根据你在哪里运行。

To understand some of your issues, you have to understand that the lifecycle of your objects in a servlet is tied to the request/response cycle of each request.要了解您的一些问题,您必须了解 servlet 中对象的生命周期与每个请求的请求/响应周期相关联。 This is not the same as a standard java app where the objects will typically live as long as you need them to, since you control their lifecycle.这与标准的 java 应用程序不同,因为您可以控制它们的生命周期,所以对象通常会根据您的需要而存在。

For example, in the code you have shown, you create the connection for each request (I assume, since not all the code is shown).例如,在您显示的代码中,您为每个请求创建连接(我假设,因为并未显示所有代码)。 Therefore registering listeners against that connection will be pointless since it will pass out of scope as soon as you leave the method, and eventually get garbage collected.因此,针对该连接注册侦听器将毫无意义,因为一旦您离开该方法,它就会从 scope 传递出去,并最终被垃圾收集。 You will have to maintain the connections outside of the scope of the servlet requests for this to work, otherwise you will be opening and closing connections for each request.您必须维护 servlet 请求的 scope 之外的连接才能正常工作,否则您将打开和关闭每个请求的连接。

XMPP is completely asynchronous by nature whereas servlet requests are synchronous. XMPP 本质上是完全异步的,而 servlet 请求是同步的。 You have to put some effort in to making them work together, so don't expect code that works in a standalone app to simply work in this environment.您必须付出一些努力才能使它们一起工作,所以不要期望在独立应用程序中工作的代码可以在这种环境中简单地工作。

Does Smack function properly in Java EE?? Smack function 在 Java EE 中是否正确? I am having issues with presence.我遇到了存在问题。 I get the credentials from the login form via doPost method..I can able to successfully authenticate as well as connection.getRoster() also works.Next I want to show only users who are online so when I get the presence of user,presence object stores default value "unavailable" for all users even when they are available!!我通过 doPost 方法从登录表单中获取凭据..我可以成功进行身份验证以及 connection.getRoster() 也可以工作。接下来我只想显示在线的用户,这样当我得到用户的存在时,存在object 为所有用户存储默认值“不可用”,即使他们可用! here my code这是我的代码

<%
 Roster rst = roster;
 rst.addRosterListener(new RosterListener() {
public void entriesAdded(final Collection args) {}

public void entriesDeleted(final Collection<String> addresses) {}

public void entriesUpdated(final Collection<String> addresses) {}

public void presenceChanged(final Presence presence) {
    final Presence prsence1 = presence;
    prsenceChanged(prsence1);
    if (prsence1.isAvailable()) {
        System.out.println("Is Available: " + presence.isAvailable());
    } 


}
});

 %> 



<%!void prsenceChanged(Presence presence){ if(null != presence){%>
<script language="javascript"> 
    alert("hai");   

</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM