大橙子网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

商业化IM客户端接口设计分析-创新互联

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

创新互联公司主要从事成都网站建设、做网站、网页设计、企业做网站、公司建网站等业务。立足成都服务阜新,十年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:18982081108下面直接上接口代码来说,
@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/

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


分享名称:商业化IM客户端接口设计分析-创新互联
网页路径:http://dzwzjz.com/article/cocdpe.html
在线咨询
服务热线
服务热线:028-86922220
TOP