XMPP协议实现即时通讯底层书写(二)--IOSXMPPFrameworkDemo+分
我希望,This is a new day! 在看代码之前,我觉得你还是应该先整理一下心情,来听我说几句: 首先,我希望你是在早上边看这篇blog,然后一边开始动手操作,如果你只是看blog而不去自己对比项目,作用不是很大。一日之计在于晨,所以怀着一颗对技术渴望,激
我希望,This is a new day!
在看代码之前,我觉得你还是应该先整理一下心情,来听我说几句:
首先,我希望你是在早上边看这篇blog,然后一边开始动手操作,如果你只是看blog而不去自己对比项目,作用不是很大。一日之计在于晨,所以怀着一颗对技术渴望,激动的,亢奋的心情去学习,你才能有所得。嗯,就拿鄙人当时做项目来说,每天早上起来的第一件事情,就是研究XMPPFramework作者的代码,按照模块来分析和模仿书写,睡觉的时候还在思考,分析,总结...
当然我并不是说每个Dev 都要向我这样,只是希望你能保持一颗积极向上的心态去对待技术,对待你的工作。
that's all。
ResourceURL:https://github.com/robbiehanson/XMPPFramework (如果你还在维护你现有的基于XMPP的产品,那么你需要sometimes 去查看,原作者是否fix 一些bug)
IphoneXMPP Demo
1.AppDelegate.m
a.大概看下头文件,ok,别跳转深入看了,下面我会教快速的看。See this method
有几个地方需要注意:
1)DDLog 用于不用不强求,鄙人喜欢干净清爽的控制台,所以就没用这玩意,因为我并不是很依赖全部打log,而是断点控制台po XXX方法,实时性找出问题修复bug
2)配置XML Stream 流 ,给你的长连接里面增加各种 buff,各种装备,各种属性。ok,不开玩笑了:),这个配置很重要,它决定了你的app需要支持哪些xmpp服务,决定了原作者(罗宾逊)哪些代码功能模块是不需要生效的
3)启动连接,当然对应的也有一个cancel connect
b 设置你的 XML Stream ,开启哪些功能
- (void)setupStream
{
NSAssert(xmppStream == nil, @"Method setupStream invoked multiple times");
// Setup xmpp stream
//
// The XMPPStream is the base class for all activity.
// Everything else plugs into the xmppStream, such as modules/extensions and delegates.
xmppStream = [[XMPPStream alloc] init];
#if !TARGET_IPHONE_SIMULATOR
{
// Want xmpp to run in the background?
//
// P.S. - The simulator doesn't support backgrounding yet.
// When you try to set the associated property on the simulator, it simply fails.
// And when you background an app on the simulator,
// it just queues network traffic til the app is foregrounded again.
// We are patiently waiting for a fix from Apple.
// If you do enableBackgroundingOnSocket on the simulator,
// you will simply see an error message from the xmpp stack when it fails to set the property.
xmppStream.enableBackgroundingOnSocket = YES;
}
#endif
// Setup reconnect
//
// The XMPPReconnect module monitors for "accidental disconnections" and
// automatically reconnects the stream for you.
// There's a bunch more information in the XMPPReconnect header file.
xmppReconnect = [[XMPPReconnect alloc] init];
// Setup roster
//
// The XMPPRoster handles the xmpp protocol stuff related to the roster.
// The storage for the roster is abstracted.
// So you can use any storage mechanism you want.
// You can store it all in memory, or use core data and store it on disk, or use core data with an in-memory store,
// or setup your own using raw SQLite, or create your own storage mechanism.
// You can do it however you like! It's your application.
// But you do need to provide the roster with some storage facility.
xmppRosterStorage = [[XMPPRosterCoreDataStorage alloc] init];
// xmppRosterStorage = [[XMPPRosterCoreDataStorage alloc] initWithInMemoryStore];
xmppRoster = [[XMPPRoster alloc] initWithRosterStorage:xmppRosterStorage];
xmppRoster.autoFetchRoster = YES;
xmppRoster.autoAcceptKnownPresenceSubscriptionRequests = YES;
// Setup vCard support
//
// The vCard Avatar module works in conjuction with the standard vCard Temp module to download user avatars.
// The XMPPRoster will automatically integrate with XMPPvCardAvatarModule to cache roster photos in the roster.
xmppvCardStorage = [XMPPvCardCoreDataStorage sharedInstance];
xmppvCardTempModule = [[XMPPvCardTempModule alloc] initWithvCardStorage:xmppvCardStorage];
xmppvCardAvatarModule = [[XMPPvCardAvatarModule alloc] initWithvCardTempModule:xmppvCardTempModule];