preface

Client routing is developed to manage module communication inside the client, and to provide a channel for the external client to communicate with the client.

The purpose of internal communication is to decouple the dependencies between modules, avoid the unnecessary maintenance work caused by the introduction of modules, and also solve the problem of communication between modules where cyclic dependencies may occur. At the same time, the introduction of routing scheme can better maintain and manage the API module.

The purpose of external communication is to create a common client communication protocol for the product, such as switching from push to user chat interface, arousing the client from the web page of the browser, opening the specified content page, and providing Native capabilities for the H5 end of the small program. External protocols can unify the communication apis of clients on the entire platform and the capabilities of clients on different platforms (such as Win, Mac, Android, and iOS).

Architecture design

Performer Reflection Jump Management Module

Based on the implementation idea of CTMediator, the OC Runtime mechanism was used to call function generation dynamically to solve the problem of module dependence.

Make a function call with TargetClassName (String), SeletorName (String), Params (Map), UserInfo (Map), Callback (Block), and asynchronous call with Callback.

Before calling, you need to check the parameters of the function. After the validation, use the NSInvocation to assemble the function call.

Note that when the return value from the NSInvocation is received, the reference count for the return value needs to be managed manually.

Routing RuleModel

Referring to traditional URL rules, the basic information of the route is abstracted.

Scheme ://host/path? arg1=aa&arg2=bb

Such as: Pluto: / / Charon/im/session? id=123&name=ginhoor

Scheme: Pluto, host: Charon, path: /im/session, params: {id:123,name:ginhoor}

To make it easier for clients to use, add specific parameters (different platforms use different fields) :

  1. VcClassName, the name of the iOS view controller class.
  2. ParamsClassName, the class name of the view controller parameter sink.
  3. OpenType, view opening mode (modal open or navigator open, with or without animation).
  4. IsPrivate: specifies whether this rule is used externally. If False, only internal client requests can succeed.

To make the rule more readable, several parameters are added:

  1. Demo, fill in the URL string for the demo.
  2. Remark, fill in the string that describes the function of the rule.

As follows, these rules are finally processed into JSON files for unified management and maintenance.

{
    "version":"1.0.0"."rules":[
        {
            "iOSVCClassName":"MCGlobalCategorySearchVC"."iOSParamsClassName":"MCGlobalCategorySearchVCInputParams"."params": {"type":1."keywords":"kw"."fromVC":"IMSessionVC"
            },
            "host":"route"."path":"/home/category_search"."demo":"daqun://route/home/category_search? type=1&keywords=kw&fromVC=IMSessionVC"."isPrivate":0."remark":"MCGlobalCategorySearchVC"}}]Copy the code

Rule Configuration

Maintain and manage the configuration of routing rules on the client.

  1. Client routing and HTTP Host whitelist
  2. Whitelist of public domain Scheme and private domain Scheme
  3. The Scheme domain of the public domain used by the client to generate route urls and the Host of the client route.

When the client starts, the local EMBEDDED JSON routing rules are loaded to the client. In addition, a configuration delivery scheme needs to be provided for the server. When the server pushes new routing rules, the local routing rules need to be updated.

Finally, you need to create indexes for different fields of routing rules to optimize the route resolution time.

URL Jump Management module (Open URL)

Urls are divided into three types: local urls generated by the client and remote urls generated by ways other than the client. These two types of urls can be controlled through isPrivate in the routing rule. The last one is a special HTTP URL. It is a pure HTTP request link, but except Scheme and Host, it can correspond to the routing rules in the configuration. It is also a valid URL.

If using the client routing URL generated qr code, the user when WeChat swept the qr code, which is not installed on your phone our client, then the URL awakens the action will fail, but if you use the URL of the page, WeChat swept qr code, transit page will determine whether can arouse our client directly, if not, You can guide the user to download.

Therefore, the HTTP URL is used to generate the QR code for service sharing. At the same time, the URL maintenance rules of the transfer page are compatible with the routing rules of the client, so as to achieve maximum reuse.

The reflection module can be used to communicate with each other (for example, to log out of an IM user or jump to the session interface). The URL string can also be assembled according to routing rules, which is transmitted by the service module and then parsed by the routing module (such as sharing the routing address through card message forwarding H5).

Finally, if the URL fails to pass the validity check, the routing module will call the OpenURL of the system to take over the subsequent operations.