即时通讯肯定少不了通讯协议,在ios开发中基本上都使用基于XMPP 的协议来做即时通讯。在oc下有封装好的XMPP协议库,你在开发时可以调用XMPP的API来进行开发。通讯的服务器可以用ejabberd来搭建。知道的就这些了
创新互联专业成都网站设计、成都网站制作、外贸网站建设,集网站策划、网站设计、网站制作于一体,网站seo、网站优化、网站营销、软文营销等专业人才根据搜索规律编程设计,让网站在运行后,在搜索中有好的表现,专业设计制作为您带来效益的网站!让网站建设为您创造效益。
[img]iOS 搭建xmpp聊天的具体步骤如下:
聊天室
[cpp] view plain copy
print?
//初始化聊天室
XMPPJID *roomJID = [XMPPJID jidWithString:ROOM_JID];
xmppRoom = [[XMPPRoom alloc] initWithRoomStorage:self jid:roomJID];
[xmppRoom activate:xmppStream];
[xmppRoom addDelegate:self delegateQueue:dispatch_get_main_queue()];
[cpp] view plain copy
print?
//创建聊天室成功
- (void)xmppRoomDidCreate:(XMPPRoom *)sender
{
DDLogInfo(@"%@: %@", THIS_FILE, THIS_METHOD);
}
[cpp] view plain copy
print?
//加入聊天室,使用昵称
[xmppRoom joinRoomUsingNickname:@"quack" history:nil];
[cpp] view plain copy
print?
//获取聊天室信息
- (void)xmppRoomDidJoin:(XMPPRoom *)sender
{
[xmppRoom fetchConfigurationForm];
[xmppRoom fetchBanList];
[xmppRoom fetchMembersList];
[xmppRoom fetchModeratorsList];
}
如果房间存在,会调用委托
[cpp] view plain copy
print?
// 收到禁止名单列表
- (void)xmppRoom:(XMPPRoom *)sender didFetchBanList:(NSArray *)items;
// 收到好友名单列表
- (void)xmppRoom:(XMPPRoom *)sender didFetchMembersList:(NSArray *)items;
// 收到主持人名单列表
- (void)xmppRoom:(XMPPRoom *)sender didFetchModeratorsList:(NSArray *)items;
房间不存在,调用委托
[cpp] view plain copy
print?
- (void)xmppRoom:(XMPPRoom *)sender didNotFetchBanList:(XMPPIQ *)iqError;
- (void)xmppRoom:(XMPPRoom *)sender didNotFetchMembersList:(XMPPIQ *)iqError;
- (void)xmppRoom:(XMPPRoom *)sender didNotFetchModeratorsList:(XMPPIQ *)iqError;
离开房间
[cpp] view plain copy
print?
[xmppRoom deactivate:xmppStream];
[cpp] view plain copy
print?
//离开聊天室
- (void)xmppRoomDidLeave:(XMPPRoom *)sender
{
DDLogVerbose(@"%@: %@", THIS_FILE, THIS_METHOD);
}
其他代理
[cpp] view plain copy
print?
//新人加入群聊
- (void)xmppRoom:(XMPPRoom *)sender occupantDidJoin:(XMPPJID *)occupantJID
{
DDLogVerbose(@"%@: %@", THIS_FILE, THIS_METHOD);
}
//有人退出群聊
- (void)xmppRoom:(XMPPRoom *)sender occupantDidLeave:(XMPPJID *)occupantJID
{
DDLogVerbose(@"%@: %@", THIS_FILE, THIS_METHOD);
}
//有人在群里发言
- (void)xmppRoom:(XMPPRoom *)sender didReceiveMessage:(XMPPMessage *)message fromOccupant:(XMPPJID *)occupantJID
{
DDLogVerbose(@"%@: %@", THIS_FILE, THIS_METHOD);
}
前提条件
另外必须了解一些CoreData相关知识;
聊天设置原理
在init初始化时:发送普通文本消息同样在XMPPManager中进行封装;
在聊天页面的viewDidload中:发送普通消息;
刷新消息的方法,需要熟悉CoreData知识;
发送图片等消息(重点)
送视频等其他文件也是一样,xmpp中需要将图片转化成NSData,然后转化成成base64的字符串进行传输,然后接收到之后再反转化成图片。
把图片设置为cell的imageView,所以图片显示了在左边,说明图片消息发送是成功的,视频等其他类型的消息,也是同样的原理。
其他
具体步骤流程图