码迷,mamicode.com
首页 > 其他好文 > 详细

XMPP 工具类

时间:2015-07-17 13:25:05      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:

继上次发文章又过了好久。程序出了问题,

发现XMPP的断线重连真的是个坑,在内网环境下随便自动重连,都没问题。

布到现网上问题就出现了,我了个日,目前还没有想到什么好的解决办法!

今天把我项目中XMPP的工具类贴出来,大家可以参考和点评一下。

没有很深入的研究过,也是从网上找了一些DEMO,弄下来自己看看代码,看看逻辑!

 

  1 public class XmppConnectionManager {
  2 
  3     private XMPPConnection connection;
  4     private static ConnectionConfiguration connectionConfig;
  5     private static XmppConnectionManager xmppConnectionManager;
  6     
  7     public static String host = "192.168.1.169";
  8     
  9     public static String name = "@test.com";
 10 
 11     static{
 12         try {
 13             Class.forName("org.jivesoftware.smack.ReconnectionManager");
 14         } catch (Exception e) {
 15             e.printStackTrace();
 16         }
 17     }
 18     synchronized public static XmppConnectionManager  getInstance(){
 19         if(xmppConnectionManager==null){
 20             xmppConnectionManager = new XmppConnectionManager();
 21         }
 22         return xmppConnectionManager;
 23     }
 24     
 25     public XMPPConnection init(Context context){
 26         Connection.DEBUG_ENABLED = false;
 27         ProviderManager pm = ProviderManager.getInstance();
 28         configureConnection(pm);
 29         
 30         connectionConfig = new ConnectionConfiguration(host,5222);
 31         connectionConfig.setSecurityMode(ConnectionConfiguration.SecurityMode.enabled);
 32         connectionConfig.setReconnectionAllowed(true);
 33         connectionConfig.setSASLAuthenticationEnabled(false); // 是否启用安全验证
 34         
 35         connectionConfig.setSendPresence(false);
 36         
 37         connection = new XMPPConnection(connectionConfig);
 38         DeliveryReceiptManager.getInstanceFor(connection).enableAutoReceipts();
 39         connection.addConnectionListener(new ConnectionManager(context));
 40     
 41         return connection;
 42     }
 43     
 44     
 45     public XMPPConnection getConnection(){    
 46         return connection;
 47     }
 48     
 49     public void disconnect(){
 50         if(connection!=null){
 51             connection.disconnect();
 52         }
 53     }
 54 //    /**
 55 //     * 打开心跳
 56 //    public void initXmppSmack(Context context){
 57 //        SmackAndroid.init(context);
 58 //        final ClassLoader appClsLoader = context.getClassLoader();
 59 //        try {
 60 //            Class.forName(org.jivesoftware.smackx.ping.PingManager.class.getName(),true,appClsLoader);
 61 //            SmackConfiguration.setDefaultPingInterval(1);
 62 //        } catch (ClassNotFoundException e) {
 63 //            e.printStackTrace();
 64 //        }
 65 //    }
 66     
 67     
 68     
 69     
 70     
 71     
 72     
 73     /**
 74      * 加入providers的函数 ASmack在/META-INF缺少一个smack.providers 文件
 75      * 
 76      * @param pm
 77      */
 78     public void configureConnection(ProviderManager pm) {
 79 
 80         // Private Data Storage
 81         pm.addIQProvider("query", "jabber:iq:private",new PrivateDataManager.PrivateDataIQProvider());
 82 
 83         // Time
 84         try {
 85             pm.addIQProvider("query", "jabber:iq:time",    Class.forName("org.jivesoftware.smackx.packet.Time"));
 86         } catch (ClassNotFoundException e) {
 87             Log.w("TestClient","Can‘t load class for org.jivesoftware.smackx.packet.Time");
 88         }
 89 
 90         // Roster Exchange
 91         pm.addExtensionProvider("x", "jabber:x:roster",new RosterExchangeProvider());
 92 
 93         // Message Events
 94         pm.addExtensionProvider("x", "jabber:x:event",new MessageEventProvider());
 95 
 96         // Chat State
 97         pm.addExtensionProvider("active","http://jabber.org/protocol/chatstates",new ChatStateExtension.Provider());
 98         pm.addExtensionProvider("composing","http://jabber.org/protocol/chatstates",new ChatStateExtension.Provider());
 99         pm.addExtensionProvider("paused","http://jabber.org/protocol/chatstates",new ChatStateExtension.Provider());
100         pm.addExtensionProvider("inactive","http://jabber.org/protocol/chatstates",new ChatStateExtension.Provider());
101         pm.addExtensionProvider("gone","http://jabber.org/protocol/chatstates",new ChatStateExtension.Provider());
102 
103         // XHTML
104         pm.addExtensionProvider("html", "http://jabber.org/protocol/xhtml-im",new XHTMLExtensionProvider());
105 
106         // Group Chat Invitations
107         pm.addExtensionProvider("x", "jabber:x:conference",new GroupChatInvitation.Provider());
108 
109         // Service Discovery # Items
110         pm.addIQProvider("query", "http://jabber.org/protocol/disco#items",new DiscoverItemsProvider());
111 
112         // Service Discovery # Info
113         pm.addIQProvider("query", "http://jabber.org/protocol/disco#info",new DiscoverInfoProvider());
114 
115         // Data Forms
116         pm.addExtensionProvider("x", "jabber:x:data", new DataFormProvider());
117 
118         // MUC User
119         pm.addExtensionProvider("x", "http://jabber.org/protocol/muc#user",new MUCUserProvider());
120 
121         // MUC Admin
122         pm.addIQProvider("query", "http://jabber.org/protocol/muc#admin",new MUCAdminProvider());
123 
124         // MUC Owner
125         pm.addIQProvider("query", "http://jabber.org/protocol/muc#owner",new MUCOwnerProvider());
126 
127         // Delayed Delivery
128         pm.addExtensionProvider("x", "jabber:x:delay",new DelayInformationProvider());
129 
130         // Version
131         try {
132             pm.addIQProvider("query", "jabber:iq:version",Class.forName("org.jivesoftware.smackx.packet.Version"));
133         } catch (ClassNotFoundException e) {
134             // Not sure what‘s happening here.
135         }
136 
137         // VCard
138         pm.addIQProvider("vCard", "vcard-temp", new VCardProvider());
139 
140         // Offline Message Requests
141         pm.addIQProvider("offline", "http://jabber.org/protocol/offline",
142                 new OfflineMessageRequest.Provider());
143 
144         // Offline Message Indicator
145         pm.addExtensionProvider("offline",
146                 "http://jabber.org/protocol/offline",
147                 new OfflineMessageInfo.Provider());
148 
149         // Last Activity
150         pm.addIQProvider("query", "jabber:iq:last", new LastActivity.Provider());
151 
152         // User Search
153         pm.addIQProvider("query", "jabber:iq:search", new UserSearch.Provider());
154 
155         // SharedGroupsInfo
156         pm.addIQProvider("sharedgroup","http://www.jivesoftware.org/protocol/sharedgroup",new SharedGroupsInfo.Provider());
157 
158         // JEP-33: Extended Stanza Addressing
159         pm.addExtensionProvider("addresses","http://jabber.org/protocol/address",new MultipleAddressesProvider());
160 
161         // FileTransfer
162         pm.addIQProvider("si", "http://jabber.org/protocol/si",new StreamInitiationProvider());
163 
164         pm.addIQProvider("query", "http://jabber.org/protocol/bytestreams",new BytestreamsProvider());
165 
166         // Privacy
167         pm.addIQProvider("query", "jabber:iq:privacy", new PrivacyProvider());
168         pm.addIQProvider("command", "http://jabber.org/protocol/commands",new AdHocCommandDataProvider());
169         
170         
171         pm.addExtensionProvider(DeliveryReceipt.ELEMENT, DeliveryReceipt.NAMESPACE, new DeliveryReceipt.Provider()); 
172         pm.addExtensionProvider(DeliveryReceiptRequest.ELEMENT, DeliveryReceipt.NAMESPACE, new DeliveryReceiptRequest.Provider());
173 //       
174 
175         pm.addExtensionProvider("malformed-action","http://jabber.org/protocol/commands",    new AdHocCommandDataProvider.MalformedActionError());
176         pm.addExtensionProvider("bad-locale","http://jabber.org/protocol/commands",new AdHocCommandDataProvider.BadLocaleError());
177         pm.addExtensionProvider("bad-payload","http://jabber.org/protocol/commands",new AdHocCommandDataProvider.BadPayloadError());
178         pm.addExtensionProvider("bad-sessionid","http://jabber.org/protocol/commands",new AdHocCommandDataProvider.BadSessionIDError());
179         pm.addExtensionProvider("session-expired","http://jabber.org/protocol/commands",new AdHocCommandDataProvider.SessionExpiredError());
180     }
181     
182 }

 

XMPP 工具类

标签:

原文地址:http://www.cnblogs.com/mr-zhang/p/4653882.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!