Skip to content

Instantly share code, notes, and snippets.

@mindjiver
Created January 13, 2012 08:56
  • Star 0 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save mindjiver/1605194 to your computer and use it in GitHub Desktop.
Smack API example
/**
*
*/
package se.redsox;
import org.jivesoftware.smack.Connection;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.ChatManager;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.SmackConfiguration;
/**
* @author pjn
*
*/
public class Smacker {
/**
* @param args
*/
public static void main(String[] args) {
SmackConfiguration.setLocalSocks5ProxyEnabled(false);
System.out.println("Starting session...");
try {
// Create a connection to the igniterealtime.org XMPP server.
String server = "brooklyn.local";
int port = 5222;
//ConnectionConfiguration config = new ConnectionConfiguration("talk.google.com", 5222, "gmail.com");
ConnectionConfiguration config = new ConnectionConfiguration(server, port);
Connection con = new XMPPConnection(config);
System.out.println("Connecting to : " + con.getHost() + ":" + con.getPort());
// Connect to the server
con.connect();
// Most servers require you to login before performing other tasks.
String username = "pjn";
String password = "zap123";
// will receive the message sent.
String receiver = "pjn";
con.login(username, password);
ChatManager cm = con.getChatManager();
Chat chat = cm.createChat(receiver, new MessageListener() {
public void processMessage(Chat chat, Message message) {
System.out.println("Received message: " + message);
}
});
chat.sendMessage("Smack> Message sent via API.");
//Thread.currentThread();
Thread.sleep(10000);
// Disconnect from the server
con.disconnect();
}
catch (XMPPException e) {
e.printStackTrace();
}
catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Ended session...");
}
}
@farazdurrani
Copy link

This example does not work. I get errors on 4 of the imports after copying the jar files.

  1. The import org.jivesoftware.smack.Connection cannot be resolved
    and others..

@thanhdevapp
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment