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


相关推荐

  • 无线信道特征_无线信道模型有哪几种

    无线信道特征_无线信道模型有哪几种目录1统计性模型(经验模型)1.1模型分类(1)按衰落分类(2)按路径损耗和延迟拓展分类1.2建模方法(1)方法一分类(2)方法二分类2确定模型2.1建模方法3半确定模型3.1建模方法4思维导图PDF、xmind格式下载5参考文献1统计性模型(经验模型)1.1模型分类(1)按衰落分类小尺度传播衰落模型描述小范围内信号的幅度和相位的快速衰落,在电波传播过程中,信号场强在短短(几个信号波长)或短时(秒级)上呈现出快速波动的状况,包括由移动台和基站相对运动造成多普勒频移引起的时间

    2022年9月23日
    0
  • js,timeout,promise执行顺序

    js,timeout,promise执行顺序

    2022年3月13日
    75
  • @RestController的作用「建议收藏」

    @RestController的作用「建议收藏」原文:文章收藏于IT老兵博客。正文理解一下@RestControlle的作用。ThiscodeusesSpring4’snew @RestController annotation,whichmarkstheclassasacontrollerwhereeverymethodreturnsadomainobjectinsteadofa…

    2022年6月17日
    39
  • 数据库 casewhen 的用法「建议收藏」

    数据库 casewhen 的用法「建议收藏」select[Id],[TrainNumber],[SupplierId],casewarehouseTypewhen0then[Amount]else[Amount]*-1endasIsOut//数据对比,[Amount],[ClassId],[WarehouseType],[Remark],[SetInDate]fromWWeiqinWarehousing

    2022年9月6日
    5
  • webide配置_web vscode

    webide配置_web vscode背景为了解决函数计算本地环境差异和配置繁琐的问题,在此背景下,就有了我们的WebIDE产品,WebIDE能让函数的开发、测试和部署更加流畅,降低了函数计算的学习成本和缩短了函数的开发周期。WebIDE入口地址:https://ide.fc.aliyun.com介绍WebIDE是一个基于H5支持多语言的集成开发环境。相当于VSCo…

    2022年10月18日
    0
  • MyBatisCodeHelperPro 2.9.3 激活_最新在线免费激活

    (MyBatisCodeHelperPro 2.9.3 激活)JetBrains旗下有多款编译器工具(如:IntelliJ、WebStorm、PyCharm等)在各编程领域几乎都占据了垄断地位。建立在开源IntelliJ平台之上,过去15年以来,JetBrains一直在不断发展和完善这个平台。这个平台可以针对您的开发工作流进行微调并且能够提供…

    2022年3月31日
    450

发表回复

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

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