Chat Cloud Newsletter

Source: Internet
Author: User

IOS SDK 2.0 Architecture
This document will detail the cloud-based SDK product architecture and messaging architecture to help you gain a deeper understanding of the cloud and develop your products faster.

Image
Cloud SDK System Architecture
Imkit

Imkit's function is to encapsulate a variety of interface objects, service developers to quickly implement their own products, the main feature is to support the rapid integration, support rich interface customization features.

Imlib

Imlib's function is to provide a library of basic communication capabilities, encapsulating various objects such as communication capabilities and Conversation,message, and serving developers who need to implement their own interfaces according to their own products. The main features are clear package, light weight, easy to use.

Protocol

Protocol is the core protocol stack for cloud, using a private binary protocol that is custom-made by cloud. The main features are light weight, orderly and reliable, do not lose the message. The Protocol section is developed using Native language to ensure business consistency on Android and IOS platforms, enabling developers to make their products commercially available.

Imlib Architecture
First introduce the Imlib system, for users who really use the cloud SDK, whether you choose Imlib or Imkit, you need to understand the system concept.

Image
Cloud Imlib Architecture
Imlib's naming convention, which is rcimclient on IOS, is rongimclient on Android. This is to match the naming conventions of two platforms.
Core class
Rcimclinet is the core class of Imlib, and you can use it directly, such as Init,connect,setdevicetoken,sendmessage, sendimagemessage, etc. You need to know more about the usage of all interfaces, please refer to the API documentation.

Session entity Class
Rcconversation is the session entity class, first you need to fully understand the relationship between the conversation (session) and message (message) two entity classes.

The session entity class and the message entity class are container classes used to store local sessions and messages, in addition to the contents of the session and the message, including the various states that are stored locally, and the objects that you get when you read the message to the client are related to these two classes. There are several types of sessions, either private chat sessions or group sessions, and each conversation (session) contains multiple messages, as shown in the relationship:

Image

With Conversationtype and Targetid, you can uniquely identify a session. Conversationtype enumeration value meanings and corresponding Targetid meanings are:

Session Type enumeration Conversationtype description corresponding to Targetid
The Id (userId) of the PRIVATE single-chat user.
Id (groupId) for group group groups.
DISCUSSION the Id of the discussion group discussion group (Discussionid).
Id of the chatroom chat room (chatroomid).
Customer_service Customer Service Id (Customerserviceid).
System account Id of the system session. Can be understood as the role of QQ number 10,000th.
App_public_service Application Public Service Id (Publicserviceid) of the public service.
Public_service Public Service Id (Publicserviceid).
Note that the session type and message type are distinguished, and the conversation type is the classification for the session, and the different session types determine the different session logic.
Another: The system session type does not necessarily mean "system message", essentially no different from the single-chat session type, but logically makes a different distinction and facilitates the deployment of different product business logic.
With a combination of Conversationtype and Targetid, you can determine a unique session. If you need to pass in Conversationtype_private and userId when you want to initiate a single chat session, you need to pass in Conversationtype_group and groupId when you need to initiate a group chat, when you need to initiate a discussion group session , you need to pass in Conversationtype_discussion and Discussionid.

Message entity class
Rcmessage is the message entity class, the message entity class is the outer container of the message class, the message entity object is the outer object that the Message object stores locally, and the message entity object encapsulates the basic information of the message such as type, ID, direction of message, receive state, receive time, sender, etc. You may need to understand the concept of two member parameters first.

ObjectName

Represents the type of message content, is an identifier (Identifier), the cloud built-in message type with RC: Start, such as rc:txtmsg,rc:imgmsg,rc:vcmsg.

Content

The message class, an object of type Rcmessagecontent, represents the actual message.

Message class
Messagecontent is the message class, and he is also the base class for all messages, such as text, pictures, etc., that inherit from it, such as you want to customize the message, but also inherit its implementation.

The message class is different from the message entity class (Rcmessage), the message class represents a specific message content, the message entity class is the outer container of the message class, the message entity object is the outer object that the message object is stored locally, and the message entity object contains the message object, including the message direction, receive state, receive time, Sender and so on.

Each message content is an identifier (Identifier) that identifies its own type, which must be unique. The important function of the message content class is encode, and each message needs to implement its own encode method to encapsulate the message content.

There are two types of messages in the current version, ordinary content class messages, and notification class messages, which are then carefully described separately.

Message classification message Behavior Status identification
A content class message represents a message sent between a user that contains specific content that needs to be displayed on a chat interface, such as a text message, a voice message, and so on.
The notification class message represents a notification message that may be displayed on the chat interface, such as a cue bar notification.
Text Message Class
Rctextmessage. Used to send text-class messages, which can include hyperlinks that are automatically recognized. Inherited from Rcmessagecontent, is an ordinary content class message.

Message Class Name: Rctextmessage

Message OBJECTNAME:RC:TXTMSG

Message status Behavior ID: messagetag.ispersisted | messagetag.iscounted

Structure of the message: {"Content": "Hello world!", "Extra": ""}

Where content is the text content of a text message, extra can place arbitrary data content, or you can remove this attribute.

Voice Message Class
Rcvoicemessage. Used to send a voice fragment message, which can include hyperlinks that are automatically recognized.

Message Class Name: Rcvoicemessage

Message OBJECTNAME:RC:VCMSG

Message status Behavior ID: messagetag.ispersisted | messagetag.iscounted

Structure of the message: {"Content": "bhzpzjximrwrtvc=", "duration": 7, "Extra": ""}

Where content for voice message recording transcoding into AMR format, the result value of BASE64 encoding, duration as the length of the voice message (in seconds), extra can put arbitrary data content, can also remove this attribute.

Picture Message Class
Rcimagemessage. Used to send a picture class message.

Message Class Name: Rcimagemessage

Message OBJECTNAME:RC:IMGMSG

Message status Behavior ID: messagetag.ispersisted | messagetag.iscounted

Structure of the message: {"Content": "bhzpzjximrwrtvc=", "Imageuri": "Http://p1.cdn.com/fds78ruhi.jpg", "Extra": ""}

Picture messages include two main parts: thumbnails and large images, thumbnails directly Base64 encoded into the content, the big picture first uploaded to the file server (the cloud SDK by default upload to seven Cow cloud storage), and then the cloud storage on the large map address into the message body. The flow diagram is as follows:

The content for the picture contents for the BASE64 encoded result value, Imageuri for the image uploaded to the image storage server after the address, extra can put arbitrary data content, you can also remove this attribute.

The thumbnail size is: x 240 pixels, in width and height of the longer edge of not more than 240 pixels, etc. than compression.

Large image size: 960 x 960 pixels, in width and height of the longer edge of not more than 960 pixels, etc. than compression.

Text Message Class
Rcrichcontentmessage. Used to send a text message, including a title, a piece of text and a picture.

Message Class Name: Rcrichcontentmessage

Message OBJECTNAME:RC:IMGTEXTMSG

Message status Behavior ID: messagetag.ispersisted | messagetag.iscounted

Structure of the message: {"title": "Big News", "Content": "I ' m ironman.", "Imageuri": "Http://p1.cdn.com/fds78ruhi.jpg", "url": "/HTTP/ Www.rongcloud.cn "," Extra ":" "}

Where title is the title of the message, content is the text of the message, Imageuri is the address of the image, the URL is the address of the jump, extra can place arbitrary data content, you can also remove this attribute.

The image size is: x 120 pixels.

Geolocation Message Class
Rclocationmessage. Used to send geo-location messages.

Message Class Name: Rclocationmessage

Message OBJECTNAME:RC:LBSMSG

Message status Behavior ID: messagetag.ispersisted | messagetag.iscounted

Structure of the message: {"Content": "bhzpzjximrwrtvc=", "latitude": 39.9139, "longitude": 116.3917, "poi": "bei Yuan Lu Beichen tai Yue da Xia, Chaoyang District, Peking City", "Extra": "" }

The contents of the map thumbnail content for the BASE64 encoded result value, latitude is the latitude value of the position, longitude is the longitude value of the position, POI is the location of the point of interest name, extra can put arbitrary data content, can also be removed from this property.

Cue bar (small gray bar) notification message
Rcinformationnotificationmessage. Used to send notification of the cue bar (small gray bar) displayed on the chat session page.

Message Class Name: Rcinformationnotificationmessage

Message OBJECTNAME:RC:INFONTF

Message status Behavior ID: messagetag.ispersisted

Structure of message: {"message": "Please pay attention to personal property safety in Chat", Extra: "}

Where message is the content of the cue bar messages, extra can place arbitrary data content, or you can remove this property.

Contact (friend) notification message
Rccontactnotificationmessage. A notification message used to send a contact action (plus friends, etc.).

Message Class Name: Rccontactnotificationmessage

Message OBJECTNAME:RC:CONTACTNTF

Message status Behavior ID: messagetag.ispersisted

Structure of the message: {"Operation": "Request", "Sourceuserid": "123", "Targetuserid": "456", "message": "I am AI, can you add a friend?" "," Extra ":" "}

Where operation is a contact action instruction, Sourceuserid for the user to notify Id,targetuserid to the user who receives the notification id,message the message content that accompanies the notification, extra can place arbitrary data content, can also remove this attribute.

The official definition for the operation attribute is "Request", "Acceptresponse", "rejectresponse" several constants, or it can be extended by the developer itself.

Data notification message
Rcprofilenotificationmessage. Used to send user data change notification messages.

Message Class Name: Rcprofilenotificationmessage

Message OBJECTNAME:RC:PROFILENTF

Message status Behavior ID: messagetag.ispersisted

Structure of message: {"Operation": "Update", "Data": "{\" nickname\ ": \" Han meimei \ ", \" hometown\ ": \" beijing\ "}", "Extra": "}

Where operation is a data notification operation, can be defined by itself, data for the operation, extra can put arbitrary data content, you can also remove this attribute.

General Command notification messages
Rccommandnotificationmessage. Used to send a generic instruction notification message that can define any JSON content within the message.

Message Class Name: Rccommandnotificationmessage

Message OBJECTNAME:RC:CMDNTF

Message status Behavior ID: messagetag.ispersisted

Structure of the message: {"name": "Atperson", "Data": "{\" sourceid\ ": \" 9527\ "}"}

Where name is the command name and can be defined by itself, data is the content of the command.

Group notification messages
The notification message used to send the group action.

Message Class Name: Rcgroupnotificationmessage

Message OBJECTNAME:RC:GRPNTF

Message status Behavior ID: messagetag.ispersisted

Structure of the message: {"Operatoruserid": "4324", "Operation": "Rename", "Data": "Local Life", "message": "Modify local life of this group", "Extra": "}

where Operatoruserid for the operator user id,operation as the operation name, data for the operation of information such as: Target user Id or modified group name, message is the content of messages, extra can place arbitrary data content, you can also remove this property.

Discussion group notification messages
A notification message that is used to send a discussion group action.

Message Class Name: Rcdiscussionnotificationmessage

Message OBJECTNAME:RC:DIZNTF

Message status Behavior ID: messagetag.ispersisted

Structure of the message: {"type": 1, "extension": "3213,4332", "operator": "5435"}

Where type is the discussion group action Type 1: Join discussion Group 2: Exit discussion Group 3: Discussion Group Rename 4: Discussion group admin kicks people, extension to be added to the discussion group user ID, multiple user IDs are separated by commas, operator is the current operation user ID.

Command message
Rccommandmessage. Used to send a generic instruction notification message, the message can be defined in any JSON content, and the difference between the General Command notification message is not stored, not counted.

Message Class Name: Rccommandmessage

Message OBJECTNAME:RC:CMDMSG

Structure of the message: {"name": "Atperson", "Data": "{\" sourceid\ ": \" 9527\ "}"}

Where name is the command name and can be defined by itself, data is the content of the command.

Custom messages
The cloud contains five message content and four notification class messages. This can meet most of the requirements, but for the special needs of your App, you may want to add your own unique content type, which is the custom message.

To define the message yourself, first understand the two concepts of ObjectName and Messagetag:

ObjectName Property

ObjectName is the global identity of the message that is used to judge and differentiate different message types in various scenarios.

Cloud system built-in message naming specification is: rc:xxxmsg (content class message) or RC:XXXNTF (notification class message) or Rc:xxxsts (status class message), you need to be aware of the custom message, do not start with "RC:" To avoid the integration of the cloud system built-in messages ObjectName duplicate name.
Messagetag annotation Types

The Messagetag defines the behavior and performance of the message on the client. The first parameter of Messagetag is the ObjectName of the message, the second parameter defines the message behavior status identifier, which can be performed or evaluated:

Enumeration value Description
Messagetag.none is a null value and does not represent any meaning.
Messagetag.iscounted indicates that after the client receives the message, the unread message count (1 increase in the number of unread messages) should be set for all content messages. Message count is not supported for non-content class messages.
Messagetag.ispersisted indicates that the client receives the message, stores it, and can then query through the interface.
Message type message base class message behavior status ID
Content class Message Messagecontent messagetag.ispersisted | messagetag.iscounted
Notification class message Notificationmessage messagetag.ispersisted
Status class message StatusMessage Messagetag.none
To implement a custom message, you need to implement a custommessagecontent inheritance rcmessagecontent that implements your custom data in your custom message content class. The base class Rcmessagecontent declaration conforms to the Rcmessagecoding protocol and the Rcmessagepersistentcompatible protocol, so you must implement the following functions:

@protocol rcmessagecoding <NSObject>
@required
/**
Encoding converts the current object into JSON data
@return encoded JSON data
*/
-(NSData *) encode;
/**
Sets the current instance based on the given JSON data
@param data-Incoming JSON data
*/
-(void) Decodewithdata: (NSData *) data;

/**
The message name should be returned, which requires a platform consistency
@return Message body Name
*/
+ (NSString *) getobjectname;

@end


/**
@protocol rcmessagepersistentcompatible
@discussion Rcmessagecontent has implemented this protocol method, derived classes need to overwrite the implementation to their own needs,
*/
@protocol rcmessagepersistentcompatible <NSObject>
@required
/**
Returns the identity of class object persistence that follows this Protocol

@return Return Persistent Set identity
@discussion default implementation returns @const (Messagepersistent_ispersisted | messagepersistent_iscounted)
*/
+ (rcmessagepersistent) Persistentflag;
@end
This gives an example of the definition of a literal message, which is also inherited from Rcmessagecontent.

#import "RCTextMessage.h"
#import "RCUtilities.h"
#import "RCJSONConverter.h"
#import "RCCommonDefine.h"

@implementation Rctextmessage

+ (Instancetype) messagewithcontent: (NSString *) content {
Rctextmessage *text = [[Rctextmessage alloc] init];
if (text) {
text.content = content;
}
return text;
}

+ (rcmessagepersistent) Persistentflag {
Return (messagepersistent_ispersisted | messagepersistent_iscounted);
}

#pragma mark-rcmessagecoding delegate Methods

-(NSData *) encode {

Nsmutabledictionary *datadict=[nsmutabledictionary Dictionary];
[Datadict setObject:self.content forkey:@ "content"];
if (Self.extra) {
[Datadict setObject:self.extra forkey:@ "Extra"];
}
nsdictionary* datadict = [nsdictionary dictionaryWithObjectsAndKeys:self.content, @ "content", nil];
NSData *data = [Nsjsonserialization datawithjsonobject:datadict
Options:kniloptions
Error:nil];
return data;
}

-(void) Decodewithdata: (NSData *) Data {

    if (!data) {
        return;
   
    nsdictionary *json = [Nsjsonserialization jsonobjectwithdata:data
                                                            options:kniloptions
                                                              Error:&__error];

if (JSON) {
self.content = json[@ "Content"];
Self.extra = json[@ "Extra"];
}
}

+ (NSString *) Getobjectname {
return rctextmessagetypeidentifier;
}

@end
Registering a custom message
After the custom message is completed, you need to register to the system message type and the system will treat your custom message as if it were other messages. The time to register is after the call to Init, before connect. The following is the code for System message registration, please refer to.

[Self registerMessageType:RCTextMessage.class];
[Self registerMessageType:RCImageMessage.class];
[Self registerMessageType:RCVoiceMessage.class];
Above, you have completed the definition of the custom message, the sender receives this custom message, then you will be concerned about the presentation of custom messages, please read the next section of "Message presentation customization".

Imkit Architecture
Imkit interface components are a core feature of cloud-based products, and developers use Imkit without having to develop their own interfaces from scratch, and simply integrate them into your App product with a short code. The most complex features of the conversation list, chat window, message Content selection, emoji gallery, friend selection, and session settings, the cloud Imkit has been done for you. For a quick start, after you start the chat session list, all the interface clouds have been completed for you.

For users who are further enriched with custom requirements, the cloud Imkit supports the customization of session lists, chat windows, input boxes, and message content, and you can accomplish your own unique interface by inheriting and rewriting the interface.

Image
Cloud IOS Imkit Architecture
Imkit has two main interfaces, a conversation list (conversation list) and a session (conversation), in the implementation of the Cloud Imkit, the former is a uitableview, the latter is a uicollectionview.

In the session interface, each of these messages is a Uicollectionviewcell that supports automatic layout. At present, the cloud for developers to achieve the five common content of the message interface display: text, images, voice, graphics, location, related interfaces are also all open, you can also inherit Rcmessagecell to customize their own message presentation.

Rcconversationmodel is a session model that contains data for each specific session, and Rcmessagemodel is a message model that contains data for each specific message that is open to the App to access and use according to its own needs.

Rcconversationlistviewcontroller and Rcconversationviewcontroller are inherited from Rcbaseviewcontroller, and you can inherit them and implement your own custom session list

Chat Cloud Newsletter

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.