CN117194074A - Method and device for realizing H5 and native communication and electronic equipment - Google Patents

Method and device for realizing H5 and native communication and electronic equipment Download PDF

Info

Publication number
CN117194074A
CN117194074A CN202311119283.7A CN202311119283A CN117194074A CN 117194074 A CN117194074 A CN 117194074A CN 202311119283 A CN202311119283 A CN 202311119283A CN 117194074 A CN117194074 A CN 117194074A
Authority
CN
China
Prior art keywords
communication
target instruction
callback function
function
instruction
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Pending
Application number
CN202311119283.7A
Other languages
Chinese (zh)
Inventor
陈润荣
英杰
胡仲强
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
China Construction Bank Corp
CCB Finetech Co Ltd
Original Assignee
China Construction Bank Corp
CCB Finetech Co Ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by China Construction Bank Corp, CCB Finetech Co Ltd filed Critical China Construction Bank Corp
Priority to CN202311119283.7A priority Critical patent/CN117194074A/en
Publication of CN117194074A publication Critical patent/CN117194074A/en
Pending legal-status Critical Current

Links

Landscapes

  • Stored Programmes (AREA)

Abstract

The invention discloses a method, a device and electronic equipment for realizing H5 and native communication, and relates to the technical field of mobile interconnection. One embodiment of the method comprises the following steps: responding to a call instruction transmitted by an H5 end, wherein the call instruction carries the instruction name and the entry of a target instruction, a communication callback function and an execution function corresponding to the target instruction are created, the communication callback function is mounted on a global object in the execution function, and a resolve method is used as the entry of the execution function; initializing a Promise instance, and transmitting an execution function into the Promise instance; the original end is controlled to execute the target instruction, and the communication callback function is called, so that the execution result of the target instruction is transmitted to the communication callback function; invoking a resolve method and taking an execution result of a target instruction as a parameter of the resolve method so that an H5 end obtains the execution result of the target instruction; destroy the Promise instance. The implementation mode can solve the technical problems that codes are bloated, the structure is not clear, and the logic of the codes is difficult to maintain.

Description

Method and device for realizing H5 and native communication and electronic equipment
Technical Field
The present invention relates to the field of mobile interconnection technologies, and in particular, to a method, an apparatus, and an electronic device for implementing H5 and native communications.
Background
When the application program (APP) is developed by using the native and H5 hybrid application, the native terminal can provide the capability of calling the native interface for the H5 terminal through the jsbridge communication bridge so as to achieve the effect that the H5 terminal and the native terminal can communicate. After the H5 end calls the native interface, the H5 end asynchronously acquires the communicated data by defining a communication callback function.
In the JavaScript world, code is executed in a single thread. For this reason, time-consuming operations in JavaScript, such as web operations, browser events, H5-side and native-side communications, etc., all need to be performed asynchronously. Asynchronous refers to the fact that when some time-consuming operation is performed, the result is not returned immediately, the following operation is not blocked, and once the time-consuming operation is completed, the function of which the result needs to be called is notified immediately for subsequent processing. Before the ECMAScript 6 standard appears, the JavaScript asynchronous programming scheme is usually implemented by using an incoming communication callback function to asynchronously receive the execution result, while after the ECMAScript 6 standard appears, the JavaScript provides the promiscuous asynchronous programming scheme.
In the process of implementing the present invention, the inventor finds that at least the following problems exist in the prior art:
As the volume of items and the amount of code increase, receiving communication data based on the communication callback function may make the code become bloated, unclear in structure, and difficult to maintain.
Disclosure of Invention
In view of this, the embodiment of the invention provides a method, a device and an electronic device for implementing H5 and native communication, so as to solve the technical problems of code bloated, unclear structure and difficult maintenance of code logic.
To achieve the above object, according to an aspect of an embodiment of the present invention, there is provided a method for implementing H5 and native communications, including:
responding to a call instruction transmitted by an H5 end, wherein the call instruction carries an instruction name and an entry of a target instruction, a communication callback function and an execution function corresponding to the target instruction are created, the communication callback function is mounted on a global object in the execution function, and a resolve method is used as the entry of the execution function;
initializing a Promise instance, and transmitting the execution function into the Promise instance;
the original terminal is controlled to execute the target instruction, and the communication callback function is called, so that an execution result of the target instruction is transmitted to the communication callback function;
Invoking the resolve method and taking the execution result of the target instruction as the parameter of the resolve method so that the H5 end obtains the execution result of the target instruction;
destroying the Promidase instance.
Optionally, creating a communication callback function and an execution function corresponding to the target instruction, in the execution function, mounting the communication callback function on a global object, and taking a resolve method as an entry of the execution function, including:
creating a communication callback function corresponding to the target instruction, naming the communication callback function, and taking the communication result of the H5 end and the original end as an entry of the communication callback function;
and creating an execution function, wherein in the execution function, the communication callback function is mounted on a global object, and a resolve method is used as an entry of the execution function.
Optionally, naming the communication callback function includes:
and combining the instruction name of the target instruction, the current time stamp and the random number generated by adopting the current time stamp into the name of the communication callback function.
Optionally, the communication callback function is installed on a global object, including:
And mounting the communication callback function on the window global object, and adopting the name of the communication callback function as a key value of the window global object.
Optionally, controlling the native terminal to execute the target instruction and call the communication callback function, so as to transfer an execution result of the target instruction to the communication callback function, including:
and transmitting the instruction name of the target instruction, the entry of the target instruction and the name of the communication callback function to a native terminal, so that the native terminal receives the instruction name of the target instruction, the entry of the target instruction and the name of the communication callback function, executes the target instruction, and calls the communication callback function, thereby transmitting the execution result of the target instruction to the communication callback function.
Optionally, calling the resolve method and taking the execution result of the target instruction as an entry of the resolve method, so that the H5 end obtains the execution result of the target instruction, including:
and calling the resolve method, taking the execution result of the target instruction as the parameter of the resolve method, so that the H5 end obtains the execution result of the target instruction, and simultaneously modifying the running state of the Promise instance from an initial state to a successful state.
Optionally, destroying the Promise instance includes:
deleting the communication callback function mounted on the global object by using a method for deleting the object attribute;
and destroying the Promise instance with the running state being a successful state.
In addition, according to another aspect of the embodiment of the present invention, there is provided an apparatus for implementing H5 and native communications, including:
the creation module is used for responding to a call instruction transmitted by an H5 end, wherein the call instruction carries the instruction name and the entry of a target instruction, creating a communication callback function corresponding to the target instruction, naming the communication callback function, and taking the communication result of the H5 end and a primary end as the entry of the communication callback function;
the instance module is used for initializing a Promise instance and transmitting the execution function into the Promise instance;
the execution module is used for controlling the original end to execute the target instruction and calling the communication callback function so as to transmit the execution result of the target instruction to the communication callback function;
the receiving module is used for calling the resolve method and taking the execution result of the target instruction as the parameter of the resolve method so that the H5 end can obtain the execution result of the target instruction;
And the destroying module is used for destroying the Promise instance.
Optionally, the creating module is further configured to:
creating a communication callback function corresponding to the target instruction, naming the communication callback function, and taking the communication result of the H5 end and the original end as an entry of the communication callback function;
and creating an execution function, wherein in the execution function, the communication callback function is mounted on a global object, and a resolve method is used as an entry of the execution function.
Optionally, the creating module is further configured to:
and combining the instruction name of the target instruction, the current time stamp and the random number generated by adopting the current time stamp into the name of the communication callback function.
Optionally, the creating module is further configured to:
and mounting the communication callback function on the window global object, and adopting the name of the communication callback function as a key value of the window global object.
Optionally, the execution module is further configured to:
and transmitting the instruction name of the target instruction, the entry of the target instruction and the name of the communication callback function to a native terminal, so that the native terminal receives the instruction name of the target instruction, the entry of the target instruction and the name of the communication callback function, executes the target instruction, and calls the communication callback function, thereby transmitting the execution result of the target instruction to the communication callback function.
Optionally, the receiving module is further configured to:
and calling the resolve method, taking the execution result of the target instruction as the parameter of the resolve method, so that the H5 end obtains the execution result of the target instruction, and simultaneously modifying the running state of the Promise instance from an initial state to a successful state.
Optionally, the destruction module is further configured to:
deleting the communication callback function mounted on the global object by using a method for deleting the object attribute;
and destroying the Promise instance with the running state being a successful state.
According to another aspect of an embodiment of the present invention, there is also provided an electronic device including:
one or more processors;
storage means for storing one or more programs,
the one or more processors implement the method of any of the embodiments described above when the one or more programs are executed by the one or more processors.
According to another aspect of an embodiment of the present invention, there is also provided a computer readable medium having stored thereon a computer program which, when executed by a processor, implements the method according to any of the embodiments described above.
According to another aspect of embodiments of the present invention, there is also provided a computer program product comprising a computer program which, when executed by a processor, implements the method according to any of the embodiments described above.
One embodiment of the above invention has the following advantages or benefits: because the communication callback function and the execution function corresponding to the creation target instruction are adopted, the communication callback function is mounted on the global object in the execution function, the resolve method is used as the parameter of the execution function, then the Promise instance is initialized, and the execution function is transmitted into the Promise instance, the technical problems of code swelling, unclear structure and difficulty in maintenance of code logic in the prior art are solved. The embodiment of the invention has the advantages that the communication result data of the H5 end and the original end can be asynchronously acquired only by the instruction name and the input parameters of the input target instruction, the problems of creating and destroying callback functions are not required to be concerned, the programming convenience can be greatly improved, the programming logic of codes can be obviously improved, the whole code quantity is greatly reduced, the code structure and the code logic become very clear and robust, and the codes are easy to maintain.
Further effects of the above-described non-conventional alternatives are described below in connection with the embodiments.
Drawings
In order to more clearly illustrate the embodiments of the invention or the technical solutions in the prior art, the drawings that are required in the embodiments or the description of the prior art will be briefly described, it being obvious that the drawings in the following description are only some embodiments of the invention, and that other drawings may be obtained according to these drawings without inventive effort for a person skilled in the art. Wherein:
FIG. 1 is a flow chart of a method of implementing H5 and native communications according to an embodiment of the invention;
FIG. 2 is a flow chart of a method of implementing H5 and native communications in accordance with a referenceable embodiment of the invention;
FIG. 3 is a flow chart of a method of implementing H5 and native communications in accordance with another referenceable embodiment of the invention;
FIG. 4 is a flow chart of a method of implementing H5 and native communications in accordance with yet another referenceable embodiment of the invention;
FIG. 5 is a schematic diagram of an apparatus for implementing H5 and native communications in accordance with an embodiment of the invention;
FIG. 6 is an exemplary system architecture diagram in which embodiments of the present invention may be applied;
fig. 7 is a schematic diagram of a computer system suitable for use in implementing an embodiment of the invention.
Detailed Description
Exemplary embodiments of the present invention will now be described with reference to the accompanying drawings, in which various details of the embodiments of the present invention are included to facilitate understanding, and are to be considered merely exemplary. Accordingly, those of ordinary skill in the art will recognize that various changes and modifications of the embodiments described herein can be made without departing from the scope and spirit of the invention. Also, descriptions of well-known functions and constructions are omitted in the following description for clarity and conciseness.
In the technical scheme of the invention, the aspects of acquisition, analysis, use, transmission, storage and the like of the related user personal information all meet the requirements of related laws and regulations, are used for legal and reasonable purposes, are not shared, leaked or sold outside the aspects of legal use and the like, and are subjected to supervision and management of a supervision department. Necessary measures should be taken for the personal information of the user to prevent illegal access to such personal information data, ensure that personnel having access to the personal information data comply with the regulations of the relevant laws and regulations, and ensure the personal information of the user. Once these user personal information data are no longer needed, the risk should be minimized by limiting or even prohibiting the data collection and/or deletion.
User privacy is protected, when applicable, by de-identifying the data, including in some related applications, such as by removing a particular identifier (e.g., name, account number, date of birth, etc.), controlling the amount or specificity of stored data, controlling how the data is stored, and/or other methods.
Fig. 1 is a flow chart of a method of implementing H5 and native communications according to an embodiment of the invention. As an embodiment of the present invention, as shown in fig. 1, the method for implementing H5 and native communication may include:
Step 101, responding to a call instruction transmitted by an H5 end, wherein the call instruction carries an instruction name and an entry of a target instruction, a communication callback function and an execution function corresponding to the target instruction are created, the communication callback function is mounted on a global object in the execution function, and a resolve method is used as the entry of the execution function.
When developing APP hybrid application, the communication method of the original end and the H5 end is realized based on jsbridge. The embodiment of the invention designs an encapsulation function called callRequestNative, which is based on a javascript Promise mechanism and is used for uniformly managing and scheduling a method for embedding a jsbridge communication bridge.
H5 refers to HTML5, which is a language description way to construct Web content. jsbridge is a means of communicating between the webview side and the native side, where the webview can invoke native capabilities through jsb and native can also execute some logic on the webview through jsb. Inside jsbridge, the full instruction name of the native side communicating with the H5 side is predefined, and each instruction, which we call action, is defined. The Native refers to an APP Native terminal, and is generally divided into an IOS terminal and an Android terminal.
After the H5 end transmits the target instruction and the input parameters of the target instruction into the packaging function, the packaging function executes steps 101-105, so that the communication result data of the H5 end and the original end can be asynchronously obtained.
Optionally, creating a communication callback function and an execution function corresponding to the target instruction, in the execution function, mounting the communication callback function on a global object, and taking a resolve method as an entry of the execution function, including: creating a communication callback function corresponding to the target instruction, naming the communication callback function, and taking the communication result data of the H5 end and the original end as the reference of the communication callback function; and creating an execution function, wherein in the execution function, the communication callback function is mounted on a global object, and a resolve method is used as an entry of the execution function. After the H5 end transmits the instruction name of the target instruction and the entering parameter of the target instruction into the packaging function, the packaging function creates a communication callback function corresponding to the target instruction and names the communication callback function in response to receiving the instruction name of the target instruction and the entering parameter of the target instruction, the uniqueness of the name of the communication callback function needs to be ensured, the communication result data of the H5 end and the original end are used as the entering parameter of the communication callback function, then an execution function is created, and in the execution function, the communication callback function is mounted on a global object, and a reserve method is used as the entering parameter of the execution function. The call-back function is used for receiving communication result data of the H5 end and the original end.
Optionally, naming the communication callback function includes: and combining the instruction name of the target instruction, the current time stamp and the random number generated by adopting the current time stamp into the name of the communication callback function. At the H5 end, for each communication operation with the native end, that is, for each action, a javascript communication callback function with an independent name needs to be defined to receive the communication data of the native end, and each name cannot be repeated, and the repeated name can cause a received data error. In order to realize that the names of the communication callback functions are not repeated, the embodiment of the invention defines the names as action instruction names, time stamps and random numbers generated by using the time stamps. The action instruction name is a specific instruction name defined by a jsbridge communication bridge SDK; the time stamp is acquired by calling new Date (). GetTime () of the API of javascript; the random number is calculated and obtained by calling the Math.random () method of javascript.
Optionally, the communication callback function is installed on a global object, including: and mounting the communication callback function on the window global object, and adopting the name of the communication callback function as a key value of the window global object. The executing function is to mount the communication callback function on the window global object, the communication callback function is packaged in the executing function, and after the name of the communication callback function is transferred to the native terminal, the native terminal can return communication result data to the H5 terminal by calling the communication callback function. The method for saving has the capability of changing the state of the Promise instance, the method for saving is immediately called after the communication callback function defined by the H5 end is called by the original end, and the communication result data is used as the input parameter of the method for saving, so that the communication result data can be asynchronously obtained at the place of calling the packaging function through the operation. Therefore, the encapsulation function provides the instruction name of the action and the entry of the action as the entry of the encapsulation function, so that the asynchronous acquisition of the communication result data of the H5 and the native communication can be realized.
Step 102, initializing a Promise instance, and transmitting the execution function into the Promise instance.
Promise is a class provided by ECMAScript 6, a new solution for asynchronous programming in javascript. Among them, promise has three operating states: initial state (pending), successful state (full filter), failed state (reject)
After the step 101 is executed, the encapsulation function initializes a promiscuous instance of javascript, and the encapsulation function uses the promiscuous instance as a return result of the encapsulation function, so the encapsulation function has the capability of waiting for the communication result of the primary end to return and counting to the outside, that is, the encapsulation function has the capability of executing asynchronously.
And step 103, controlling the original terminal to execute the target instruction, and calling the communication callback function, so that an execution result of the target instruction is transmitted to the communication callback function.
In the step, the H5 end transmits the name of the action instruction to be called, the participation of the action contract and the name of the communication callback function to the original end, the original end executes the action and calls the communication callback function, and the execution result (namely the communication result data) of the action is transmitted to the communication callback function.
Optionally, step 103 may include: and transmitting the instruction name of the target instruction, the entry of the target instruction and the name of the communication callback function to a native terminal, so that the native terminal receives the instruction name of the target instruction, the entry of the target instruction and the name of the communication callback function, executes the target instruction, and calls the communication callback function, thereby transmitting the execution result of the target instruction to the communication callback function.
After the native terminal receives the name of the action instruction, the participation of the action convention and the name of the communication callback function which are required to be called by the H5 terminal, the execution result of the action instruction is obtained after the execution of the action instruction is completed by the native terminal, the native terminal calls the communication callback function which is mounted on the window global object by the H5 terminal, the execution result of the action instruction is transmitted to the communication callback function in the form of parameters, and the parameter value of the participation of the communication callback function defined in the step 101 is the execution result of the action instruction.
And 104, calling the resolve method and taking the execution result of the target instruction as the parameter of the resolve method so that the H5 end obtains the execution result of the target instruction.
After the communication callback function receives the execution result of the action instruction, the encapsulation function calls the reserve method and takes the execution result of the action as an entry of the reserve method, so that the H5 end obtains the execution result of the action.
Optionally, step 104 may include: and calling the resolve method, taking the execution result of the target instruction as the parameter of the resolve method, so that the H5 end obtains the execution result of the target instruction, and simultaneously modifying the running state of the Promise instance from an initial state to a successful state. After the communication callback function receives the execution result of the action instruction, the encapsulation function calls the reserve method and takes the execution result of the action as the parameter of the reserve method, so that the H5 end obtains the execution result of the target instruction, and the running state of the Promise instance is modified from an initial state to a successful state.
And step 105, destroying the Promise instance.
And after the H5 end obtains the execution result of the target instruction, automatically destroying the Promise instance.
According to the various embodiments described above, it can be seen that the technical means of creating the communication callback function and the execution function corresponding to the target instruction, in the execution function, mounting the communication callback function on the global object, taking the resolve method as the parameter of the execution function, initializing the promiscuous instance, and transferring the execution function into the promiscuous instance are provided, so that the technical problems of code bloated, unclear structure and difficult maintenance of code logic in the prior art are solved. The embodiment of the invention has the advantages that the communication result data of the H5 end and the original end can be asynchronously acquired only by the instruction name and the input parameters of the input target instruction, the problems of creating and destroying callback functions are not required to be concerned, the programming convenience can be greatly improved, the programming logic of codes can be obviously improved, the whole code quantity is greatly reduced, the code structure and the code logic become very clear and robust, and the codes are easy to maintain.
Fig. 2 is a flow chart of a method of implementing H5 and native communications in accordance with a referenceable embodiment of the invention. As yet another embodiment of the present invention, as shown in fig. 2, the method for implementing H5 and native communications may include:
step 201, responding to a call instruction transmitted by an H5 end, wherein the call instruction carries an instruction name and an entry of a target instruction, creates a communication callback function corresponding to the target instruction, forms the instruction name of the target instruction, a current time stamp and a random number generated by adopting the current time stamp into the name of the communication callback function, and takes a communication result of the H5 end and a primary end as the entry of the communication callback function.
Step 202, creating an execution function, in which the communication callback function is mounted on a global object, and a resolve method is used as an entry of the execution function.
After the H5 end transmits the instruction name of the target instruction and the entering parameter of the target instruction into the packaging function, the packaging function creates a communication callback function corresponding to the target instruction and names the communication callback function in response to receiving the instruction name of the target instruction and the entering parameter of the target instruction, the uniqueness of the name of the communication callback function needs to be ensured, the communication result data of the H5 end and the original end are used as the entering parameter of the communication callback function, then an execution function is created, and in the execution function, the communication callback function is mounted on a global object, and a reserve method is used as the entering parameter of the execution function. The call-back function is used for receiving communication result data of the H5 end and the original end. In order to realize that the names of the communication callback functions are not repeated, the embodiment of the invention defines the names as action instruction names, time stamps and random numbers generated by using the time stamps. The action instruction name is a specific instruction name defined by a jsbridge communication bridge SDK; the time stamp is acquired by calling new Date (). GetTime () of the API of javascript; the random number is calculated and obtained by calling the Math.random () method of javascript.
It should be noted that, in some scenarios, the same action may be called concurrently, if the name of the action instruction is simply relied on, the uniqueness of the name of the communication callback function in such scenarios cannot be guaranteed, so that the embodiment of the invention splices the timestamp and the random number on the basis of the name of the action instruction, thereby guaranteeing the uniqueness of the name of the communication callback function.
Step 203, initializing a Promise instance, and transmitting the execution function into the Promise instance.
When the Promise instance is initialized, an execution function is required to be transmitted, the execution function receives a resolve method provided by the Promise instance, the resolve method can change the running state of the Promise instance, and the running state of the Promise instance is modified from an initial state to a successful state.
And 204, controlling the original end to execute the target instruction, and calling the communication callback function, so that an execution result of the target instruction is transmitted to the communication callback function.
The H5 end transmits the instruction name of the action to be called, the admission of the action convention and the name of the communication callback function to the original end, the original end executes the action and calls the communication callback function, and the execution result (namely the communication result data) of the action is transmitted to the communication callback function.
Step 205, calling the resolve method and taking the execution result of the target instruction as the parameter of the resolve method, so that the H5 end obtains the execution result of the target instruction.
After the communication callback function receives the execution result of the action instruction, the encapsulation function calls the reserve method and takes the execution result of the action as an entry of the reserve method, so that the H5 end obtains the execution result of the action.
And 206, destroying the Promise instance.
In addition, in the embodiment of the present invention, the implementation of the method for implementing H5 and native communication is described in detail in the above method for implementing H5 and native communication, so that the description is not repeated here.
Fig. 3 is a flow chart of a method of implementing H5 and native communications in accordance with another referenceable embodiment of the invention. As another embodiment of the present invention, as shown in fig. 3, the method for implementing H5 and native communication may include:
step 301, responding to a call instruction transmitted by an H5 end, wherein the call instruction carries an instruction name and an entry of a target instruction, creates a communication callback function corresponding to the target instruction, forms the instruction name of the target instruction, a current timestamp and a random number generated by adopting the current timestamp into the name of the communication callback function, and takes a communication result of the H5 end and a primary end as the entry of the communication callback function.
Step 302, creating an execution function, in which the communication callback function is mounted on a window global object, the name of the communication callback function is adopted as a key value of the window global object, and a resolve method is adopted as an entry of the execution function.
Step 303, initializing a promiscuous instance and transmitting the execution function into the promiscuous instance.
Step 304, transmitting the instruction name of the target instruction and the entry of the target instruction and the name of the communication callback function to a native terminal, so that after the native terminal receives the instruction name of the target instruction and the entry of the target instruction and the name of the communication callback function, executing the target instruction, and calling the communication callback function, thereby transmitting the execution result of the target instruction to the communication callback function.
After the native terminal receives the name of the action instruction, the participation of the action convention and the name of the communication callback function which are required to be called by the H5 terminal, the native terminal firstly executes the action instruction, after the execution of the action instruction is completed, an execution result of the action instruction is obtained, then the native terminal calls the communication callback function which is mounted on the window global object by the H5 terminal, and the execution result of the action instruction is transmitted to the communication callback function in a parameter mode.
Step 305, calling the resolve method and taking the execution result of the target instruction as an entry of the resolve method, so that the H5 end obtains the execution result of the target instruction, and simultaneously, modifying the running state of the Promise instance from an initial state to a successful state.
And 306, deleting the communication callback function mounted on the global object by using a method for deleting the object attribute.
Step 307, destroy the Promise instance whose running state is successful.
After the H5 end obtains the execution result of the target instruction, the communication callback function mounted on the window global object is deleted by using a method (delete operator) for deleting the object attribute by using javascript. Because the communication callback function is mounted on the window global object, after the H5 end obtains the execution result of the target instruction, the communication callback function mounted on the window global object is deleted, so that the pollution to the global variable and the memory consumption can be reduced, and the extra performance cost is not increased.
In addition, in another embodiment of the present invention, the implementation of the method for implementing H5 and native communication is described in detail in the foregoing method for implementing H5 and native communication, so that the description is not repeated here.
Fig. 4 is a flow chart of a method of implementing H5 and native communications in accordance with yet another referenceable embodiment of the invention. As yet another embodiment of the present invention, as shown in fig. 4, the method for implementing H5 and native communications may include:
step 401, judging whether jsbridge has finished mounting at the H5 end; if not, go to step 402; if yes, go to step 403.
And step 402, delaying the judgment again.
When using jsbridge, it is first considered whether the jsbridge has completed mounting at the H5 end, and the method inside the jsbridge can be used only if the mounting is completed. The judgment of whether the jsbridge has completed mounting is confirmed by judging whether an entry class method window. Since the mounting of jsbridge is asynchronous, there may be situations where the H5 application is already running, but the jsbridge is not yet present. If the window. CCBBridge class does not exist, then the API for javascript needs to be passed: the setTimeout time delay is judged again, and the process is circulated until the jsbridge is successfully mounted, and then the jbridge can be used for realizing communication interaction between the primary end and the H5 end, namely, the communication channels between the primary end and the H5 end are opened.
Step 403, responding to a call instruction transmitted from an H5 end, where the call instruction carries an instruction name and an entry of a target instruction, creating a communication callback function and an execution function corresponding to the target instruction, in the execution function, mounting the communication callback function on a global object, and taking a resolve method as the entry of the execution function.
Step 404, initializing a promiscuous instance, and transmitting the execution function into the promiscuous instance.
And step 405, controlling the native terminal to execute the target instruction, and calling the communication callback function, so as to transfer the execution result of the target instruction to the communication callback function.
Step 406, calling the resolve method and taking the execution result of the target instruction as the parameter of the resolve method, so that the H5 end obtains the execution result of the target instruction.
Step 407, destroying the Promise instance.
In addition, in still another embodiment of the present invention, the implementation of the method for implementing H5 and native communication is described in detail in the above method for implementing H5 and native communication, so that the description is not repeated here.
Fig. 5 is a schematic diagram of an apparatus for implementing H5 and native communications in accordance with an embodiment of the invention. As shown in fig. 5, the apparatus 500 for implementing H5 communication with native communications includes a creation module 501, an instance module 502, an execution module 503, a receiving module 504, and a destruction module 505; the creation module 501 is configured to respond to a call instruction transmitted from an H5 end, where the call instruction carries an instruction name and an entry of a target instruction, create a communication callback function corresponding to the target instruction, name the communication callback function, and use a communication result of the H5 end and a primary end as an entry of the communication callback function; the instance module 502 is configured to initialize a promiscuous instance, and transfer the execution function into the promiscuous instance; the execution module 503 is configured to control the native terminal to execute the target instruction, and call the communication callback function, so as to transfer an execution result of the target instruction to the communication callback function; the receiving module 504 is configured to call the resolve method and take an execution result of the target instruction as an entry of the resolve method, so that the H5 end obtains the execution result of the target instruction; the destruction module 505 is configured to destroy the Promise instance.
Optionally, the creation module 501 is further configured to:
creating a communication callback function corresponding to the target instruction, naming the communication callback function, and taking the communication result of the H5 end and the original end as an entry of the communication callback function;
and creating an execution function, wherein in the execution function, the communication callback function is mounted on a global object, and a resolve method is used as an entry of the execution function.
Optionally, the creation module 501 is further configured to:
and combining the instruction name of the target instruction, the current time stamp and the random number generated by adopting the current time stamp into the name of the communication callback function.
Optionally, the creation module 501 is further configured to:
and mounting the communication callback function on the window global object, and adopting the name of the communication callback function as a key value of the window global object.
Optionally, the execution module 503 is further configured to:
and transmitting the instruction name of the target instruction, the entry of the target instruction and the name of the communication callback function to a native terminal, so that the native terminal receives the instruction name of the target instruction, the entry of the target instruction and the name of the communication callback function, executes the target instruction, and calls the communication callback function, thereby transmitting the execution result of the target instruction to the communication callback function.
Optionally, the receiving module 504 is further configured to:
and calling the resolve method, taking the execution result of the target instruction as the parameter of the resolve method, so that the H5 end obtains the execution result of the target instruction, and simultaneously modifying the running state of the Promise instance from an initial state to a successful state.
Optionally, the destruction module 505 is further configured to:
deleting the communication callback function mounted on the global object by using a method for deleting the object attribute;
and destroying the Promise instance with the running state being a successful state.
The implementation of the device for implementing H5 and native communication according to the present invention is described in detail in the method for implementing H5 and native communication described above, and thus the description thereof will not be repeated here.
Fig. 6 illustrates an exemplary system architecture 600 for a method of implementing H5-to-native communications or an apparatus implementing H5-to-native communications to which embodiments of the invention may be applied.
As shown in fig. 6, the system architecture 600 may include terminal devices 601, 602, 603, a network 604, and a server 605. The network 604 is used as a medium to provide communication links between the terminal devices 601, 602, 603 and the server 605. The network 604 may include various connection types, such as wired, wireless communication links, or fiber optic cables, among others.
A user may interact with the server 605 via the network 604 using the terminal devices 601, 602, 603 to receive or send messages, etc. Various communication client applications such as shopping class applications, web browser applications, search class applications, instant messaging tools, mailbox clients, social platform software, etc. (by way of example only) may be installed on the terminal devices 601, 602, 603.
The terminal devices 601, 602, 603 may be various electronic devices having a display screen and supporting web browsing, including but not limited to smartphones, tablets, laptop and desktop computers, and the like.
The server 605 may be a server providing various services, such as a background management server (by way of example only) providing support for shopping-type websites browsed by users using terminal devices 601, 602, 603. The background management server can analyze and other data such as the received article information inquiry request and feed back the processing result to the terminal equipment.
It should be noted that, the method for implementing H5 and native communication provided by the embodiment of the present invention is performed by the terminal devices 601, 602, 603, and accordingly, the apparatus for implementing H5 and native communication is provided in the terminal devices 601, 602, 603.
It should be understood that the number of terminal devices, networks and servers in fig. 6 is merely illustrative. There may be any number of terminal devices, networks, and servers, as desired for implementation.
Referring now to FIG. 7, there is illustrated a schematic diagram of a computer system 700 suitable for use in implementing an embodiment of the present invention. The terminal device shown in fig. 7 is only an example, and should not impose any limitation on the functions and the scope of use of the embodiment of the present invention.
As shown in fig. 7, the computer system 700 includes a Central Processing Unit (CPU) 701, which can perform various appropriate actions and processes according to a program stored in a Read Only Memory (ROM) 702 or a program loaded from a storage section 708 into a Random Access Memory (RAM) 703. In the RAM703, various programs and data required for the operation of the system 700 are also stored. The CPU 701, ROM 702, and RAM703 are connected to each other through a bus 704. An input/output (I/O) interface 705 is also connected to bus 704.
The following components are connected to the I/O interface 705: an input section 706 including a keyboard, a mouse, and the like; an output portion 707 including a Cathode Ray Tube (CRT), a Liquid Crystal Display (LCD), and the like, a speaker, and the like; a storage section 708 including a hard disk or the like; and a communication section 709 including a network interface card such as a LAN card, a modem, or the like. The communication section 709 performs communication processing via a network such as the internet. The drive 710 is also connected to the I/O interface 705 as needed. A removable medium 711 such as a magnetic disk, an optical disk, a magneto-optical disk, a semiconductor memory, or the like is mounted on the drive 710 as necessary, so that a computer program read therefrom is mounted into the storage section 708 as necessary.
In particular, according to embodiments of the present disclosure, the processes described above with reference to flowcharts may be implemented as computer software programs. For example, embodiments of the present disclosure include a computer program comprising a computer program embodied on a computer readable medium, the computer program comprising program code for performing the method shown in the flow chart. In such an embodiment, the computer program may be downloaded and installed from a network via the communication portion 709, and/or installed from the removable medium 711. The above-described functions defined in the system of the present invention are performed when the computer program is executed by a Central Processing Unit (CPU) 701.
The computer readable medium shown in the present invention may be a computer readable signal medium or a computer readable storage medium, or any combination of the two. The computer readable storage medium can be, for example, but not limited to, an electronic, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or a combination of any of the foregoing. More specific examples of the computer-readable storage medium may include, but are not limited to: an electrical connection having one or more wires, a portable computer diskette, a hard disk, a Random Access Memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disc read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the foregoing. In the context of this document, a computer readable storage medium may be any tangible medium that can contain, or store a program for use by or in connection with an instruction execution system, apparatus, or device. In the present invention, however, the computer-readable signal medium may include a data signal propagated in baseband or as part of a carrier wave, with the computer-readable program code embodied therein. Such a propagated data signal may take any of a variety of forms, including, but not limited to, electro-magnetic, optical, or any suitable combination of the foregoing. A computer readable signal medium may also be any computer readable medium that is not a computer readable storage medium and that can communicate, propagate, or transport a program for use by or in connection with an instruction execution system, apparatus, or device. Program code embodied on a computer readable medium may be transmitted using any appropriate medium, including but not limited to: wireless, wire, fiber optic cable, RF, etc., or any suitable combination of the foregoing.
The flowcharts and block diagrams in the figures illustrate the architecture, functionality, and operation of possible implementations of systems, methods and computer programs according to various embodiments of the present invention. In this regard, each block in the flowchart or block diagrams may represent a module, segment, or portion of code, which comprises one or more executable instructions for implementing the specified logical function(s). It should also be noted that, in some alternative implementations, the functions noted in the block may occur out of the order noted in the figures. For example, two blocks shown in succession may, in fact, be executed substantially concurrently, or the blocks may sometimes be executed in the reverse order, depending upon the functionality involved. It will also be noted that each block of the block diagrams or flowchart illustration, and combinations of blocks in the block diagrams or flowchart illustration, can be implemented by special purpose hardware-based systems which perform the specified functions or acts, or combinations of special purpose hardware and computer instructions.
The modules involved in the embodiments of the present invention may be implemented in software or in hardware. The described modules may also be provided in a processor, for example, as: a processor comprises a creation module, an instance module, an execution module, a receiving module and a destruction module, wherein the names of these modules do not constitute a limitation of the module itself in some cases.
As another aspect, the present invention also provides a computer-readable medium that may be contained in the apparatus described in the above embodiments; or may be present alone without being fitted into the device. The computer readable medium carries one or more programs which, when executed by a device, implement the method of: responding to a call instruction transmitted by an H5 end, wherein the call instruction carries an instruction name and an entry of a target instruction, a communication callback function and an execution function corresponding to the target instruction are created, the communication callback function is mounted on a global object in the execution function, and a resolve method is used as the entry of the execution function; initializing a Promise instance, and transmitting the execution function into the Promise instance; the original terminal is controlled to execute the target instruction, and the communication callback function is called, so that an execution result of the target instruction is transmitted to the communication callback function; invoking the resolve method and taking the execution result of the target instruction as the parameter of the resolve method so that the H5 end obtains the execution result of the target instruction; destroying the Promidase instance.
As a further aspect, embodiments of the present invention also provide a computer program product comprising a computer program which, when executed by a processor, implements the method according to any of the above embodiments.
According to the technical scheme of the embodiment of the invention, the communication callback function and the execution function corresponding to the creation target instruction are adopted, in the execution function, the communication callback function is mounted on the global object, the resolve method is used as the parameter of the execution function, then the Promise instance is initialized, and the execution function is transmitted into the Promise instance, so that the technical problems of code bloated, unclear structure and difficulty in maintenance of code logic in the prior art are solved. The embodiment of the invention has the advantages that the communication result data of the H5 end and the original end can be asynchronously acquired only by the instruction name and the input parameters of the input target instruction, the problems of creating and destroying callback functions are not required to be concerned, the programming convenience can be greatly improved, the programming logic of codes can be obviously improved, the whole code quantity is greatly reduced, the code structure and the code logic become very clear and robust, and the codes are easy to maintain.
The above embodiments do not limit the scope of the present invention. It will be apparent to those skilled in the art that various modifications, combinations, sub-combinations and alternatives can occur depending upon design requirements and other factors. Any modifications, equivalent substitutions and improvements made within the spirit and principles of the present invention should be included in the scope of the present invention.

Claims (17)

1. A method for implementing H5-to-native communications, comprising:
responding to a call instruction transmitted by an H5 end, wherein the call instruction carries an instruction name and an entry of a target instruction, a communication callback function and an execution function corresponding to the target instruction are created, the communication callback function is mounted on a global object in the execution function, and a resolve method is used as the entry of the execution function;
initializing a Promise instance, and transmitting the execution function into the Promise instance;
the original terminal is controlled to execute the target instruction, and the communication callback function is called, so that an execution result of the target instruction is transmitted to the communication callback function;
invoking the resolve method and taking the execution result of the target instruction as the parameter of the resolve method so that the H5 end obtains the execution result of the target instruction;
Destroying the Promidase instance.
2. The method of claim 1, wherein creating a communication callback function and an execution function corresponding to the target instruction, wherein in the execution function, the communication callback function is mounted on a global object, and a resolve method is used as an entry of the execution function, and the method comprises:
creating a communication callback function corresponding to the target instruction, naming the communication callback function, and taking the communication result of the H5 end and the original end as an entry of the communication callback function;
and creating an execution function, wherein in the execution function, the communication callback function is mounted on a global object, and a resolve method is used as an entry of the execution function.
3. The method of claim 2, wherein naming the communication callback function comprises:
and combining the instruction name of the target instruction, the current time stamp and the random number generated by adopting the current time stamp into the name of the communication callback function.
4. The method of claim 2, wherein mounting the communication callback function on the global object comprises:
and mounting the communication callback function on the window global object, and adopting the name of the communication callback function as a key value of the window global object.
5. The method according to claim 1, wherein controlling the native terminal to execute the target instruction and call the communication callback function, so as to transfer an execution result of the target instruction to the communication callback function, comprises:
and transmitting the instruction name of the target instruction, the entry of the target instruction and the name of the communication callback function to a native terminal, so that the native terminal receives the instruction name of the target instruction, the entry of the target instruction and the name of the communication callback function, executes the target instruction, and calls the communication callback function, thereby transmitting the execution result of the target instruction to the communication callback function.
6. The method of claim 1, wherein invoking the resolve method and taking the execution result of the target instruction as an entry to the resolve method to cause the H5 side to obtain the execution result of the target instruction comprises:
and calling the resolve method, taking the execution result of the target instruction as the parameter of the resolve method, so that the H5 end obtains the execution result of the target instruction, and simultaneously modifying the running state of the Promise instance from an initial state to a successful state.
7. The method of claim 6, wherein destroying the Promise instance comprises:
deleting the communication callback function mounted on the global object by using a method for deleting the object attribute;
and destroying the Promise instance with the running state being a successful state.
8. An apparatus for enabling H5 to native communications, comprising:
the creation module is used for responding to a call instruction transmitted by an H5 end, wherein the call instruction carries the instruction name and the entry of a target instruction, creating a communication callback function corresponding to the target instruction, naming the communication callback function, and taking the communication result of the H5 end and a primary end as the entry of the communication callback function;
the instance module is used for initializing a Promise instance and transmitting the execution function into the Promise instance;
the execution module is used for controlling the original end to execute the target instruction and calling the communication callback function so as to transmit the execution result of the target instruction to the communication callback function;
the receiving module is used for calling the resolve method and taking the execution result of the target instruction as the parameter of the resolve method so that the H5 end can obtain the execution result of the target instruction;
And the destroying module is used for destroying the Promise instance.
9. The apparatus of claim 8, wherein the creation module is further to:
creating a communication callback function corresponding to the target instruction, naming the communication callback function, and taking the communication result of the H5 end and the original end as an entry of the communication callback function;
and creating an execution function, wherein in the execution function, the communication callback function is mounted on a global object, and a resolve method is used as an entry of the execution function.
10. The apparatus of claim 9, wherein the creation module is further configured to:
and combining the instruction name of the target instruction, the current time stamp and the random number generated by adopting the current time stamp into the name of the communication callback function.
11. The apparatus of claim 9, wherein the creation module is further configured to:
and mounting the communication callback function on the window global object, and adopting the name of the communication callback function as a key value of the window global object.
12. The apparatus of claim 8, wherein the execution module is further to:
And transmitting the instruction name of the target instruction, the entry of the target instruction and the name of the communication callback function to a native terminal, so that the native terminal receives the instruction name of the target instruction, the entry of the target instruction and the name of the communication callback function, executes the target instruction, and calls the communication callback function, thereby transmitting the execution result of the target instruction to the communication callback function.
13. The apparatus of claim 8, wherein the receiving module is further configured to:
and calling the resolve method, taking the execution result of the target instruction as the parameter of the resolve method, so that the H5 end obtains the execution result of the target instruction, and simultaneously modifying the running state of the Promise instance from an initial state to a successful state.
14. The apparatus of claim 13, wherein the destruction module is further configured to:
deleting the communication callback function mounted on the global object by using a method for deleting the object attribute;
and destroying the Promise instance with the running state being a successful state.
15. An electronic device, comprising:
one or more processors;
Storage means for storing one or more programs,
the one or more processors implement the method of any of claims 1-7 when the one or more programs are executed by the one or more processors.
16. A computer readable medium, on which a computer program is stored, characterized in that the program, when being executed by a processor, implements the method according to any of claims 1-7.
17. A computer program product comprising a computer program which, when executed by a processor, implements the method according to any of claims 1-7.
CN202311119283.7A 2023-08-31 2023-08-31 Method and device for realizing H5 and native communication and electronic equipment Pending CN117194074A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202311119283.7A CN117194074A (en) 2023-08-31 2023-08-31 Method and device for realizing H5 and native communication and electronic equipment

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202311119283.7A CN117194074A (en) 2023-08-31 2023-08-31 Method and device for realizing H5 and native communication and electronic equipment

Publications (1)

Publication Number Publication Date
CN117194074A true CN117194074A (en) 2023-12-08

Family

ID=88984346

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202311119283.7A Pending CN117194074A (en) 2023-08-31 2023-08-31 Method and device for realizing H5 and native communication and electronic equipment

Country Status (1)

Country Link
CN (1) CN117194074A (en)

Similar Documents

Publication Publication Date Title
CN111090423B (en) Webhook framework system and method for realizing active calling and event triggering
CN110795741B (en) Method and device for carrying out security processing on data
CN111427701A (en) Workflow engine system and business processing method
WO2023011274A1 (en) Communication protocol conversion method, and device, system, and gateway device
CN113517985B (en) File data processing method and device, electronic equipment and computer readable medium
CN113595927A (en) Method and device for processing mirror flow in bypass mode
CN112835632B (en) Method and equipment for calling end capability and computer storage medium
CN109725887B (en) Data interaction method and device based on message research and development framework and terminal equipment
CN114979295A (en) Gateway management method and device
CN112015383A (en) Login method and device
CN113010238A (en) Permission determination method, device and system for micro application call interface
CN113760264A (en) Method and device for multiplexing components among multiple applications
CN113760487B (en) Service processing method and device
CN117194074A (en) Method and device for realizing H5 and native communication and electronic equipment
CN114327404A (en) File processing method and device, electronic equipment and computer readable medium
CN110909269B (en) Log reporting method and device
CN112905273A (en) Service calling method and device
CN113709171A (en) Method and device for cross-component state communication
CN113765983A (en) Site service deployment method and device
CN112306695A (en) Data processing method and device, electronic equipment and computer storage medium
CN113779122A (en) Method and apparatus for exporting data
CN112099841A (en) Method and system for generating configuration file
US11595356B1 (en) Method, apparatus, device and storage medium for processing network request
CN117076160B (en) Component calling method, device, equipment and storage medium
CN114095758B (en) Cloud image intercepting method and related device

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination