大家好,又见面了,我是全栈君
参考其他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.cn/116879.html原文链接:https://javaforall.cn