Smack API

| 2 TrackBacks

Smack is an Open Source XMPP (Jabber) Java client library.
Writing a gTalk (Jabber/XMPP) client in Java has a example code. But it has exception.

java.lang.NullPointerException
at org.jivesoftware.smack.sasl.SASLMechanism.authenticate(SASLMechanism.java:117)

Just set SASL Authentication is disable, like below.
config.setSASLAuthenticationEnabled(false);

The complete java code is:

import java.util.*;
import java.io.*;

import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;

public class ChatClient implements MessageListener {
    XMPPConnection connection;

    public void login(String userName, String password) throws XMPPException {
        ConnectionConfiguration config = new ConnectionConfiguration(
                "talk.google.com", 5222, "gmail.com");
        config.setCompressionEnabled(true);
        config.setSASLAuthenticationEnabled(false);
        connection = new XMPPConnection(config);

        connection.connect();
        connection.login(userName, password);
    }

    public void sendMessage(String message, String to) throws XMPPException {
        Chat chat = connection.getChatManager().createChat(to, this);
        chat.sendMessage(message);
    }

    public void displayBuddyList() {
        Roster roster = connection.getRoster();
        roster.setSubscriptionMode(Roster.SubscriptionMode.accept_all);
        Collection<RosterEntry> entries = roster.getEntries();

        System.out.println("\n\n" + entries.size() + " buddy(ies):");
        for (RosterEntry r : entries) {
            System.out.println(r.getUser());
        }
    }

    public void disconnect() {
        connection.disconnect();
    }

    public void processMessage(Chat chat, Message message) {
        if (message.getType() == Message.Type.chat)
            System.out.println(chat.getParticipant() + " says: "
                    + message.getBody());
    }

    public static void main(String args[]) throws XMPPException, IOException {
        // declare variables
        ChatClient c = new ChatClient();
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String msg;

        // turn on the enhanced debugger
        XMPPConnection.DEBUG_ENABLED = false;

        // provide your login information here
        c.login("SendAccount", "SendPassword");

        c.displayBuddyList();
        System.out.println("-----");
        System.out.println("Enter your message in the console.");
        System.out.println("All messages will be sent to abhijeet.maharana");
        System.out.println("-----\n");

        while (!(msg = br.readLine()).equals("bye")) {
            // your buddy's gmail address goes here
            c.sendMessage(msg, "XXXXX@gmail.com");
        }

        c.disconnect();
        System.exit(0);
    }
}

2 TrackBacks

TrackBack URL: http://server.everfine.com.tw/blog/mt-tb.cgi/257

從自由軟體鑄造場得知Microsoft 打開 Messenger 大門 開始支援 XMPP,因此目前主流即時訊息都支援XMPP,包括Facebook chat。 既然如此,當然找看看熟悉的Smack套件是否也有MSN的sample code。果然Github上有MS放出的LiveSDK,便有JAVA跟Android的程式碼。有空再把拿token那程式改成用httpclient。 JAVA程式碼如下: package messenger;import java.net.URL;import java.... Read More

之前說到XMPP已經成為即時訊息的標準,那就利用Facebook手機即時通的便捷快速來送Facebook Chat訊息。 其實只要拿之前GTalk的程式碼,稍微修改一下,就可發送了。其中差異在於DIGEST-MD5的認證機制。在Smack討論區已經有人實作出XMPP DIGEST-MD5的物件。只要將login部份加上DIGEST-MD5認證。 完整程式碼如下: package messenger;import java.io.BufferedReader;import java.io.IOExc... Read More

April 2013

Sun Mon Tue Wed Thu Fri Sat
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30        

Archives

About this Entry

This page contains a single entry by philipz published on June 30, 2009 3:35 PM.

Run Shell Script from IE was the previous entry in this blog.

HTML Parsers in Java is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.