RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
商业化IM客户端接口设计分析

  对于刚接触IM(即时通讯)开发,通过阅读成熟的商业代码能够对即时通讯软件大体上有个认识,比如消息发送,消息接受,消息监听,群聊,单聊,聊天室。我这边直接拿[Gobelieve IM] 源码来做剖析。IMService在代码层级里起着承上启下的作用,负责发送消息,接受消息(聊天消息,系统消息,控制命令消息(比如邀请VOIP,退群,加群)),消息在客户端转发,消息类型判断和分发,消息observer的增加和删除,IMService本身会根据业务需求实现handlers对接到数据传输层(socket)。Observers是衔接IMService和UI层。如果只侧重于UI层开发,重点是Observers,比如PeerMessageObserver是一对一聊天监听,GroupMessageObserver:群聊天监听,RoomMessageObserver:聊天室监听。

在牡丹江等地区,都构建了全面的区域性战略布局,加强发展的系统性、市场前瞻性、产品创新能力,以专注、极致的服务理念,为客户提供成都网站制作、成都网站设计 网站设计制作按需求定制制作,公司网站建设,企业网站建设,成都品牌网站建设,全网整合营销推广,外贸网站建设,牡丹江网站建设费用合理。

下面直接上接口代码来说,
@class IMessage;

  IMessage 模型类的前置声明

@protocol IMPeerMessageHandler 
-(BOOL)handleMessage:(IMMessage*)msg uid:(int64_t)uid;
-(BOOL)handleMessageACK:(int)msgLocalID uid:(int64_t)uid;
-(BOOL)handleMessageFailure:(int)msgLocalID uid:(int64_t)uid;
@end

    一对一聊天的hanlder定义,IM有一个ACK的设计,用来显示消息是否已经通过服务器下发到对方客户端。具体的函数,handleMessage()接收到消息的处理函数。handleMessageACK()接收到消息已读的处理函数。 handleMessageFailure()接收到消息发送失败的处理函数。 

@protocol IMGroupMessageHandler 

-(BOOL)handleMessage:(IMMessage*)msg;
-(BOOL)handleMessageACK:(int)msgLocalID gid:(int64_t)gid;
-(BOOL)handleMessageFailure:(int)msgLocalID gid:(int64_t)gid;

-(BOOL)handleGroupNotification:(NSString*)notification;
@end

     群聊天的hanlder定义,接口上比单聊多一个群状态改变的处理,还有就是单聊下发的是个人ID,群聊下发的是群聊ID,同样的函数,handleMessage()接收到消息的处理函数。handleMessageACK()接收到消息已读的处理函数。 handleMessageFailure()接收到消息发送失败的处理函数。handleGroupNotification(),处理群状态改变的函数,比如群名称改变,群成员改变,群解散等等事件。 

@protocol IMCustomerMessageHandler 
-(BOOL)handleCustomerSupportMessage:(CustomerMessage*)msg;
-(BOOL)handleMessage:(CustomerMessage*)msg;
-(BOOL)handleMessageACK:(CustomerMessage*)msg;
-(BOOL)handleMessageFailure:(CustomerMessage*)msg;
@end

 客服聊天的handler定义。 

@protocol LoginPointObserver 
//用户在其他地方登陆
-(void)onLoginPoint:(LoginPoint*)lp;
@end

 多端登录事件监听。 

@protocol PeerMessageObserver 
@optional
-(void)onPeerMessage:(IMMessage*)msg;

//服务器ack
-(void)onPeerMessageACK:(int)msgLocalID uid:(int64_t)uid;

//消息发送失败
-(void)onPeerMessageFailure:(int)msgLocalID uid:(int64_t)uid;

//对方正在输入
-(void)onPeerInputing:(int64_t)uid;

@end

  一对一聊天的Observer的定义,提供了对输入状态监听的接口,用来实现,实时获取对方是否在编辑消息。 

@protocol GroupMessageObserver 
@optional
-(void)onGroupMessage:(IMMessage*)msg;
-(void)onGroupMessageACK:(int)msgLocalID gid:(int64_t)gid;
-(void)onGroupMessageFailure:(int)msgLocalID gid:(int64_t)gid;

-(void)onGroupNotification:(NSString*)notification;
@end

  群聊天的Observer的定义。 

@protocol RoomMessageObserver 
@optional
-(void)onRoomMessage:(RoomMessage*)rm;
-(void)onRoomMessageACK:(RoomMessage*)rm;
-(void)onRoomMessageFailure:(RoomMessage*)rm;

@end

  聊天室消息Observer的定义。 

@protocol RTMessageObserver 

@optional
-(void)onRTMessage:(RTMessage*)rt;

@end

@protocol SystemMessageObserver 
@optional
-(void)onSystemMessage:(NSString*)sm;

@end

  系统消息的Observer的定义。 

@protocol CustomerMessageObserver 
@optional
-(void)onCustomerMessage:(CustomerMessage*)msg;
-(void)onCustomerSupportMessage:(CustomerMessage*)msg;

//服务器ack
-(void)onCustomerMessageACK:(CustomerMessage*)msg;
//消息发送失败
-(void)onCustomerMessageFailure:(CustomerMessage*)msg;
@end

  客服消息的Observer的定义。 

@protocol VOIPObserver 

-(void)onVOIPControl:(VOIPControl*)ctl;

@end

  支持整合VOIP功能的Observer的定义。 

@interface IMService : TCPConnection
@property(nonatomic, copy) NSString *deviceID;
@property(nonatomic, copy) NSString *token;
@property(nonatomic) int64_t uid;
//客服app需要设置,普通app不需要设置
@property(nonatomic) int64_t appID;

@property(nonatomic, weak)id peerMessageHandler;//一对一聊天Handler
@property(nonatomic, weak)id groupMessageHandler;//群聊handler
@property(nonatomic, weak)id customerMessageHandler;//客服handler

当前的IMService实现了三个(一对一聊天,群聊,客服)handler,可以按自己需要增加新的handler类型。消息统一在IMService做转发。
根据注册的Observer,传递到对该消息类型感兴趣的界面。

+(IMService*)instance;//IMService是单例的形式使用

-(BOOL)isPeerMessageSending:(int64_t)peer id:(int)msgLocalID;
-(BOOL)isGroupMessageSending:(int64_t)groupID id:(int)msgLocalID;
-(BOOL)isCustomerSupportMessageSending:(int)msgLocalID
                            customerID:(int64_t)customerID
                         customerAppID:(int64_t)customerAppID;
-(BOOL)isCustomerMessageSending:(int)msgLocalID storeID:(int64_t)storeID;

-(BOOL)sendPeerMessage:(IMMessage*)msg;
-(BOOL)sendGroupMessage:(IMMessage*)msg;
-(BOOL)sendRoomMessage:(RoomMessage*)msg;
//顾客->客服
-(BOOL)sendCustomerMessage:(CustomerMessage*)im;
//客服->顾客
-(BOOL)sendCustomerSupportMessage:(CustomerMessage*)im;
-(BOOL)sendRTMessage:(RTMessage*)msg;

-(void)enterRoom:(int64_t)roomID;
-(void)leaveRoom:(int64_t)roomID;

//正在输入
-(void)sendInputing:(MessageInputing*)inputing;
//更新未读的消息数目
-(void)sendUnreadCount:(int)unread;

-(void)addPeerMessageObserver:(id)ob;
-(void)removePeerMessageObserver:(id)ob;

-(void)addGroupMessageObserver:(id)ob;
-(void)removeGroupMessageObserver:(id)ob;

-(void)addLoginPointObserver:(id)ob;
-(void)removeLoginPointObserver:(id)ob;

-(void)addRoomMessageObserver:(id)ob;
-(void)removeRoomMessageObserver:(id)ob;

-(void)addSystemMessageObserver:(id)ob;
-(void)removeSystemMessageObserver:(id)ob;

-(void)addCustomerMessageObserver:(id)ob;
-(void)removeCustomerMessageObserver:(id)ob;

-(void)addRTMessageObserver:(id)ob;
-(void)removeRTMessageObserver:(id)ob;
    
-(void)pushVOIPObserver:(id)ob;
-(void)popVOIPObserver:(id)ob;

-(BOOL)sendVOIPControl:(VOIPControl*)ctl;

@end

  坑下挖好,慢慢补充,完整的代码和DEMO可以到[Gobelieve IM]查看。

 

[1]: http://developer.gobelieve.io/


网站名称:商业化IM客户端接口设计分析
网页地址:http://cqwzjz.cn/article/ppcdpp.html