商業化IM 客戶端設計---Message模型

在IM開發中,一個問題是怎麼管理傳輸,包括處理消息發送,消息接受和怎麼轉發等等,就是上一篇文章提到的IMService扮演的角色。另外一個問題就是傳輸的具體數據是怎麼定義的,既包括業務數據(文字,語音,圖片,地理位置等),也包括控制數據(音頻請求,加羣請求,離羣請求)。如今經過分析Message實體類來學習一下。服務器

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

#define MSG_HEARTBEAT 1  //心跳
#define MSG_AUTH 2 //認證
#define MSG_AUTH_STATUS 3 //認證狀態
#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 //在線狀態
#define MSG_ONLINE_STATE 12 //在線狀態
#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  //系統消息
#define MSG_UNREAD_COUNT 22   //未讀消息數
#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,發送者ID,時間戳,消息本地存儲ID,消息內容構成,見下面代碼。atom

@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這幾個屬性須要根據客服消息特定的使用場景來理解。orm

@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 聊天室消息token

  

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

  

消息輸入狀態圖片

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

  

  多點登陸結構體get

@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

  若是想要在現有的消息類型上支持新的消息類型,好比(實時定位,閱後即焚,(T)一下)。須要在Message基礎上作擴展。cmd

完整的代碼和DEMO能夠到[Gobelieve IM]查看。

相關文章
相關標籤/搜索