您好,登錄后才能下訂單哦!
對于剛接觸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/
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。