Disclosure of Invention
One or more embodiments of the present specification describe a method and apparatus for communication, which enables point-to-multipoint bidirectional communication between terminals.
In a first aspect, a method for communication is provided, where the method is applied to a first terminal, the first terminal includes a container with a proxy function, the container corresponds to multiple applications located in multiple terminals, the multiple terminals include the first terminal, and the method includes:
receiving, by the container, a publication request sent by a first application program that establishes a connection with the container among the plurality of application programs, where the publication request carries a message to be published and a first message topic corresponding to the message to be published, and the first application program is an application program in the first terminal or an application program in another terminal;
determining a second application program which is subscribed to the first message topic in the plurality of application programs according to a subscription relation between the application programs and the message topics which are stored in the container in advance;
and sending the message to the terminal to which the second application program belongs through the container.
Optionally, the method further includes:
receiving a subscription request sent by a third application program in the plurality of application programs through the container, wherein the subscription request carries a second message topic;
and adding the subscription relationship between the third application program and the second message topic in the subscription relationship between the application program and the message topic pre-stored in the container.
Optionally, the method further includes establishing a connection with the first application through the container, including:
receiving a connection request sent by the first application program through the container, wherein the connection request carries an encrypted first password, and the encrypted first password is obtained by encrypting the first password by the first application program through a first private key;
decrypting the encrypted first password according to a public key corresponding to the first private key stored in the container in advance to obtain the first password;
determining a second password corresponding to the first application program according to the corresponding relation between the application program and the password stored in the container in advance;
if the first password is the same as the second password, sending a connection success response to the first application program through the container so that the first application program and the container are connected;
and if the first password is different from the second password, sending a connection failure response to the first application program through the container.
Optionally, the method further includes:
receiving a password acquisition request sent by the first application program through the container;
acquiring a third password corresponding to the first application program through a container;
encrypting the third password according to a second private key stored in the container in advance to obtain an encrypted third password;
and sending a password acquisition response to the first application program through the container, wherein the password acquisition response carries the encrypted third password.
Optionally, the obtaining a third password corresponding to the first application includes:
and determining a third password corresponding to the first application program according to the corresponding relation between the application program and the password stored in the container in advance.
Optionally, the obtaining a third password corresponding to the first application includes:
generating a third password corresponding to the first application program according to a preset password generation rule;
the method further comprises the following steps:
and storing the corresponding relation between the first application program and the third password into the container.
In a second aspect, there is provided an apparatus for communication, the apparatus being deployed in a first terminal and having a proxy function, the apparatus corresponding to a plurality of applications located in a plurality of terminals including the first terminal, the apparatus comprising:
a first receiving module, configured to receive a publication request sent by a first application program that establishes a connection with the device among the multiple application programs, where the publication request carries a message to be published and a first message topic corresponding to the message to be published, and the first application program is an application program in the first terminal or an application program in another terminal;
the first determining module is used for determining a second application program which subscribes to the first message topic in the plurality of application programs according to a pre-stored subscription relationship between the application programs and the message topics;
and the first sending module is used for sending the message to the terminal to which the second application program belongs.
Optionally, the apparatus further comprises:
a second receiving module, configured to receive a subscription request sent by a third application program of the multiple application programs, where the subscription request carries a second message topic;
and the adding module is used for adding the subscription relationship between the third application program and the second message topic in the pre-stored subscription relationship between the application program and the message topic.
Optionally, the apparatus further comprises:
a third receiving module, configured to receive a connection request sent by the first application, where the connection request carries an encrypted first password, and the encrypted first password is obtained by encrypting the first password by the first application through a first private key;
the decryption module is used for decrypting the encrypted first password according to a pre-stored public key corresponding to the first private key to obtain the first password;
the second determining module is used for determining a second password corresponding to the first application program according to the corresponding relation between the prestored application program and the password;
a second sending module, configured to send a connection success response to the first application program if the first password is the same as the second password, so that the first application program establishes a connection with the container;
and a third sending module, configured to send a connection failure response to the first application program if the first password is different from the second password.
Optionally, the apparatus further comprises:
a fourth receiving module, configured to receive a password acquisition request sent by the first application;
the acquisition module is used for acquiring a third password corresponding to the first application program;
the encryption module is used for encrypting the third password according to a prestored second private key to obtain an encrypted third password;
a fourth sending module, configured to send a password acquisition response to the first application, where the password acquisition response carries the encrypted third password.
Optionally, the obtaining module is specifically configured to:
and determining a third password corresponding to the first application program according to the corresponding relation between the application program and the password stored in the container in advance.
Optionally, the obtaining module is specifically configured to:
generating a third password corresponding to the first application program according to a preset password generation rule;
the device further comprises:
and the storage module is used for storing the corresponding relation between the first application program and the third password into the device.
In a third aspect, there is provided a terminal comprising a memory and a processor, wherein the memory stores executable code, and the processor executes the executable code to implement the method of any one of the first aspect.
In a fourth aspect, there is provided a computer readable storage medium having a computer program stored thereon, wherein the computer program, when executed in a computer, causes the computer to perform the method of any of the first aspects.
According to the communication method and device provided by the embodiment of the specification, the first terminal receives a publishing request sent by a first application program which establishes connection with the container in the plurality of application programs through the container. The publishing request carries the message to be published and the first message topic corresponding to the message to be published. Then, the first terminal determines a second application program subscribed to the first message topic in the plurality of application programs according to the subscription relationship between the application programs and the message topics stored in the container in advance, and sends the message to the terminal to which the second application program belongs through the container. In this way, after the first container receives the message published to the first message topic by the first application program in the first terminal, the message can be forwarded to the terminals to which all the second application programs subscribed to the first message topic belong, so as to realize point-to-multipoint communication between the first terminal and other terminals.
Detailed Description
The scheme provided by the specification is described below with reference to the accompanying drawings.
Fig. 1 is an architecture diagram of a system provided by an embodiment of the present disclosure, which may include a first terminal and a second terminal connected to each other, as shown in fig. 1. The first terminal and the second terminal are respectively operated with a first application and a second application. Wherein the first application and the second application may be mixed mode mobile applications (hybrid-apps), or web applications (web-apps) or native applications (native-apps). The first application may include a first application program and a first container. The second application may include a second application program and a second container. The first application program and the second application program are integrated with a subscription (Subscribers) function and a publishing (Publishers) function; the first container or the second container is integrated with a proxy (browser) function and a security function. The embodiment of the present application is described by taking an example in which the first container is integrated with an agent function and a security function, and the other cases are similar to these.
It should be noted that the subscription function and the publishing function integrated in the application program, and the proxy function and the security function integrated in the container may implement a subscription function, a publishing function, a proxy function, and a security function in a Message queue Telemetry Transport protocol (MQTT) or a WebSocket protocol, or may also be a subscription function, a publishing function, a proxy function, and a security function in other types of protocols, which is not limited in the embodiment of the present application.
The embodiment of the present specification provides a communication method, which can implement point-to-multipoint bidirectional communication between terminals, and the method is applied to a first terminal, and as shown in fig. 2, the processing procedure is as follows:
step 201, receiving, by a container, a publishing request sent by a first application program, which establishes a connection with the container, in the plurality of application programs.
The publishing request carries the message to be published and the first message topic corresponding to the message to be published.
In implementation, when the first terminal needs to communicate with other terminals, the first terminal may establish a connection with a container (i.e., a first container) having an agent function, which the first application includes, through an application (i.e., a first application) included in the first application installed in the first terminal. The first terminal may then send a publication request to the first container via the first application. The publishing request carries the message to be published and the first message topic corresponding to the message to be published. After receiving the publishing request sent by the first application program, the first container may parse the publishing request to obtain the message to be published carried in the publishing request and the first message topic corresponding to the message to be published.
The first application may be the first application in the first terminal, or may be an application in another terminal. The embodiment of the present application is described by taking a first application as an example of a first application in a first terminal, and other situations are similar to the first application.
Step 202, according to the subscription relationship between the application programs and the message topics pre-stored in the container, determining a second application program subscribing to the first message topic from the plurality of application programs.
In implementation, the first container may store a subscription relationship between the application program and the message topic in advance. The subscription relationship may be set by a technician, or may be established according to a subscription request sent by an application program. After the first container obtains the message to be published carried in the publication request sent by the first application program and the first message topic corresponding to the message to be published, a second application program which subscribes to the first message topic in the multiple application programs can be queried in the subscription relationship between the prestored application program and the message topic.
Step 203, sending a message to the terminal to which the second application program belongs through the container.
In an implementation, after the first container obtains the second application program subscribed to the first message topic, the message may be forwarded to the terminal to which the second application program belongs. In this way, after the first container receives the message that the first application program in the first terminal publishes to the first message topic, the first container can forward the message to the terminals to which all the second application programs subscribed to the first message topic belong, thereby realizing the point-to-multipoint communication between the first terminal and other terminals.
Optionally, the first terminal may further establish a subscription relationship between the application program and the message topic according to a subscription request sent by the first application program, where the detailed processing procedure is as follows:
step one, a subscription request sent by a third application program in a plurality of application programs is received through a container.
And the subscription request carries the second message topic.
In an implementation, the third application may be an application in the first terminal, or may be an application in another terminal. The embodiment of the present application is described by taking a first application program in a first terminal as an example, and other situations are similar to the first application program. When a first terminal needs to subscribe to a certain message topic, the first terminal can send a subscription request to a first container with a proxy function, which is contained in a first application installed in the first terminal, through the first application program contained in the first application. Wherein the subscription request carries a second message topic. After receiving the subscription request sent by the first application program, the first container may parse the subscription request to obtain a second message topic carried in the subscription request.
And step two, adding the subscription relationship between the third application program and the second message topic in the subscription relationship between the application program and the message topic pre-stored in the container.
In implementation, after the first container obtains the second message topic carried in the subscription request sent by the first application program, the subscription relationship between the first application program and the second message topic may be added in the pre-stored subscription relationship between the application program and the message topic. In this way, when the first container receives the message which is published to the second message topic by the terminal to which the other application program belongs, the message can be sent to the first terminal to which the first application program belongs according to the subscription relationship between the first application program and the second message topic.
Optionally, the first terminal may further establish a connection between the first application and the first container according to a connection request sent by the first application. The detailed treatment process is as follows:
step one, receiving a connection request sent by a first application program through a container.
The connection request carries an encrypted first password, and the encrypted first password is obtained by encrypting the first password by the first application program through the first private key.
In implementation, when the first terminal needs to communicate with other terminals, the first terminal needs to first establish a connection between the first application and the first container. The first terminal may transmit a connection request to a first container having a proxy function, which the first application includes, through a first application program included in a first application installed in the first terminal. The connection request carries an encrypted first password, and the encrypted first password is obtained by encrypting the first password by the first application program through the first private key. After receiving the connection request sent by the first application program, the first container may parse the connection request to obtain the encrypted first password carried in the connection request.
It should be noted that the encrypted first password may be transmitted as a parameter of a Uniform Resource Identifier (URI) in the connection request.
And step two, decrypting the encrypted first password according to a public key corresponding to a first private key stored in the container in advance to obtain the first password.
In implementation, after the first container obtains the encrypted first password carried in the connection request sent by the first application program, the first container may decrypt the encrypted first password according to a pre-stored public key corresponding to the first private key to obtain the first password.
And step three, determining a second password corresponding to the first application program according to the corresponding relation between the application program and the password stored in the container in advance.
In implementation, the first container may further store a correspondence between the application program and the password in advance. The password corresponding to the application program can be set by a technician or generated according to a preset password generation rule. After receiving the connection request sent by the first application program, the first container may query a second password corresponding to the first application program in a pre-stored correspondence between the application program and the password. After the first container obtains the first password and the second password, whether the first password and the second password are the same can be further judged. And if the first password is the same as the second password, executing the step four. And if the first password is different from the second password, executing a fifth step.
And step four, sending a connection success response to the first application program through the container so as to enable the first application program to establish connection with the container.
In an implementation, if the first password is the same as the second password, indicating that the first container is successfully authenticated to the first application, the first container may send a connection success response to the first application. In this way, the first application program can establish connection with the first container after receiving the connection success response sent by the first container.
And step five, sending a connection failure response to the first application program through the container.
In an implementation, if the first password is different from the second password, indicating that the first container failed to authenticate the first application, the first container may send a connection failure response to the first application.
Optionally, the first terminal may further obtain a password corresponding to the first application program according to a password obtaining request sent by the first application program. The detailed treatment process is as follows:
step one, a password acquisition request sent by a first application program is received through a container.
In implementation, when a first terminal needs to establish a connection between a first application and a first container, the first terminal may send a password acquisition request to the first container with an agent function, which is included in a first application installed in the first terminal, through the first application included in the first application.
And step two, acquiring a third password corresponding to the first application program through the container.
In implementation, after receiving a password obtaining request sent by a first application program, the first container may obtain a third password corresponding to the first application program. The way for the first container to obtain the third password corresponding to the first application program may be various, and the embodiment of the present application provides two possible implementation manners.
In the first embodiment, the third password corresponding to the first application program is determined according to the correspondence between the application program and the password stored in the container in advance.
In implementation, the first container may store a corresponding relationship between the application program and the password in advance, and the corresponding relationship may be set by a technician. After receiving the password acquisition request sent by the first application program, the first container may query a third password corresponding to the first application program in a pre-stored correspondence between the application program and the password.
In the second embodiment, a third password corresponding to the first application program is generated according to a preset password generation rule.
In an implementation, the first container may have a password generation rule stored therein in advance. The password generation rule may be set by a technician. After receiving the password acquisition request sent by the first application program, the first container may generate a third password corresponding to the first application program according to a preset password generation rule.
Optionally, after the first container generates the third password corresponding to the first application program according to the preset password generation rule, the first container may further store the corresponding relationship between the first application program and the third password in the container, so as to subsequently verify the first application program.
And step three, encrypting the third password according to a second private key stored in the container in advance to obtain the encrypted third password.
In implementation, after the first container obtains the third password corresponding to the first application program, the third password may be encrypted according to the pre-stored second private key to obtain the encrypted third password.
And step four, sending a password acquisition response to the first application program through the container.
And the password acquisition response carries the encrypted third password.
In implementation, after obtaining the encrypted third password, the first container may send a password obtaining response to the first application. And the password acquisition response carries the encrypted third password. Subsequently, after receiving the password acquisition response sent by the first container, the first application program may analyze the password acquisition response to obtain an encrypted third password carried in the password acquisition response. Then, the first application program may decrypt the encrypted third password according to the pre-stored public key corresponding to the second private key to obtain the third password, and send a connection request to the first container based on the third password to establish the connection.
According to the communication method provided by the embodiment of the specification, the first terminal receives a publishing request sent by a first application program which establishes a connection with the container in the plurality of application programs through the container. The publishing request carries the message to be published and the first message topic corresponding to the message to be published. Then, the first terminal determines a second application program subscribed to the first message topic in the plurality of application programs according to the subscription relationship between the application programs and the message topics stored in the container in advance, and sends the message to the terminal to which the second application program belongs through the container. In this way, after the first container receives the message published to the first message topic by the first application program in the first terminal, the message can be forwarded to the terminals to which all the second application programs subscribed to the first message topic belong, so as to realize point-to-multipoint communication between the first terminal and other terminals.
Based on the same technical concept, as shown in fig. 3, an embodiment of the present specification further provides a communication apparatus, which is deployed in a first terminal and has an agent function, and corresponds to a plurality of applications located in a plurality of terminals, where the plurality of terminals include the first terminal, and the apparatus includes:
a first receiving module 310, configured to receive a publishing request sent by a first application program that establishes a connection with the apparatus in the multiple application programs, where the publishing request carries a message to be published and a first message topic corresponding to the message to be published, and the first application program is an application program in a first terminal or an application program in another terminal;
a first determining module 320, configured to determine, according to a pre-stored subscription relationship between an application program and a message topic, a second application program that subscribes to the first message topic in the multiple application programs;
a first sending module 330, configured to send a message to a terminal to which the second application belongs.
Optionally, the apparatus further comprises:
the second receiving module is used for receiving a subscription request sent by a third application program in the plurality of application programs, and the subscription request carries a second message theme;
and the adding module is used for adding the subscription relationship between the third application program and the second message topic in the pre-stored subscription relationship between the application program and the message topic.
Optionally, the apparatus further comprises:
the third receiving module is used for receiving a connection request sent by the first application program, wherein the connection request carries an encrypted first password, and the encrypted first password is obtained by encrypting the first password by the first application program through a first private key;
the decryption module is used for decrypting the encrypted first password according to a pre-stored public key corresponding to the first private key to obtain a first password;
the second determining module is used for determining a second password corresponding to the first application program according to the corresponding relation between the prestored application program and the password;
the second sending module is used for sending a connection success response to the first application program if the first password is the same as the second password so as to enable the first application program to establish connection with the container;
and the third sending module is used for sending a connection failure response to the first application program if the first password is different from the second password.
Optionally, the apparatus further comprises:
the fourth receiving module is used for receiving the password acquisition request sent by the first application program;
the acquisition module is used for acquiring a third password corresponding to the first application program;
the encryption module is used for encrypting the third password according to a prestored second private key to obtain an encrypted third password;
and the fourth sending module is used for sending a password acquisition response to the first application program, wherein the password acquisition response carries the encrypted third password.
Optionally, the obtaining module is specifically configured to:
and determining a third password corresponding to the first application program according to the corresponding relation between the application program and the password stored in the container in advance.
Optionally, the obtaining module is specifically configured to:
generating a third password corresponding to the first application program according to a preset password generation rule;
the device also includes:
and the storage module is used for storing the corresponding relation between the first application program and the third password into the device.
With the communication apparatus provided by the embodiments of the present specification, the first terminal receives, through the container, a publication request sent by a first application that establishes a connection with the container, from among the plurality of applications. The publishing request carries the message to be published and the first message topic corresponding to the message to be published. Then, the first terminal determines a second application program subscribed to the first message topic in the plurality of application programs according to the subscription relationship between the application programs and the message topics stored in the container in advance, and sends the message to the terminal to which the second application program belongs through the container. In this way, after the first container receives the message published to the first message topic by the first application program in the first terminal, the message can be forwarded to the terminals to which all the second application programs subscribed to the first message topic belong, so as to realize point-to-multipoint communication between the first terminal and other terminals.
Based on the same technical concept, embodiments of the present specification further provide a terminal, including a memory and a processor, where the memory stores executable codes, and when the processor executes the executable codes, the method executed by the first terminal is implemented.
Based on the same technical concept, embodiments of the present specification further provide a computer-readable storage medium having a computer program stored thereon, where the computer program is executed in a computer, and causes the computer to execute the method executed by the first terminal.
Those skilled in the art will recognize that, in one or more of the examples described above, the functions described in this invention may be implemented in hardware, software, firmware, or any combination thereof. When implemented in software, the functions may be stored on or transmitted over as one or more instructions or code on a computer-readable medium.
The above-mentioned embodiments, objects, technical solutions and advantages of the present invention are further described in detail, it should be understood that the above-mentioned embodiments are only exemplary embodiments of the present invention, and are not intended to limit the scope of the present invention, and any modifications, equivalent substitutions, improvements and the like made on the basis of the technical solutions of the present invention should be included in the scope of the present invention.