Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2017
467
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.51 KB | None | 0 0
  1. package com.someapp.networking;
  2.  
  3. import java.util.Collection;
  4.  
  5. import org.jivesoftware.smack.Chat;
  6. import org.jivesoftware.smack.ChatManager;
  7. import org.jivesoftware.smack.ConnectionConfiguration;
  8. import org.jivesoftware.smack.ConnectionConfiguration.SecurityMode;
  9. import org.jivesoftware.smack.filter.MessageTypeFilter;
  10. import org.jivesoftware.smack.filter.PacketFilter;
  11. import org.jivesoftware.smack.packet.Message;
  12. import org.jivesoftware.smack.packet.Packet;
  13. import org.jivesoftware.smack.util.StringUtils;
  14. import org.jivesoftware.smack.MessageListener;
  15. import org.jivesoftware.smack.PacketListener;
  16. import org.jivesoftware.smack.Roster;
  17. import org.jivesoftware.smack.RosterEntry;
  18. import org.jivesoftware.smack.SASLAuthentication;
  19. import org.jivesoftware.smack.SmackConfiguration;
  20. import org.jivesoftware.smack.XMPPConnection;
  21. import org.jivesoftware.smack.XMPPException;
  22.  
  23.  
  24.  
  25. import android.util.Log;
  26.  
  27. public class XMPPMessenger
  28. {
  29.     private final String XMPP_SERVER = "jabber.ru";
  30.     private final String USERNAME = "lets_test";
  31.     private final String PASSWORD = "lets_test";   
  32. /*  private final String XMPP_SERVER = "talk.google.com";
  33.     private final String USERNAME = "letstest49@gmail.com";
  34.     private final String PASSWORD = "lets_test";*/
  35. /*  private final String XMPP_SERVER = "localhost";
  36.     private final String USERNAME = "admin@localhost";
  37.     private final String PASSWORD = "12345";*/
  38.     private static final int PACKET_REPLY_TIMEOUT = 1500; // millis
  39.     private static final int PORT = 5222;
  40.    
  41.     private XMPPConnection connection;
  42.     private ConnectionConfiguration config;
  43.    
  44.     private ChatManager chatManager;
  45.     private MessageListener messageListener = null;
  46.    
  47.    
  48.     public boolean init()
  49.     {
  50.         SmackConfiguration.setPacketReplyTimeout(PACKET_REPLY_TIMEOUT);
  51.         config = new ConnectionConfiguration(XMPP_SERVER, PORT);
  52.         config.setSASLAuthenticationEnabled(false);
  53.         config.setSecurityMode(SecurityMode.disabled);
  54.         connection = new XMPPConnection(config);
  55.         Log.i("XMPP", connection.getHost().concat(" ".concat(Integer.toString(connection.getPort()))));
  56.         try
  57.         {
  58.             connection.connect();
  59.         }
  60.         catch (XMPPException e)
  61.         {          
  62.             e.printStackTrace();
  63.             setConnection(null);
  64.             return false;
  65.         }
  66.         return true;       
  67.     }
  68.    
  69.     public boolean login()
  70.     {
  71.         if (connection != null && connection.isConnected())
  72.         {
  73.             Log.i("XMPP", connection.getHost());
  74.             try
  75.             {
  76.                 connection.login(USERNAME, PASSWORD);
  77.             }
  78.             catch (XMPPException e)
  79.             {
  80.                 e.printStackTrace();
  81.                 Log.e("XMPP", connection.getHost().concat(" ".concat(Integer.toString(connection.getPort()))));
  82.                 Log.e("XMPP", Boolean.toString(connection.isConnected()));
  83.                 setConnection(null);
  84.                 return false;
  85.             }
  86.             return true;
  87.         }
  88.         Log.e("XMPP", connection.getHost().concat(" ".concat(Integer.toString(connection.getPort()))));
  89.         Log.e("XMPP", Boolean.toString(connection.isConnected()));
  90.         return false;
  91.     }
  92.    
  93.     public void destroy()
  94.     {
  95.         if (connection != null && connection.isConnected())
  96.         {
  97.             connection.disconnect();
  98.         }
  99.     }
  100.    
  101.     public boolean sendMessage(String buddyJID, String message)
  102.     {
  103.         Chat chat = chatManager.createChat(buddyJID, messageListener);
  104.         try
  105.         {
  106.             chat.sendMessage(message);
  107.         }
  108.         catch (Exception e)
  109.         {
  110.             e.printStackTrace();
  111.             return false;
  112.         }
  113.         return true;
  114.     }
  115.    
  116.     public boolean createEntry(String user, String name)
  117.     {
  118.         Roster roster = connection.getRoster();
  119.         try
  120.         {
  121.             roster.createEntry(user, name, null);
  122.         }
  123.         catch (Exception e)
  124.         {
  125.             e.printStackTrace();
  126.             return false;
  127.         }
  128.         return true;
  129.     }
  130.    
  131.     public Collection<RosterEntry> getRosterEntries()
  132.     {
  133.         Roster roster = connection.getRoster();
  134.         return roster.getEntries();
  135.     }
  136.    
  137.     public void setMessageListener(MessageListener messageListener)
  138.     {
  139.         this.messageListener = messageListener;
  140.     }
  141.    
  142.     public void setConnection(XMPPConnection connection)
  143.     {
  144.         this.connection = connection;
  145.         if (connection != null)
  146.         {
  147.             PacketFilter filter = new MessageTypeFilter(Message.Type.chat);
  148.             connection.addPacketListener(new PacketListenerImpl(), filter);
  149.         }
  150.     }
  151.    
  152.     private class PacketListenerImpl implements PacketListener
  153.     {
  154.  
  155.         @Override
  156.         public void processPacket(Packet packet)
  157.         {
  158.             Message message = (Message) packet;
  159.             if (message.getBody() != null)
  160.             {   // TODO: Добавить проверку на пустую строку
  161.                 String from = StringUtils.parseBareAddress(message.getFrom());
  162.                 String body = message.getBody();
  163.                 Log.i("XMPP", from + ": " + body);
  164.             }
  165.            
  166.         }
  167.        
  168.     }
  169.  
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement