首页 > 代码库 > XMPP学习--建立管理类

XMPP学习--建立管理类

参考了别人的demo之后发现建立一个管理类看起来舒服些,显得比较有条理……

但在建立与服务器连接当中,发现

Connect Error: {

    NSLocalizedDescription = "You must set myJID before calling connect.";

}

这样的一个问题,知道是jid没有设置好,但是jid怎么设置呢?今天仍然没有弄清,如果有清楚的可以交流一下。


1.将管理类写成单例

staticXmppManager *shareManager = Nil;

+ (XmppManager *)shareInstance

{

   static dispatch_once_t onceToken;

   dispatch_once(&onceToken, ^{

        shareManager = [[XmppManageralloc] init];

        [shareManagersetupXMPPStream];

    });

    

    returnshareManager;

}


2.建立XML流,确定代理方法

- (void)setupXMPPStream

{

    xmppStream = [[XMPPStreamalloc] init];

    

#if !TARGET_OS_IPHONE

   //非模拟器时可进入后台工作

    xmppStream.enableBackgroundingOnSocket =YES;

#endif

   //代理

    [xmppStreamaddDelegate:selfdelegateQueue:dispatch_get_main_queue()];

    

    [xmppStreamsetHostName:kXMPPHost];

    [xmppStreamsetHostPort:5222];

    

    //重新连接

    reconnect = [[XMPPReconnectalloc] init];

    

    //花名册

    XMPPRosterCoreDataStorage *rosterStorage = [[XMPPRosterCoreDataStoragealloc] initWithInMemoryStore];

   roster = [[XMPPRosteralloc] initWithRosterStorage:rosterStorage];

    [rosteraddDelegate:selfdelegateQueue:dispatch_get_main_queue()];

    [reconnectactivate:xmppStream];

    [rosteractivate:xmppStream];

    

    allowSSLHostNameMismatch =YES;

    allowSelfSignedCertificates =YES;

}


3.在dealloc消除XML流

- (void)dealloc

{

    [selfsetoutXMPPStream];

}


- (void)setoutXMPPStream

{

    [xmppStreamremoveDelegate:self];

    [reconnectdeactivate];

    [rosterdeactivate];

    [xmppStreamdisconnect];

    xmppStream =Nil;

    reconnect =Nil;

   roster = Nil;

}


4.写用户的各种状态:上线,离线,离开,勿扰…..

#pragma mark - 用户状态

/*

 presence 的状态:

 available 上线

 away 离开

 do not disturb 忙碌

 unavailable 下线

 */


- (void)goonline

{

    XMPPPresence *presence = [XMPPPresencepresenceWithType:@"available"];

    [xmppStreamsendElement:presence];

}


- (void)gooffline

{

    XMPPPresence *presence = [XMPPPresencepresenceWithType:@"unavailable"];

    [xmppStreamsendElement:presence];

}


- (void)away

{

    XMPPPresence *presence = [XMPPPresencepresenceWithType:@"away"];

    [xmppStreamsendElement:presence];

}


- (void)busy

{

    XMPPPresence *presence = [XMPPPresencepresenceWithType:@"do not disturb"];

    [xmppStreamsendElement:presence];

}


5.写用户的各种操作:添加好友,删除好友,发送消息

#pragma mark - 用户操作

//增加成员

- (void)addSomeBody:(NSString *)userId

{

    [rostersubscribePresenceToUser:[XMPPJIDjidWithString:[NSStringstringWithFormat:@"%@@qq.com",userId]]];

}


//删除好友

- (void)deleteSomeBody:(NSString *)userId

{

    [rosterunsubscribePresenceFromUser:[XMPPJIDjidWithString:[NSStringstringWithFormat:@"%@@qq.com",userId]]];

}


//发送消息

- (void)sendMessage:(NSString *)message toUser:(NSString *)user

{

    NSXMLElement *body = [NSXMLElementelementWithName:@"body"];

    [bodysetStringValue:message];

    NSXMLElement *message_ = [NSXMLElementelementWithName:@"message"];

    [message_ addAttributeWithName:@"type"stringValue:@"chat"];

   NSString *to = [NSStringstringWithFormat:@"%@@qq.com", user];

    [message_ addAttributeWithName:@"to"stringValue:to];

    [message_addChild:body];

    [xmppStreamsendElement:message_];

}


6.还有系统操作

#pragma mark - 系统操作

- (BOOL)connect

{

    if (![xmppStreamisDisconnected]) {

       return YES;

    }

    NSString *user = [[NSUserDefaultsstandardUserDefaults] stringForKey:kXMPPmyJID];

    NSString *password = [[NSUserDefaultsstandardUserDefaults] stringForKey:kXMPPmyPassword];

    

   XMPPJID *jid = [XMPPJIDjidWithUser:user domain:@""resource:@""];

    [xmppStreamsetMyJID:jid];

   passWord = password;

    

    returnYES;

    

}


- (void)disconnect

{

    [selfgooffline];

    [xmppStreamdisconnect];

}

XMPP学习--建立管理类