This code allow you talk with other people under protocol xmpp instant messages ...
In this example i use de server "talk.google.com" ...
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package clientxmppapp;
/**
*
* @author Antonio Nobre
*/
import XMLParsing.XMLParsingMessage;
import java.util.*;
import java.io.*;
import org.jivesoftware.smack.Chat;
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;
import org.jivesoftware.smack.packet.Message;
public class ClientXmppApp implements MessageListener{
XMPPConnection connection;
public void login(String userName, String password) throws XMPPException
{
ConnectionConfiguration config = new ConnectionConfiguration("talk.google.com", 5222, "gmail.com");
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 ListaEnderecoEmail()
{
Roster roster = connection.getRoster();
Collection entradas = roster.getEntries();
System.out.println("\n\n" + entradas.size() + " list of addresses associated to email");
for(RosterEntry r:entradas)
{
System.out.println(r.getUser());
}
}
public void disconnect()
{
connection.disconnect();
}
public void processMessage(Chat chat, Message message)
{
try{
XMLParsingMessage xmlMessage = new XMLParsingMessage();
System.out.println("Begin...");
if(message.getType() == Message.Type.chat){
System.out.println("messageString: "+message.getBody().substring(0, 5));
if(message.getBody().substring(0, 5).equals("
xmlMessage.message(message);
System.out.println(chat.getParticipant() + " Send: " + message.getBody());
//System.out.println("XML 2");
}
else{
System.out.println("Inválid MESSAGE !!! - " + message.getBody());
}
}
}finally{
//System.out.println("Message send with sucess ...");
}
}
public static void main(String args[]) throws XMPPException, IOException
{
ClientXmppApp c = new ClientXmppApp();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String msg;
//XMPPConnection.DEBUG_ENABLED = true;
System.out.println("Write your address email: ");
String enderecoEmail = br.readLine();
System.out.println("Write your password: ");
String passwordEmail = br.readLine();
System.out.println("Email: " + enderecoEmail + " Password " + passwordEmail);
c.login(enderecoEmail, passwordEmail);
c.ListaEnderecoEmail();
System.out.println("##########################################");
System.out.println("who you want talk? - Write email address and press 'Enter':");
String receptor = br.readLine();
System.out.println("##########################################");
System.out.println("Write your message and press 'Enter':");
System.out.println("###########################################\n");
while( !(msg=br.readLine()).equals("bye"))
{
c.sendMessage(msg, receptor);
}
c.disconnect();
System.exit(0);
}
}
Subscrever:
Enviar feedback (Atom)
Sem comentários:
Enviar um comentário