對于剛接觸IM(即時通訊)開發(fā),通過閱讀成熟的商業(yè)代碼能夠對即時通訊軟件大體上有個認識,比如消息發(fā)送,消息接受,消息監(jiān)聽,群聊,單聊,聊天室。我這邊直接拿[Gobelieve IM] 源碼來做剖析。IMService在代碼層級里起著承上啟下的作用,負責發(fā)送消息,接受消息(聊天消息,系統(tǒng)消息,控制命令消息(比如邀請VOIP,退群,加群)),消息在客戶端轉發(fā),消息類型判斷和分發(fā),消息observer的增加和刪除,IMService本身會根據(jù)業(yè)務需求實現(xiàn)handlers對接到數(shù)據(jù)傳輸層(socket)。Observers是銜接IMService和UI層。如果只側重于UI層開發(fā),重點是Observers,比如PeerMessageObserver是一對一聊天監(jiān)聽,GroupMessageObserver:群聊天監(jiān)聽,RoomMessageObserver:聊天室監(jiān)聽。
創(chuàng)新互聯(lián)公司主要從事成都網站建設、做網站、網頁設計、企業(yè)做網站、公司建網站等業(yè)務。立足成都服務阜新,十年網站建設經驗,價格優(yōu)惠、服務專業(yè),歡迎來電咨詢建站服務:18982081108下面直接上接口代碼來說,@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的設計,用來顯示消息是否已經通過服務器下發(fā)到對方客戶端。具體的函數(shù),handleMessage()接收到消息的處理函數(shù)。handleMessageACK()接收到消息已讀的處理函數(shù)。 handleMessageFailure()接收到消息發(fā)送失敗的處理函數(shù)。
@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定義,接口上比單聊多一個群狀態(tài)改變的處理,還有就是單聊下發(fā)的是個人ID,群聊下發(fā)的是群聊ID,同樣的函數(shù),handleMessage()接收到消息的處理函數(shù)。handleMessageACK()接收到消息已讀的處理函數(shù)。 handleMessageFailure()接收到消息發(fā)送失敗的處理函數(shù)。handleGroupNotification(),處理群狀態(tài)改變的函數(shù),比如群名稱改變,群成員改變,群解散等等事件。
@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
多端登錄事件監(jiān)聽。
@protocol PeerMessageObserver <NSObject> @optional -(void)onPeerMessage:(IMMessage*)msg; //服務器ack -(void)onPeerMessageACK:(int)msgLocalID uid:(int64_t)uid; //消息發(fā)送失敗 -(void)onPeerMessageFailure:(int)msgLocalID uid:(int64_t)uid; //對方正在輸入 -(void)onPeerInputing:(int64_t)uid; @end
一對一聊天的Observer的定義,提供了對輸入狀態(tài)監(jiān)聽的接口,用來實現(xiàn),實時獲取對方是否在編輯消息。
@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
系統(tǒng)消息的Observer的定義。
@protocol CustomerMessageObserver <NSObject> @optional -(void)onCustomerMessage:(CustomerMessage*)msg; -(void)onCustomerSupportMessage:(CustomerMessage*)msg; //服務器ack -(void)onCustomerMessageACK:(CustomerMessage*)msg; //消息發(fā)送失敗 -(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實現(xiàn)了三個(一對一聊天,群聊,客服)handler,可以按自己需要增加新的handler類型。消息統(tǒng)一在IMService做轉發(fā)。 根據(jù)注冊的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; //更新未讀的消息數(shù)目 -(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/
另外有需要云服務器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。
新聞名稱:商業(yè)化IM客戶端接口設計分析-創(chuàng)新互聯(lián)
本文地址:http://aaarwkj.com/article34/cocdpe.html
成都網站建設公司_創(chuàng)新互聯(lián),為您提供網頁設計公司、企業(yè)建站、微信小程序、服務器托管、用戶體驗、手機網站建設
聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內容