· 10 years ago · Feb 20, 2016, 07:15 AM
1/**
2 *
3 */
4package se.redsox;
5
6import org.jivesoftware.smack.Connection;
7import org.jivesoftware.smack.ConnectionConfiguration;
8import org.jivesoftware.smack.XMPPConnection;
9import org.jivesoftware.smack.XMPPException;
10import org.jivesoftware.smack.Roster;
11import org.jivesoftware.smack.RosterEntry;
12import org.jivesoftware.smack.Chat;
13import org.jivesoftware.smack.ChatManager;
14import org.jivesoftware.smack.MessageListener;
15import org.jivesoftware.smack.packet.Message;
16import org.jivesoftware.smack.SmackConfiguration;
17
18/**
19 * @author pjn
20 *
21 */
22public class Smacker {
23
24 /**
25 * @param args
26 */
27 public static void main(String[] args) {
28
29 SmackConfiguration.setLocalSocks5ProxyEnabled(false);
30
31 System.out.println("Starting session...");
32 try {
33 // Create a connection to the igniterealtime.org XMPP server.
34 String server = "brooklyn.local";
35 int port = 5222;
36 //ConnectionConfiguration config = new ConnectionConfiguration("talk.google.com", 5222, "gmail.com");
37 ConnectionConfiguration config = new ConnectionConfiguration(server, port);
38 Connection con = new XMPPConnection(config);
39 System.out.println("Connecting to : " + con.getHost() + ":" + con.getPort());
40 // Connect to the server
41 con.connect();
42 // Most servers require you to login before performing other tasks.
43 String username = "pjn";
44 String password = "zap123";
45 // will receive the message sent.
46 String receiver = "pjn";
47 con.login(username, password);
48 ChatManager cm = con.getChatManager();
49 Chat chat = cm.createChat(receiver, new MessageListener() {
50 public void processMessage(Chat chat, Message message) {
51 System.out.println("Received message: " + message);
52 }
53 });
54
55 chat.sendMessage("Smack> Message sent via API.");
56
57 //Thread.currentThread();
58 Thread.sleep(10000);
59 // Disconnect from the server
60 con.disconnect();
61 }
62 catch (XMPPException e) {
63 e.printStackTrace();
64 }
65
66 catch (InterruptedException e) {
67 e.printStackTrace();
68 }
69 System.out.println("Ended session...");
70 }
71
72}