iPhone平台下基于XMPP的IM研究
iPhone平台下基于XMPP的IM研究
看了下iphone平台下xmpp的使用。XmppFramework 是一个开源项目,使用Objective-C实现了XMPP协议,它和前面所说的smack使用起来一样的方便,不过官网上提供的资料远不及smack。
源码地址:http://code.google.com/p/xmppframework/,目前需要使用git才能download到源码,。
PC客户端使用Spark,不知是否是我的黑苹果原因,spark装上不能运行(郁闷中...)
服务器使用Openfire
数据库我使用还是MySQL
怎样将XMPPFramework添加到我们自己的项目中,请参考。
代码步骤:
1、初始化XMPPStream
xmppStream = [[XMPPStream alloc] init];
xmppStream.hostName = @"127.0.0.1";
xmppStream.hostPort = 5222;
[xmppStreamaddDelegate:selfdelegateQueue:dispatch_get_main_queue()];
XmppFramework的消息监听方式使用delegate。在smack中我们使用的是listener,其实都一样。
2、设置JID;(注意JID的Domain一定要用主机名,不要用IP地址。我的疏忽让我晚上熬到了3点多)
xmppStream.myJID = [XMPPJID jidWithString:[NSString stringWithFormat:@"%@@liu-lavymatoMacBook-Pro.local",myJID]];
3、连接服务器
NSError *error = nil;
[xmppStream connect:&error];
接下来就是一系列依次调用delegate的方法
xmppStreamWillConnect
socketDidConnect
xmppStreamDidConnect 在这个方法中我们需要调用: [xmppStreamauthenticateWithPassword:myPassworderror:&error]
验证成功:xmppStreamDidAuthenticate:
验证失败:xmppStream: didNotAuthenticate:
- //
- // XmppTest1AppDelegate.h
- // XmppTest1
- //
- // Created by liu lavy on 11-10-2.
- // Copyright 2011 __MyCompanyName__. All rights reserved.
- //
- #import <UIKit/UIKit.h>
- #import "XMPPFramework.h"
- @class XmppTest1ViewController;
- @interface XmppTest1AppDelegate : NSObject <UIApplicationDelegate, XMPPRosterDelegate> {
- UIWindow *window;
- XmppTest1ViewController *viewController;
- XMPPStream *xmppStream;
- XMPPReconnect *xmppReconnect;
- NSString *myPassword;
- }
- @property (nonatomic, retain) IBOutlet UIWindow *window;
- @property (nonatomic, retain) IBOutlet XmppTest1ViewController *viewController;
- @property (nonatomic, retain) XMPPStream *xmppStream;
- @property (nonatomic, readonly) XMPPReconnect *xmppReconnect;
- @property (nonatomic, retain) NSString *myPassword;
- -(BOOL) connect:(NSString *)myJID password:(NSString *)myPassword;
- -(BOOL) authenticate;
- -(void) disConnect;
- @end
|
评论暂时关闭