How to use smack with Openfire

2020-06-17 06:53发布

Hi I am planning to develop a chat client which can connect to gtalk facebook etc...I have decided to use the smack API along with openfire..

But I need little guidance as to how to use it with openfire server..

And does the openfire provide a basic UI like log in box chat window etc...

I need to know how to plug or use smack with openfire

Thanks:)

4条回答
何必那么认真
2楼-- · 2020-06-17 07:07

This is a sample, which will help set the status message on gtalk.

import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.packet.Presence;

public class SmackToGtalk {
public static void main(String[] args) 
{
    ConnectionConfiguration config = new ConnectionConfiguration(
            "talk.google.com", 5222, "google.com");
    XMPPConnection connection = new XMPPConnection(config);
    Presence presence;
    String status;

    try {
        connection.connect();
        connection.login("mail_id@gmail.com", "password");
        status = "DND";

        presence = new Presence(Presence.Type.available, status, 24,
                Presence.Mode.available);
        while (true) {
            status = set(status);
            presence.setStatus(status);
            connection.sendPacket(presence);
            Thread.sleep(1000);
        }

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        connection.disconnect();
    }
}

private static String set(String input) {
    return input.substring(1) + input.charAt(0);
}
}
查看更多
劫难
3楼-- · 2020-06-17 07:25

I have decided to use the smack API along with openfire.. But I need little guidance as to how to use it with openfire server..

What about Smack API Getting Started?

And does the openfire provide a basic UI like log in box chat window etc...

OpenFire is just the Server. To actually chat, you'll need some Jabber/XMPP Client. You could use Spark for tests.

查看更多
兄弟一词,经得起流年.
4楼-- · 2020-06-17 07:29

Configure openfire then refer to documentation provided by Smack. It has easy to understand examples. FYI openfire works fine with gtalk but with facebook it is very slow.


Sample code:-

ConnectionConfiguration config = new ConnectionConfiguration(host, 5222);
XMPPConnection connection = new XMPPConnection(config);
connection.connect();
connection.login(user_name, password);

Here host is the ip/domain name where openfire is configured.

查看更多
我命由我不由天
5楼-- · 2020-06-17 07:30

In JSP / Java, import the smack.jar

<%@ page import="org.jivesoftware.smack.*;" %>

Place smack.jar in

tomcat/lib 

or yourwebapp/WEB-INF/lib

查看更多
登录 后发表回答