IM项目,使用openfire服务,Smack的android端概述

目前正在做一个IM(即时通讯)项目,所以就拿出来和大家分享一下,自己也做个记录。

下面就介绍一下:

Java领域的即时通信的解决方案可以考虑openfire+spark+smack。当然也有其他的选择。

首先需要在电脑上安装openfire,这就是服务器。

android端开发的话,可以使用asmack,这个有着强大的API的提供。

1.先定义一个服务器连接工具类:

/**
 * 
 * XMPP服务器连接工具类.
 * 
 */
public class XmppConnectionManager {
	private XMPPConnection connection;
	private static XmppConnectionManager xmppConnectionManager;

	private XmppConnectionManager() {

	}

	public static XmppConnectionManager getInstance() {
		if (xmppConnectionManager == null) {
			xmppConnectionManager = new XmppConnectionManager();
		}
		return xmppConnectionManager;
	}

	// init
		public XMPPConnection init() {
			Connection.DEBUG_ENABLED = false;
			ProviderManager pm = ProviderManager.getInstance();
			configure(pm);

			connectionConfig = new ConnectionConfiguration(
					"192.168.1.11", 5222,
					"localhost");<span style="white-space:pre">			</span>//三个参数分别为服务器地址,端口号,服务器名称
			connectionConfig.setSASLAuthenticationEnabled(false);// 不使用SASL验证,设置为false
			connectionConfig
					.setSecurityMode(ConnectionConfiguration.SecurityMode.enabled);
			// 允许自动连接
			connectionConfig.setReconnectionAllowed(false);
			// 允许登陆成功后更新在线状态
			connectionConfig.setSendPresence(false);
			// 收到好友邀请后manual表示需要经过同意,accept_all表示不经同意自动为好友
			Roster.setDefaultSubscriptionMode(Roster.SubscriptionMode.manual);
			connection = new XMPPConnection(connectionConfig);
			return connection;
		}

		/**
		 * 
		 * 返回一个有效的xmpp连接,如果无效则返回空.
		 */
		public XMPPConnection getConnection() {
			if (connection == null) {
				Connection.DEBUG_ENABLED = false;
				ProviderManager pm = ProviderManager.getInstance();
				configure(pm);

				connectionConfig = new ConnectionConfiguration(
					"172.17.10.37", 5222,
						"localhost");
				connectionConfig.setSASLAuthenticationEnabled(false);// 不使用SASL验证,设置为false
				connectionConfig
						.setSecurityMode(ConnectionConfiguration.SecurityMode.enabled);
				// 允许自动连接
				connectionConfig.setReconnectionAllowed(false);
				// 允许登陆成功后更新在线状态
				connectionConfig.setSendPresence(true);
				// 收到好友邀请后manual表示需要经过同意,accept_all表示不经同意自动为好友
				Roster.setDefaultSubscriptionMode(Roster.SubscriptionMode.manual);
				connection = new XMPPConnection(connectionConfig);
				return connection;
			}
			return connection;
		}

		/**
		 * 
		 * 销毁xmpp连接.
		 */
		public void disconnect() {
			if (connection != null) {
				connection.disconnect();
			}
		}

		public void configure(ProviderManager pm) {

			// Private Data Storage
			pm.addIQProvider("query", "jabber:iq:private",
					new PrivateDataManager.PrivateDataIQProvider());

			// Time
			try {
				pm.addIQProvider("query", "jabber:iq:time",
						Class.forName("org.jivesoftware.smackx.packet.Time"));
			} catch (ClassNotFoundException e) {
			}

			// XHTML
			pm.addExtensionProvider("html", "http://jabber.org/protocol/xhtml-im",
					new XHTMLExtensionProvider());

			// Roster Exchange
			pm.addExtensionProvider("x", "jabber:x:roster",
					new RosterExchangeProvider());
			// Message Events
			pm.addExtensionProvider("x", "jabber:x:event",
					new MessageEventProvider());
			// Chat State
			pm.addExtensionProvider("active",
					"http://jabber.org/protocol/chatstates",
					new ChatStateExtension.Provider());
			pm.addExtensionProvider("composing",
					"http://jabber.org/protocol/chatstates",
					new ChatStateExtension.Provider());
			pm.addExtensionProvider("paused",
					"http://jabber.org/protocol/chatstates",
					new ChatStateExtension.Provider());
			pm.addExtensionProvider("inactive",
					"http://jabber.org/protocol/chatstates",
					new ChatStateExtension.Provider());
			pm.addExtensionProvider("gone",
					"http://jabber.org/protocol/chatstates",
					new ChatStateExtension.Provider());

			// FileTransfer
			pm.addIQProvider("si", "http://jabber.org/protocol/si",
					new StreamInitiationProvider());

			// Group Chat Invitations
			pm.addExtensionProvider("x", "jabber:x:conference",
					new GroupChatInvitation.Provider());
			// Service Discovery # Items
			pm.addIQProvider("query", "http://jabber.org/protocol/disco#items",
					new DiscoverItemsProvider());
			// Service Discovery # Info
			pm.addIQProvider("query", "http://jabber.org/protocol/disco#info",
					new DiscoverInfoProvider());
			// Data Forms
			pm.addExtensionProvider("x", "jabber:x:data", new DataFormProvider());
			// MUC User
			pm.addExtensionProvider("x", "http://jabber.org/protocol/muc#user",
					new MUCUserProvider());
			// MUC Admin
			pm.addIQProvider("query", "http://jabber.org/protocol/muc#admin",
					new MUCAdminProvider());
			// MUC Owner
			pm.addIQProvider("query", "http://jabber.org/protocol/muc#owner",
					new MUCOwnerProvider());
			// Delayed Delivery
			pm.addExtensionProvider("x", "jabber:x:delay",
					new DelayInformationProvider());
			// Version
			try {
				pm.addIQProvider("query", "jabber:iq:version",
						Class.forName("org.jivesoftware.smackx.packet.Version"));
			} catch (ClassNotFoundException e) {
			}
			// VCard
			pm.addIQProvider("vCard", "vcard-temp", new VCardProvider());
			// Offline Message Requests
			pm.addIQProvider("offline", "http://jabber.org/protocol/offline",
					new OfflineMessageRequest.Provider());
			// Offline Message Indicator
			pm.addExtensionProvider("offline",
					"http://jabber.org/protocol/offline",
					new OfflineMessageInfo.Provider());
			// Last Activity
			pm.addIQProvider("query", "jabber:iq:last", new LastActivity.Provider());
			// User Search
			pm.addIQProvider("query", "jabber:iq:search", new UserSearch.Provider());
			// SharedGroupsInfo
			pm.addIQProvider("sharedgroup",
					"http://www.jivesoftware.org/protocol/sharedgroup",
					new SharedGroupsInfo.Provider());
			// JEP-33: Extended Stanza Addressing
			pm.addExtensionProvider("addresses",
					"http://jabber.org/protocol/address",
					new MultipleAddressesProvider());

		}
	}
所有的登陆,注册,发消息,发文件,接消息等等所有的操作都需要线连接服务器,所以必须有一个XMPPConnection对象,从上面的服务器工具类中可以获得这个对象

有了这,其他的才得以实现,说了这么多,一句话,这个必须的。

下一篇就开始讲一下登陆,注册等操作。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值