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

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

#defineMSG_HEARTBEAT1//心跳#defineMSG_AUTH2//认证#defineMSG_AUTH_STATUS3//认证状态#defineMSG_IM4#defineMSG_ACK5//ACK#defineMSG_RST6#defineMSG_GROUP_NOTIFICATION7//群消息#defineMSG_GROUP_IM8//群消息#defineMSG_PEER_ACK9//ACK#defineMSG_INPUTING10//输入#defineMSG_SUBSCRIBE_ONLINE_STATE11//在线状态#defineMSG_ONLINE_STATE12//在线状态#defineMSG_PING13//#defineMSG_PONG14//#defineMSG_AUTH_TOKEN15//TOKEN#defineMSG_LOGIN_POINT16//多点登录#defineMSG_RT17#defineMSG_ENTER_ROOM18//进入聊天室#defineMSG_LEAVE_ROOM19//离开聊天室#defineMSG_ROOM_IM20//聊天室消息#defineMSG_SYSTEM21//系统消息#defineMSG_UNREAD_COUNT22//未读消息数#defineMSG_CUSTOMER_SERVICE23//客服服务消息#defineMSG_CUSTOMER24//客服消息#defineMSG_CUSTOMER_SUPPORT25//客服支持#defineMSG_VOIP_CONTROL64//VOIP命令

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

#definePLATFORM_IOS1#definePLATFORM_ANDROID2#definePLATFORM_WEB3

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

@interfaceIMMessage:NSObject@property(nonatomic,assign)int64_tsender;@property(nonatomic,assign)int64_treceiver;@property(nonatomic,assign)int32_ttimestamp;@property(nonatomic,assign)int32_tmsgLocalID;@property(nonatomic,copy)NSString*content;@end

  CustomerMessage,客服消息类,和通用的IM消息格式差别在,customerID,sellerID,customerAppID,storeID这几个属性需要根据客服消息特定的使用场景来理解。

@interfaceCustomerMessage:NSObject//本地消息id不会序列化传到服务器@property(nonatomic,assign)int32_tmsgLocalID;本地存储ID@property(nonatomic,assign)int64_tcustomerAppID;//APPid@property(nonatomic,assign)int64_tcustomerID;//客服用户ID@property(nonatomic,assign)int64_tstoreID;//商铺ID@property(nonatomic,assign)int64_tsellerID;//客服ID@property(nonatomic,assign)int32_ttimestamp;@property(nonatomic,copy)NSString*content;@end

RoomMessage 聊天室消息

  

@interfaceRoomMessage:NSObject@property(nonatomic,assign)int64_tsender;@property(nonatomic,assign)int64_treceiver;@property(nonatomic,copy)NSString*content;@end

  

消息输入状态

typedefRoomMessageRTMessage;@interfaceMessageInputing:NSObject@property(nonatomic,assign)int64_tsender;@property(nonatomic,assign)int64_treceiver;@end

    

  授权结构体

@interfaceAuthenticationToken:NSObject@property(nonatomic,copy)NSString*token;@property(nonatomic,assign)int8_tplatformID;@property(nonatomic,copy)NSString*deviceID;@end

  

  多点登录结构体

@interfaceLoginPoint:NSObject@property(nonatomic,assign)int32_tupTimestamp;@property(nonatomic,assign)int8_tplatformID;@property(nonatomic,copy)NSString*deviceID;@end

@interfaceVOIPControl:NSObject@property(nonatomic,assign)int64_tsender;@property(nonatomic,assign)int64_treceiver;@property(nonatomic)NSData*content;@end

  

@interfaceMessage:NSObject@property(nonatomic,assign)intcmd;@property(nonatomic,assign)intseq;@property(nonatomic)NSObject*body;-(NSData*)pack;-(BOOL)unpack:(NSData*)data;@end

  如果想要在现有的消息类型上支持新的消息类型,比如(实时定位,阅后即焚,(T)一下)。需要在Message基础上做扩展。

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