_m_xmppRoom = [[XMPPRoom alloc] initWithRoomStorage:_xmppRoomDatajid:RoomJID dispatchQueue:queue2 ];
那我这里就详细介绍一下各个参数该如何设置.
第一个参数,是XMPPRoomCoreDataStorage类型,他是为这个房间分配一个coredata的数据库,但由于我是用自己的数据库,所以就用
_xmppRoomData = [[XMPPRoomCoreDataStoragealloc] initWithInMemoryStore];
来实例化之。
第二个参数则是Room的JID这个需要自己分配,不能跟服务器已经有的JID重复。
第三个参数则是房间所在队列,此参数特别重要,请大家一定注意。xmpp的提供了一个IOS 的demo,此demo很大程度上误导了大家,因为他初始化queue都用的是
dispatch_get_main_queue()
这是主线程队列(没错就是管UI更新的那个),他可能是为了图写domo方便所以才用这个队列。但是实际的聊天软件,你敢在写主线程队列里面?一个也许还行,20个房间呢?程序根本就卡到爆。所以我建议大家重新新建一个串行队列
e.g :
queue2 =dispatch_queue_create("com.我的工程.RoomQueue",NULL); // 串行queue
你也许想问,为什么一定要用串行?并行为什么不行?因为,stream也是串行的,也是一条一条往服务器送。
注意事项:最好自己抽象一个RoomClass,一个聊天室就是一个RoomClass对象,这样可以方便管控房间消息和加入离开房间等。下面贴上我自己的RoomClass和初始化RoomClass方法:
RoomClass.h
//
// RoomClass.h
// MICI13
//
// Created by on 14-6-17.
// Copyright (c) 2014年 DoubleKill. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "XMPPFramework.h"
#import "XMPPRoom.h"
#import "CoreDataManager.h"
//#import "CentreControl.h"
#import "TFTimer.h"
@interface RoomClass :NSObject<XMPPRoomDelegate>{
NSTimer *RoomTimer;
XMPPStream *stream;
}
//#define kNofSingleMessage @"kNofSingleMessage"
//#define kNofRoomMessage @"kNofRoomMessage"
//#define kNofRoomDidJion @"kNofRoomDidJion"
//#define kNofRoomReceiveInvitation @"kNofRoomReceiveInvitation"
typedefvoid(^handler)(BOOL result,id obj) ;
@property (nonatomic,copy)handler RoomBlock;//
@property (nonatomic,strong) XMPPRoomCoreDataStorage *xmppRoomData;
@property (nonatomic,strong) XMPPRoom *m_xmppRoom;
@property (nonatomic,copy) NSString *Jid;
@property (nonatomic,assign) BOOL isCame;
@property (nonatomic,assign) BOOL isLoadHistory;
- (id)initWithJid:(NSString *)jid members:(NSMutableArray *)members stream:(XMPPStream*)xmppstream Finish:(handler)finished;
-(void)leaveRoom;
@end
//
// RoomClass.m
// MICI13
//
// Created by on 14-6-17.
// Copyright (c) 2014年 DoubleKill. All rights reserved.
//
#import "RoomClass.h"
@implementation RoomClass{
XMPPStream * m_stream;
NSMutableArray * members;
NSString *Roomjid;
NSDate *dateIn;
}
extern dispatch_queue_t queue2;
//
//NSString *const kXMPPmyJID = @"kXMPPmyJID";
//NSString *const kXMPPmyPassword = @"kXMPPmyPassword";
- (id)initWithJid:(NSString *)RoomJID members:(NSMutableArray *)member stream:(XMPPStream*)xmppstream Finish:(handler)finished
{
_RoomBlock = finished;
m_stream = xmppstream;
Roomjid = RoomJID;
members = member;
self = [superinit];
if (self) {
// Custom initialization
_xmppRoomData = [[XMPPRoomCoreDataStoragealloc]