Working with Xmpp in Android
// Create variable for XMPPConnection.
private XMPPConnection xmppConnection = null;
// Create packet listener to get reply from server.
private MyPacketListener myPacketListener = null;
// connect to XMPP server
private void connectToXmpp()
{
new Thread(new Runnable()
{
public void run()
{
// Create a connection
// pass server url , port number and service.
// server url and service are same.
ConnectionConfiguration connectionConfiguration = new ConnectionConfiguration(
"23.239.201.132", 5222, "23.239.201.132");
xmppConnection = new XMPPConnection(connectionConfiguration);
connectionConfiguration.setSASLAuthenticationEnabled(true);
try
{
xmppConnection.connect();
Log.i("Tag", "XMPP " + "CONNECTED TO " + xmppConnection.getHost());
try
{
// pass username and password for login with xmppConnection
xmppConnection.login("8888888888", "8888888888");
Log.i("Tag", "XMPP " + "LOGGED IN AS " + xmppConnection.getUser());
// Set the status to available
Presence presence = new Presence(Presence.Type.available);
xmppConnection.sendPacket(presence);
setConnection(xmppConnection);
}
catch (XMPPException ex)
{
Log.e("Tag", "XMPP " + "FAILED TO LOG IN");
setConnection(null);
}
}
catch (XMPPException ex)
{
Log.e("Tag", "XMPP " + "FAILED TO LOG IN AS "
+ xmppConnection.getHost());
Log.e("Tag", "ERROR = " + ex.toString());
setConnection(null);
}
}
}).start();
}
// setConnection() that use for set packet listener
public void setConnection(XMPPConnection connection)
{
this.xmppConnection = connection;
if (connection != null)
{
myPacketListener = new MyPacketListener();
// Add a packet listener to get messages sent to us
PacketFilter filter = new MessageTypeFilter(Message.Type.chat);
connection.addPacketListener(myPacketListener, filter);
}
}
// if you wont to remove connection then call this method.
public void removeConnection(XMPPConnection connection)
{
if (connection != null)
{
if (myPacketListener != null)
connection.removePacketListener(myPacketListener);
}
}
// this class continue receive message from server.
private class MyPacketListener implements PacketListener
{
public void processPacket(Packet packet)
{
final Message message = (Message) packet;
if (message.getBody() != null)
{
// get sender name from message.
final String fromName = StringUtils.parseBareAddress(message.getFrom());
Log.e("Tag", "XMPP " + "GOT TEXT [" + message.getBody() + "] FROM ["
+ fromName + "]");
// get message body that contains message.
String reply_msg = message.getBody();
}
}
}
private XMPPConnection xmppConnection = null;
// Create packet listener to get reply from server.
private MyPacketListener myPacketListener = null;
// connect to XMPP server
private void connectToXmpp()
{
new Thread(new Runnable()
{
public void run()
{
// Create a connection
// pass server url , port number and service.
// server url and service are same.
ConnectionConfiguration connectionConfiguration = new ConnectionConfiguration(
"23.239.201.132", 5222, "23.239.201.132");
xmppConnection = new XMPPConnection(connectionConfiguration);
connectionConfiguration.setSASLAuthenticationEnabled(true);
try
{
xmppConnection.connect();
Log.i("Tag", "XMPP " + "CONNECTED TO " + xmppConnection.getHost());
try
{
// pass username and password for login with xmppConnection
xmppConnection.login("8888888888", "8888888888");
Log.i("Tag", "XMPP " + "LOGGED IN AS " + xmppConnection.getUser());
// Set the status to available
Presence presence = new Presence(Presence.Type.available);
xmppConnection.sendPacket(presence);
setConnection(xmppConnection);
}
catch (XMPPException ex)
{
Log.e("Tag", "XMPP " + "FAILED TO LOG IN");
setConnection(null);
}
}
catch (XMPPException ex)
{
Log.e("Tag", "XMPP " + "FAILED TO LOG IN AS "
+ xmppConnection.getHost());
Log.e("Tag", "ERROR = " + ex.toString());
setConnection(null);
}
}
}).start();
}
// setConnection() that use for set packet listener
public void setConnection(XMPPConnection connection)
{
this.xmppConnection = connection;
if (connection != null)
{
myPacketListener = new MyPacketListener();
// Add a packet listener to get messages sent to us
PacketFilter filter = new MessageTypeFilter(Message.Type.chat);
connection.addPacketListener(myPacketListener, filter);
}
}
// if you wont to remove connection then call this method.
public void removeConnection(XMPPConnection connection)
{
if (connection != null)
{
if (myPacketListener != null)
connection.removePacketListener(myPacketListener);
}
}
// this class continue receive message from server.
private class MyPacketListener implements PacketListener
{
public void processPacket(Packet packet)
{
final Message message = (Message) packet;
if (message.getBody() != null)
{
// get sender name from message.
final String fromName = StringUtils.parseBareAddress(message.getFrom());
Log.e("Tag", "XMPP " + "GOT TEXT [" + message.getBody() + "] FROM ["
+ fromName + "]");
// get message body that contains message.
String reply_msg = message.getBody();
}
}
}
Comments
Post a Comment