标签:share this help b2c 参数 delay str cli xmpp
https://www.jianshu.com/p/84d15683b61e
https://www.cnblogs.com/lurenq/p/7026983.html
XMPP 是一组基于 XML 的技术。用于实时应用程序。最初。XMPP 作为一个框架开发。目标是支持企业环境内的即时消息传递和联机状态应用程序。详细简介
iOS端:
Openfire + Spark 简介:下载地址及简单介绍:
openfire + Spark这边服务器端了解不多,只是知道通过下载这个应用,通过配置来创建好友和创建房间,进行简单的单聊群聊。如果做服务器的话,肯定不止这么少东西,如果要学习的话还是自己查资料吧。 Openfire下载地址
xmpp是基于xml类型进行消息传递的。所有的消息传递类型都是xmpp类型的。
(1) 发送消息类型:
<message type="chat" to="110095@domain" id="d851f47818db46b58abf4e982327ab36"><request xmlns="urn:xmpp:receipts"></request><body>{
"messageId" : "d851f47818db46b58abf4e982327ab36", //messageid
"content" : "你好", //消息内容
"fromUserName" : "张三", //发送人
"isSecret" : 0, //单聊是否是密聊消息 0:正常聊天 1:密聊
"timeLen" : 0,
"isAt" : 0, //群聊是否@人
"timeSend" : 1522034423, //发送时间
"type" : 1 //消息类型
}</body></message>
(2) 接收消息类型:
<message xmlns="jabber:client" id="cdc6007ca03b401f8c9e4a2ccb75d8d5" type="chat" to="111751@domain" from="109965@domain/resource"><request xmlns="urn:xmpp:receipts"></request><body>{
"messageId" : "cdc6007ca03b401f8c9e4a2ccb75d8d5",
"content" : "天津",
"fromUserName" : "李四",
"isSecret" : 0,
"timeLen" : 0,
"isAt" : 0,
"timeSend" : 1522034878,
"type" : 1
}</body></message>
(3)发送回执消息类型:
<message to="109965@domain/resource" type="chat"><received xmlns="urn:xmpp:receipts" id="cdc6007ca03b401f8c9e4a2ccb75d8d5"/></message>
(4)接收回执消息类型:
<message xmlns="jabber:client" type="chat" to="111751@domain/resource" from="109965@domain/resource"><received xmlns="urn:xmpp:receipts" id="626b61f461eb485fa99177b69512273e"></received></message>
流程:客户端A给客户端B发送一条消息,消息先从客户端A传到服务器,服务器通过判断客户端B是否在线,如果在线的就直接把消息推送给客户端B,客户端B收到消息以后,则发送后回执,服务器收到回执以后再推给客户端A。
如果客户端B不在线,则消息在服务器端保存,服务器发送回回执告诉客户端A发送消息成功,等客户端B上线以后再把消息推送给客户端B。
问题1: 当客户端A给B推送消息时,客户端B正好此时进入后台。由于后台心跳检测还没有检测到B已经离线,这个时候服务器会把消息推送给客户端B,并且服务器没有保存这条消息。而此时客户端B已经进入后台,无法接收到消息,也就无法发送回执。会造成客户端A发送消息失败。
解决方法:服务器每次收到客户端A都会发送的消息,都会由服务器发送回执告诉A已经发送成功。客户端首先保存聊天信息,再客户端B是否在线。如果在线,则发送消息,接受回执,如果没有收到回执,则当成不在线,保存聊天消息为未发送状态,下次等客户端B连线后推送。
封装的xmpp类图
(1) XMPPStream 类主要是设置主机名(HostName), 端口号(port), 链接xmpp服务器,及获取其delegate,来获取登录状态及输入xmppPassword来进行登录
(2)XMPPReconnect来设置重新连接的时间,及激活xmppStream
(3) xmppRoster 获取好友花名册
(4)xmppRosterStorage 来通过xmpp自带的coreData数据库保存用户数据
(5)roomPool是保存用户进入房间的池子,可以通过池子中的所有房间,获取群聊房间的离线消息和退出登录时,退出所有群聊房间。
APPDelegate中调用xmpp的流程
- applicationWillEnterForeground方法中[[NTXmpp sharedInstance] login];
- applicationDidEnterBackground方法中[[NTXmpp sharedInstance] disconnect];
- 登录成功以后[[NTXmpp sharedInstance] performSelector:@selector(login) withObject:nil afterDelay:0.5];//0.5秒后执行xmpp登录
XMPPHelper方法代码
+ (NTXmpp*)sharedInstance
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedManager = [[NTXmpp alloc] init];
[sharedManager setupStream];
});
return sharedManager;
}
//开始配置xmppstream
#pragma mark--------配置XML流---------
- (void)setupStream
{
NSAssert(xmppStream == nil, @"Method setupStream invoked multiple times");
xmppStream = [[XMPPStream alloc] init];
#if !TARGET_IPHONE_SIMULATOR
{
xmppStream.enableBackgroundingOnSocket = YES;
}
#endif
xmppReconnect = [[XMPPReconnect alloc] init];
xmppReconnect.reconnectTimerInterval = 10;
//xmppRosterStorage = [[XMPPRosterCoreDataStorage alloc] init];
//xmppRoster = [[XMPPRoster alloc] initWithRosterStorage:xmppRosterStorage];
//xmppRoster.autoFetchRoster = YES;
//xmppRoster.autoAcceptKnownPresenceSubscriptionRequests = YES;
[xmppReconnect activate:xmppStream];
//[xmppRoster activate:xmppStream];
// Add ourself as a delegate to anything we may be interested in
[xmppStream addDelegate:self delegateQueue:dispatch_get_main_queue()];
[xmppReconnect addDelegate:self delegateQueue:dispatch_get_main_queue()];
[xmppStream setHostName:g_config.XMPPHost];
[xmppStream setHostPort:5222];
// You may need to alter these settings depending on the server you‘re connecting to
//allowSelfSignedCertificates = NO;
//allowSSLHostNameMismatch = NO;
self.roomPool = [[NTRoomPool alloc] init];
}
//xmpp登陆
- (void)login
{
if (isLogined == login_status_yes)
return;
if (![self connect]) {
[HUDNotificationCenter showMessage:@"服务连接失败" hideAfter:2.0f];
};
}
- (BOOL)connect
{
if (![xmppStream isDisconnected])
return YES;
NSString* myJID = g_loginUser.userId;
//NSString *myPassword = [[NSUserDefaults standardUserDefaults] stringForKey:kMY_USER_PASSWORD]; 112003
NSString* myPassword = g_loginUser.xmppPassword;
if (myJID == nil || myPassword == nil) {
return NO;
}
self.isLogined = login_status_ing;
[self notify];
[xmppStream setMyJID:[XMPPJID jidWithString:[NSString stringWithFormat:@"%@@%@/%@", myJID, g_config.XMPPDomain, XMPP_RESOURCE]]];
password = myPassword;
NSError* error = nil;
if (![xmppStream connectWithTimeout:10 error:&error]) {
self.isLogined = login_status_no;
[self notify];
return NO;
}
return YES;
}
- (void)disconnect
{
[self goOffline];
[xmppStream disconnect];
}
#xmppStreamDelegate
- (void)xmppStreamDidConnect:(XMPPStream*)sender
{
NTLog(@"xmpp connect success");
isXmppConnected = YES;
NSError* error = nil;
if (![xmppStream authenticateWithPassword:password error:&error]) {
self.isLogined = login_status_no;
[self notify];
//DDLogError(@"Error authenticating: %@", error);
}
}
- (void)xmppStreamDidDisconnect:(XMPPStream*)sender withError:(NSError*)error
{
NTLog(@"xmpp did disconnect");
[self logout];
if (!isXmppConnected) {
//[xmppStream authenticateWithPassword:password error:&error];
}
}
- (void)xmppStreamDidAuthenticate:(XMPPStream*)sender
{
NTLog(@"xmpp did authenticate");
//DDLogVerbose(@"%@: %@", THIS_FILE, THIS_METHOD);
[self doLogin];
// NSError* error;
//[xmppStream authenticateWithPassword:password error:&error];
}
- (void)xmppStream:(XMPPStream*)sender didNotAuthenticate:(NSXMLElement*)error
{
NTLog(@"xmpp did not Autherticate");
NTLog(@"didNotAuthenticate :密码校验失败,登录不成功,原因是:%@", [error XMLString]);
self.isLogined = login_status_no;
[self notify];
//DDLogVerbose(@"%@: %@", THIS_FILE, THIS_METHOD);
}
xmpp发送消息需要把消息包装成xml类型
#pragma mark------收发消息-------
- (void)sendMessage:(NTMessage*)msg roomName:(NSString*)roomName //TODO: 添加是否secret参数
{
XMPPMessage* aMessage;
NSString* from = [NSString stringWithFormat:@"%@@%@", msg.fromUserId, g_config.XMPPDomain];
if (roomName == nil) {
//单聊
NSString* to = [NSString stringWithFormat:@"%@@%@", msg.toUserId, g_config.XMPPDomain];
aMessage = [XMPPMessage messageWithType:@"chat" to:[XMPPJID jidWithString:to]];
to = nil;
}
else {
//群聊
NSString* roomJid = [NSString stringWithFormat:@"%@@muc.%@", roomName, g_config.XMPPDomain];
aMessage = [XMPPMessage messageWithType:@"groupchat" to:[XMPPJID jidWithString:roomJid]];
roomJid = nil;
}
[aMessage addAttributeWithName:@"id" stringValue:msg.messageId];
NSXMLElement* request = [NSXMLElement elementWithName:@"request" xmlns:@"urn:xmpp:receipts"];
[aMessage addChild:request];
request = nil;
DDXMLNode* node = [DDXMLNode elementWithName:@"body" stringValue:jsonString];
[aMessage addChild:node];
[xmppStream sendElement:aMessage];
//[aMessage release];
[_poolSend setObject:msg forKey:msg.messageId];
//NSrunloop的一个方法
[self performSelector:@selector(onSendTimeout:) withObject:msg afterDelay:[msg getMaxWaitTime]];
from = nil;
}
- (void)xmppStream:(XMPPStream*)sender didReceiveMessage:(XMPPMessage*)message
{
//analysis message
}
在xmpp登录以后,需要通过接口获取群列表,在使用for循环进行请求每个群的离线消息,如果距离上次时间太长,就会有很多离线消息等待解析,存储到数据库。
解决方法:采用异步请求服务器,获取离线消息,使用多线程来执行消息的数据库存储。使用主线程来把用户加入到当前群。
主要是xmpp框架中有内置的coredata数据库,每条消息会存储到coreData中。并且这个是xmpp内部控制的。
解决方法。在xmpp框架中,手动设置信息不保存到coreData中。我们是用的fmdb来保存消息。
标签:share this help b2c 参数 delay str cli xmpp
原文地址:https://www.cnblogs.com/sundaysgarden/p/11691270.html