商业化IM 客户端设计---Message模型

在IM开发中,一个问题是怎么管理传输,包括处理消息发送,消息接受和怎么转发等等,就是上一篇文章提到的IMService扮演的角色。另外一个问题就是传输的具体数据是怎么定义的,既包括业务数据(文字,语音,图片,地理位置等),也包括控制数据(音频请求,加群请求,离群请求)。如今经过分析Message实体类来学习一下。服务器

下面一系列的常量定义了Message的type学习

#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命令

下面几个常量定义了平台typeatom

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

IMMessage类由接受者ID,发送者ID,时间戳,消息本地存储ID,消息内容构成,见下面代码。code

@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

cmd

@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基础上作扩展。

完整的代码和DEMO能够到Gobelieve IM查看。

相关文章
相关标签/搜索