IOS Swift Project IM implementation, from long connection to data stream parsing socket

Source: Internet
Author: User

IOS Swift Project IM implementation, from long-connect to the underlying data analysis socket

A: Project introduction: Last year began to take over a state-owned enterprise mobile project, the demand for the project is to achieve im Instant messaging capabilities.

* The first edition features include:

1. approval of the company's business, quotation and award approval process;

2. single chat and group chat (lead daily work exchange);

3. Chat messages include support for text-like messages, languages, pictures, and language support across platforms;

4. the network data transmission of the message realizes the encryption compress processing, guarantees the data safe transmission;

* Features of the project:

Because of the project business: approval, quotation and the specificity of the award approval process, IM implementation can not be applied to third parties, such as: Aurora push, etc., so it is necessary to implement processing socket bottom data to solve the planing.

Second: TheiOS IM feature is implemented using the TCP request protocol of the socket, it is recommended to use a third party sdk:cocoaasyncsocket

Cocoaasyncsocket:oc version, is the application of GCD write asynchronous processing, interested can go to see the original code

Three: Cocoaasyncsocket

Pod ' cocoaasyncsocket '//Can be integrated via pod

Four: Cocoaasyncsocket Init and delegate

----INIT analysis------

----Delegate analysis------

* Implementation of Cocoaasyncsocket 4 agents to handle socket communication data, the following analysis of the role of each agent:

1.-(void) Socket: (Gcdasyncsocket *) sock didconnecttohost: (NSString *) host port: (uint16_t) port;

    • After successfully connecting to the server, you need to send a heartbeat mechanism that maintains a long connection, implemented in a successful callback method:

    • Send the heartbeat mechanism at the same time, initiate account login verification

2.-(void) Socketdiddisconnect: (Gcdasyncsocket *) sock witherror: (Nullable nserror *) err;

* With success, there must be failure:

    • Failure requires immediate updating of the connection state, ensuring the user's security (non-rollover) of the operation in the event of a connection failure, and UI hints

The status of the update includes: 1: Network status, 2: User Data Sync status, 3: Heartbeat interval, 4: Forced reconnection

Note: (a robust and well-performing IM system, the factors to consider are important)

    • Reconnect, in this project is always responsible for general Wang's advice, once disconnected to give 6 times the reconnection opportunity

Note: This project is always responsible for Wang, is the backstage born, the whole project backstage (report EQ system, ERP system) system has the Wang Total building, backstage of the entire IM framework is also the Wang total integration, maintenance and improvement,

The position of state-owned enterprises is highly and technically comprehensive indeed admired.

3.-(void) Socket: (Gcdasyncsocket *) sock Didwritedatawithtag: (long) tag;

    • Gcdasyncsocket packet sent, involving 3 states, analyzed as follows:
    • First: packets are sent only, no callback packets are accepted
      the second type: packets have been sent, to determine that there is packet acceptance
      The Third Kind: The packet has the send, is not sure has the packet accepts
    • Analysis: The receipt of the Gcdasyncsocket packet requires an advance subscription (providing thread task processing), and if the supplied subscription task is exhausted, then gcdasyncsocket does not process and callback the newly received message data. Didwritedata the implementation of this method is to deal with the compatibility of the 3 states, to ensure that new messages can be processed in a timely manner.

4.-(void) Socket: (Gcdasyncsocket *) sock didreaddata: (NSData *) data withtag: (long) tag;

    • When the new message is successfully subscribed, the data is parsed and the data types are divided into:

Instruction message (SYC instruction), heartbeat message, init message, and Invoke (get) message;

    • Didreaddata: The tag field is called back in the method, but we can't differentiate it by tag, tag is just the tag of callback thread, must be the type of data to distinguish

V: Socke Summary

* The socket connection layer of the cocoaasyncsocket is almost there, and the regulations listed above are the places that require great attention.

* The IM Connectivity layer also has a very important place: need to monitor the network status of the project, the change of network status needs to be handled,

4G network needs to remind the user, the virtual network needs to optimize the processing of IM tasks, in the case of disconnection reconnection to prioritize network status,

These are performance optimizations for IM systems that provide a resilient foundation for project iterations.

Note: Next: IOS Swift project IM implementation, Payload Data packaging and parsing from long connections to data flow analytic analysis.

Related Article

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: 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.