How to use smack with Openfire

此生再无相见时 提交于 2019-12-03 14:49:00
Tim Büthe

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.

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.

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);
}
}
deepakssn

In JSP / Java, import the smack.jar

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

Place smack.jar in

tomcat/lib 

or yourwebapp/WEB-INF/lib

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!