商業化IM 客戶端接口設計分析

對於剛接觸IM(即時通信)開發,經過閱讀成熟的商業代碼可以對即時通信軟件大致上有個認識,好比消息發送,消息接受,消息監聽,羣聊,單聊,聊天室。我這邊直接拿Gobelieve IM 源碼來作剖析。IMService在代碼層級裏起着承上啓下的做用,負責發送消息,接受消息(聊天消息,系統消息,控制命令消息(好比邀請VOIP,退羣,加羣)),消息在客戶端轉發,消息類型判斷和分發,消息observer的增長和刪除,IMService自己會根據業務需求實現handlers對接到數據傳輸層(socket)。Observers是銜接IMService和UI層。若是隻側重於UI層開發,重點是Observers,好比PeerMessageObserver是一對一聊天監聽,GroupMessageObserver:羣聊天監聽,RoomMessageObserver:聊天室監聽。服務器

下面直接上接口代碼來講,app

@class IMessage;

IMessage 模型類的前置聲明socket

@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(),處理羣狀態改變的函數,好比羣名稱改變,羣成員改變,羣解散等等事件。ui

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

客服聊天的handler定義。atom

@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的定義,提供了對輸入狀態監聽的接口,用來實現,實時獲取對方是否在編輯消息。code

@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的定義。server

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

@end

聊天室消息Observer的定義。token

@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查看。

相關文章
相關標籤/搜索