XMPP得知--建立一个管理类

XMPP得知--建立一个管理类

大家好,又见面了,我是全栈君,今天给大家准备了Idea注册码。

参考其他demo之后,设立一个管理类的发现看起来更舒服,理……

但在建立与server连接其中。发现

Connect Error: {

    NSLocalizedDescription = “You must set myJID before calling connect.”;

}

这种一个问题。知道是jid没有设置好,可是jid怎么设置呢?今天仍然没有弄清。假设有清楚的能够交流一下。


1.将管理类写成单例

static XmppManager *shareManager = Nil;

+ (XmppManager *)shareInstance

{

    static dispatch_once_t onceToken;

    dispatch_once(&onceToken, ^{

        shareManager = [[XmppManager alloc] init];

        [shareManager setupXMPPStream];

    });

    

    return shareManager;

}

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

– (void)setupXMPPStream

{

    xmppStream = [[XMPPStream alloc] init];

    

#if !TARGET_OS_IPHONE

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

    xmppStream.enableBackgroundingOnSocket = YES;

#endif

    //代理

    [xmppStream addDelegate:self delegateQueue:dispatch_get_main_queue()];

    

    [xmppStream setHostName:kXMPPHost];

    [xmppStream setHostPort:5222];

    

    //又一次连接

    reconnect = [[XMPPReconnect alloc] init];

    

    //花名冊

    XMPPRosterCoreDataStorage *rosterStorage = [[XMPPRosterCoreDataStorage alloc] initWithInMemoryStore];

    roster = [[XMPPRoster alloc] initWithRosterStorage:rosterStorage];

    [roster addDelegate:self delegateQueue:dispatch_get_main_queue()];

    [reconnect activate:xmppStream];

    [roster activate:xmppStream];

    

    allowSSLHostNameMismatch = YES;

    allowSelfSignedCertificates = YES;

}

3.在dealloc消除XML流

– (void)dealloc

{

    [self setoutXMPPStream];

}


– (void)setoutXMPPStream

{

    [xmppStream removeDelegate:self];

    [reconnect deactivate];

    [roster deactivate];

    [xmppStream disconnect];

    xmppStream = Nil;

    reconnect = Nil;

    roster = Nil;

}


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

#pragma mark – 用户状态

/*

 presence 的状态:

 available 上线

 away 离开

 do not disturb 忙碌

 unavailable 下线

 */


– (void)goonline

{

    XMPPPresence *presence = [XMPPPresence presenceWithType:@”available”];

    [xmppStream sendElement:presence];

}


– (void)gooffline

{

    XMPPPresence *presence = [XMPPPresence presenceWithType:@”unavailable”];

    [xmppStream sendElement:presence];

}


– (void)away

{

    XMPPPresence *presence = [XMPPPresence presenceWithType:@”away”];

    [xmppStream sendElement:presence];

}


– (void)busy

{

    XMPPPresence *presence = [XMPPPresence presenceWithType:@”do not disturb”];

    [xmppStream sendElement:presence];

}


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

#pragma mark – 用户操作

//添加成员

– (void)addSomeBody:(NSString *)userId

{

    [roster subscribePresenceToUser:[XMPPJID jidWithString:[NSString stringWithFormat:@”%@@qq.com”,userId]]];

}


//删除好友

– (void)deleteSomeBody:(NSString *)userId

{

    [roster unsubscribePresenceFromUser:[XMPPJID jidWithString:[NSString stringWithFormat:@”%@@qq.com”,userId]]];

}


//发送消息

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

{

    NSXMLElement *body = [NSXMLElement elementWithName:@”body”];

    [body setStringValue:message];

    NSXMLElement *message_ = [NSXMLElement elementWithName:@”message”];

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

    NSString *to = [NSString stringWithFormat:@”%@@qq.com”, user];

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

    [message_ addChild:body];

    [xmppStream sendElement:message_];

}


6.还有系统操作

#pragma mark – 系统操作

– (BOOL)connect

{

    if (![xmppStream isDisconnected]) {

        return YES;

    }

    NSString *user = [[NSUserDefaults standardUserDefaults] stringForKey:kXMPPmyJID];

    NSString *password = [[NSUserDefaults standardUserDefaults] stringForKey:kXMPPmyPassword];

    

    XMPPJID *jid = [XMPPJID jidWithUser:user domain:@”” resource:@””];

    [xmppStream setMyJID:jid];

    passWord = password;

    

    return YES;

    

}


– (void)disconnect

{

    [self gooffline];

    [xmppStream disconnect];

}

版权声明:本文博主原创文章。博客,未经同意不得转载。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/116879.html原文链接:https://javaforall.net

(0)
上一篇 2022年1月13日 下午10:00
下一篇 2022年1月13日 下午10:00


相关推荐

  • “UEFI虚拟机”以及“EFI SHELL”介绍「建议收藏」

    “UEFI虚拟机”以及“EFI SHELL”介绍「建议收藏」含UEFIBIOS的电脑并不是所有网友都有,为了让大家都能体验“UEFI”,现在将VMware(8.0以上的版本)打造成“UEFI虚拟机”,具体方法去年提供过,现在为了本系列教程的完整性,重新提供:新建虚拟机后,在虚拟机目录下找到后缀为vmx的文件,使用记事本打开后,添加一行文字(红色):—————–firmware=”efi”———-…

    2022年7月19日
    22
  • 成员变量和局部变量的区别

    成员变量和局部变量的区别成员变量和局部变量的区别 nbsp nbsp nbsp 1 在类中的位置不同 nbsp nbsp nbsp nbsp nbsp nbsp 成员变量 类中方法外 nbsp nbsp nbsp nbsp nbsp nbsp 局部变量 方法定义中或者方法声明上 nbsp nbsp nbsp 2 在内存中的位置不同 nbsp nbsp nbsp nbsp nbsp nbsp 成员变量 在堆中 nbsp nbsp nbsp nbsp nbsp nbsp 局部变量 在栈中 nbsp nbsp nbsp 3 生命周期不同 nbsp nbsp nbsp nbsp nbsp nbsp 成员变量 随着对象的创建而存在 随着对象的消失而消失 nbsp nbsp nbsp nbsp nbsp nbsp 局部变量 随着方法的调用而存在 随着

    2026年3月20日
    2
  • QXDM日志分析工具「建议收藏」

    QXDM日志分析工具「建议收藏」testhttp://pan.baidu.com/s/1gfOCb8f

    2026年4月15日
    10
  • python读取pkl文件_pkl文件是怎么训练出来的

    python读取pkl文件_pkl文件是怎么训练出来的importosimportpickledefread_pickle(work_path):data_list=[]withopen(work_path,”rb”)asf:whileTrue:try:data=pickle.load(f)data_list.append(data)

    2025年10月16日
    5
  • 永恒之蓝病毒端口_openwrt安全性

    永恒之蓝病毒端口_openwrt安全性1.使用shadowbroker的eternalblue还有doublepulsar实现注入到目标机器的进程,然后使用kali下的msf获取到有病毒机器的shell然后修改密码查看问题等.根据网上的教程来进行处理以及复现等教程地址:http://blog.csdn.net/claygrit/article/details/77284739http://blo…

    2022年10月16日
    3
  • Storm On YARN带来的好处

    Storm On YARN带来的好处

    2021年12月17日
    69

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号