溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

商業(yè)化IM 客戶端設計---Message模型

發(fā)布時間:2020-06-10 03:08:07 來源:網(wǎng)絡 閱讀:1184 作者:gobelieve_io 欄目:移動開發(fā)

在IM開發(fā)中,一個問題是怎么管理傳輸,包括處理消息發(fā)送,消息接受和怎么轉發(fā)等等,就是上一篇文章提到的IMService扮演的角色。另一個問題就是傳輸?shù)木唧w數(shù)據(jù)是怎么定義的,既包括業(yè)務數(shù)據(jù)(文字,語音,圖片,地理位置等),也包括控制數(shù)據(jù)(音頻請求,加群請求,離群請求)。現(xiàn)在通過分析Message實體類來學習一下。

下面一系列的常量定義了Message的type

#define MSG_HEARTBEAT 1  //心跳
#define MSG_AUTH 2 //認證
#define MSG_AUTH_STATUS 3 //認證狀態(tài)
#define MSG_IM 4
#define MSG_ACK 5 //ACK
#define MSG_RST 6 
#define MSG_GROUP_NOTIFICATION 7 //群消息
#define MSG_GROUP_IM 8 //群消息
#define MSG_PEER_ACK 9 //ACK
#define MSG_INPUTING 10 //輸入
#define MSG_SUBSCRIBE_ONLINE_STATE 11 //在線狀態(tài)
#define MSG_ONLINE_STATE 12 //在線狀態(tài)
#define MSG_PING 13  //
#define MSG_PONG 14  //
#define MSG_AUTH_TOKEN 15 //TOKEN
#define MSG_LOGIN_POINT 16 //多點登錄
#define MSG_RT 17
#define MSG_ENTER_ROOM 18 //進入聊天室
#define MSG_LEAVE_ROOM 19 //離開聊天室
#define MSG_ROOM_IM 20  //聊天室消息
#define MSG_SYSTEM 21  //系統(tǒng)消息
#define MSG_UNREAD_COUNT 22   //未讀消息數(shù)
#define MSG_CUSTOMER_SERVICE 23  //客服服務消息
#define MSG_CUSTOMER 24  //客服消息
#define MSG_CUSTOMER_SUPPORT 25 //客服支持
#define MSG_VOIP_CONTROL 64  //VOIP命令

  下面幾個常量定義了平臺type

#define PLATFORM_IOS  1       
#define PLATFORM_ANDROID 2
#define PLATFORM_WEB 3

  IMMessage類由接受者ID,發(fā)送者ID,時間戳,消息本地存儲ID,消息內容構成,見下面代碼。

@interface IMMessage : NSObject
@property(nonatomic, assign)int64_t sender;
@property(nonatomic, assign)int64_t receiver;
@property(nonatomic, assign)int32_t timestamp;
@property(nonatomic, assign)int32_t msgLocalID;
@property(nonatomic, copy)NSString *content;
@end

  CustomerMessage,客服消息類,和通用的IM消息格式差別在,customerID,sellerID,customerAppID,storeID這幾個屬性需要根據(jù)客服消息特定的使用場景來理解。

@interface CustomerMessage : NSObject
//本地消息id 不會序列化傳到服務器
@property(nonatomic, assign)int32_t msgLocalID;本地存儲ID
@property(nonatomic, assign)int64_t customerAppID;//APPid
@property(nonatomic, assign)int64_t customerID;//客服用戶ID
@property(nonatomic, assign)int64_t storeID;//商鋪ID
@property(nonatomic, assign)int64_t sellerID;//客服ID
@property(nonatomic, assign)int32_t timestamp;
@property(nonatomic, copy)NSString *content;
@end

RoomMessage 聊天室消息

  

@interface RoomMessage : NSObject
@property(nonatomic, assign)int64_t sender;
@property(nonatomic, assign)int64_t receiver;
@property(nonatomic, copy)NSString *content;
@end

  

消息輸入狀態(tài)

typedef RoomMessage RTMessage;

@interface MessageInputing : NSObject
@property(nonatomic, assign)int64_t sender;
@property(nonatomic, assign)int64_t receiver;
@end

    

  授權結構體

@interface AuthenticationToken : NSObject
@property(nonatomic, copy) NSString *token;
@property(nonatomic, assign) int8_t platformID;
@property(nonatomic, copy) NSString *deviceID;
@end

  

  多點登錄結構體

@interface LoginPoint : NSObject
@property(nonatomic, assign) int32_t upTimestamp;
@property(nonatomic, assign) int8_t platformID;
@property(nonatomic, copy) NSString *deviceID;
@end

 

@interface VOIPControl : NSObject
@property(nonatomic, assign) int64_t sender;
@property(nonatomic, assign) int64_t receiver;
@property(nonatomic) NSData *content;

@end

  

 

@interface Message : NSObject
@property(nonatomic, assign)int cmd;
@property(nonatomic, assign)int seq;
@property(nonatomic) NSObject *body;

-(NSData*)pack;

-(BOOL)unpack:(NSData*)data;
@end

  如果想要在現(xiàn)有的消息類型上支持新的消息類型,比如(實時定位,閱后即焚,(T)一下)。需要在Message基礎上做擴展。

完整的代碼和DEMO可以到[Gobelieve IM]查看。

  

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經查實,將立刻刪除涉嫌侵權內容。

AI