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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • C++进制转换模板

    C++进制转换模板do{ num[i++]=n%radix; n/=radix;}while(n!=0);

    2022年7月19日
    18
  • 深度学习之卷积神经网络(Convolutional Neural Networks, CNN)

    深度学习之卷积神经网络(Convolutional Neural Networks, CNN)前面,我们介绍了DNN及其参数求解的方法(BP算法),我们知道了DNN仍然存在很多的问题,其中最主要的就是BP求解可能造成的梯度消失和梯度爆炸的问题.那么,人们又是怎么解决这个问题的呢?本节的卷积神经网络(ConvolutionalNeuralNetworks,CNN)就是一种解决方法.我们知道神经网络主要有三个部分组成,分别为:网络结构—描述神经元的层次与连接神经元的结构. 激活函数(激励函数)—用于加入非线性的因素,解决线性模型所不能解决的问题. 参数学习方…

    2025年9月1日
    5
  • tomcat 热加载[通俗易懂]

    tomcat 热加载[通俗易懂]tomcat 热加载

    2022年4月24日
    36
  • AJAX常见面试问题[通俗易懂]

    AJAX常见面试问题[通俗易懂]1.工作当中会和后台交互吗? 那你能说说封装好的ajax里的几个参数吗 ?url: 发送请求的地址。type:请求方式(post或get)默认为get。async:同步异步请求,默认true所有请求均为异步请求。timeout:超时时间设置,单位毫秒data:要求为Object或String类型的参数,发送到服务器的数据cache:默认为true(当dataType为script时,…

    2022年8月25日
    5
  • 20213D激活码_通用破解码[通俗易懂]

    20213D激活码_通用破解码,https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月17日
    204
  • mysql报错1396_mysql连接不上数据库

    mysql报错1396_mysql连接不上数据库我似乎无法重新创建一个已删除的简单用户,即使以root用户身份在MySQL中也是如此。我的情况是:用户’jack’曾经存在,但是我从mysql.user中删除了它以重新创建它。我在那张桌子上看不到任何痕迹。如果我对其他随机用户名(例如“jimmy”)执行此命令,则该命令会正常工作(就像最初对“jack”所做的一样)。我已经做了些什么来破坏用户“jack”,以及如何撤销该破坏,以便重新创建“…

    2022年8月12日
    5

发表回复

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

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