前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >XMPP得知--建立一个管理类

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

作者头像
全栈程序员站长
发布2022-01-14 16:14:57
3920
发布2022-01-14 16:14:57
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是全栈君

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

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

代码语言:javascript
复制
Connect Error: {
    NSLocalizedDescription = “You must set myJID before calling connect.”;
}

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

1.将管理类写成单例

代码语言:javascript
复制
static XmppManager *shareManager =  Nil;
+ (XmppManager *)shareInstance
{
 static dispatch_once_t onceToken;
 dispatch_once(&onceToken, ^{
  shareManager = [[XmppManager alloc]  init];
         [shareManager setupXMPPStream];
    });
  return shareManager;
}

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

代码语言:javascript
复制
– (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流

代码语言:javascript
复制
– (void)dealloc
{
     [self setoutXMPPStream];
}
– (void)setoutXMPPStream
{
     [xmppStream removeDelegate:self];
     [reconnect deactivate];
     [roster deactivate];
     [xmppStream disconnect];
  xmppStream = Nil;
  reconnect = Nil;
 roster = Nil;
}

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

代码语言:javascript
复制
 #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.写用户的各种操作:加入好友,删除好友,发送消息

代码语言:javascript
复制
 #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.还有系统操作

代码语言:javascript
复制
 #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.cn/116879.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档