首页 > 代码库 > 商业化IM 客户端接口设计分析

商业化IM 客户端接口设计分析

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

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

  IMessage 模型类的前置声明

@protocol IMPeerMessageHandler <NSObject>-(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 <NSObject>-(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 <NSObject>-(BOOL)handleCustomerSupportMessage:(CustomerMessage*)msg;-(BOOL)handleMessage:(CustomerMessage*)msg;-(BOOL)handleMessageACK:(CustomerMessage*)msg;-(BOOL)handleMessageFailure:(CustomerMessage*)msg;@end

  客服聊天的handler定义。 

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

  多端登录事件监听。 

@protocol PeerMessageObserver <NSObject>@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 <NSObject>@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 <NSObject>@optional-(void)onRoomMessage:(RoomMessage*)rm;-(void)onRoomMessageACK:(RoomMessage*)rm;-(void)onRoomMessageFailure:(RoomMessage*)rm;@end

  聊天室消息Observer的定义。 

@protocol RTMessageObserver <NSObject>@optional-(void)onRTMessage:(RTMessage*)rt;@end@protocol SystemMessageObserver <NSObject>@optional-(void)onSystemMessage:(NSString*)sm;@end

  系统消息的Observer的定义。 

@protocol CustomerMessageObserver <NSObject>@optional-(void)onCustomerMessage:(CustomerMessage*)msg;-(void)onCustomerSupportMessage:(CustomerMessage*)msg;//服务器ack-(void)onCustomerMessageACK:(CustomerMessage*)msg;//消息发送失败-(void)onCustomerMessageFailure:(CustomerMessage*)msg;@end

  客服消息的Observer的定义。 

@protocol VOIPObserver <NSObject>-(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<IMPeerMessageHandler> peerMessageHandler;//一对一聊天Handler@property(nonatomic, weak)id<IMGroupMessageHandler> groupMessageHandler;//群聊handler@property(nonatomic, weak)id<IMCustomerMessageHandler> 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<PeerMessageObserver>)ob;-(void)removePeerMessageObserver:(id<PeerMessageObserver>)ob;-(void)addGroupMessageObserver:(id<GroupMessageObserver>)ob;-(void)removeGroupMessageObserver:(id<GroupMessageObserver>)ob;-(void)addLoginPointObserver:(id<LoginPointObserver>)ob;-(void)removeLoginPointObserver:(id<LoginPointObserver>)ob;-(void)addRoomMessageObserver:(id<RoomMessageObserver>)ob;-(void)removeRoomMessageObserver:(id<RoomMessageObserver>)ob;-(void)addSystemMessageObserver:(id<SystemMessageObserver>)ob;-(void)removeSystemMessageObserver:(id<SystemMessageObserver>)ob;-(void)addCustomerMessageObserver:(id<CustomerMessageObserver>)ob;-(void)removeCustomerMessageObserver:(id<CustomerMessageObserver>)ob;-(void)addRTMessageObserver:(id<RTMessageObserver>)ob;-(void)removeRTMessageObserver:(id<RTMessageObserver>)ob;    -(void)pushVOIPObserver:(id<VOIPObserver>)ob;-(void)popVOIPObserver:(id<VOIPObserver>)ob;-(BOOL)sendVOIPControl:(VOIPControl*)ctl;@end

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

 

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

商业化IM 客户端接口设计分析