A Web extension method and device based on WebSocket
By executing the process on the WebSocket server to generate adaptation layer code and register message processing functions, establishing two-way communication, the problem of Web extension relying on JS engine or host is solved, and Web extensions that do not rely on these engines or hosts are implemented, simplifying the implementation process of extensions.
Patent Information
- Application Number
- CN202110912291.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-08-10
- Publication Date
- 2025-03-04
- Estimated Expiration
- 2041-08-10
AI Technical Summary
The existing web extension scheme relies on the JS engine or host with the JS engine, which causes the Webapp to not be able to run directly normally when the JS engine or its host changes and needs to be re-implemented.
Using WebSocket-based Web extension method, the adaptation layer code is generated by executing the process on the WebSocket server, setting up local service event listening, registering message processing functions, establishing two-way communication, registering general message processing functions, establishing event listening callback channels, and waiting for request messages from the Web side in a loop.
It implements that it does not rely on the JS engine or its host, and only needs to implement an extension once, solving the problem that Webapp needs to re-implement the extension when the JS engine or host changes.
Smart Images

Figure CN113591006B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer technology, and in particular to a Web expansion method, device, computer equipment and computer-readable storage medium based on WebSocket. Background Art
[0002] The principle of common extension schemes in existing Web is based on JS engine or frameworks based on JS engine in browser. The basic principle is to register the native interface into JS engine through a string binding so that the Web context can recognize this field. When calling this field, it is equivalent to calling the native interface, and then the Native interface will process the specific request and return the result to Web. Existing extension schemes rely on JS engine or host (browser) with JS engine. When JS engine or its host changes, Webapp cannot run normally directly and needs to re-implement its extension.
[0003] At present, there is no effective solution to the problem that Web extensions in related technologies need to rely on JS engines or hosts with JS engines. When the JS engine or its host changes, the Webapp cannot run normally directly and needs to re-implement its extension. Summary of the invention
[0004] The purpose of the present application is to provide a Web extension method, apparatus, computer device and computer-readable storage medium based on WebSocket in order to address the deficiencies in the prior art, so as to at least solve the problem in the related art that Web extension needs to rely on a JS engine or a host with a JS engine, and when the JS engine or its host changes, the Webapp cannot run normally directly and needs to re-implement its extension.
[0005] To achieve the above purpose, the technical solution adopted by this application is:
[0006] In a first aspect, an embodiment of the present application provides a Web extension method based on WebSocket, and the execution process of the method on the WebSocket server includes:
[0007] Write a JS extended interface description file according to the format, and generate an adaptation layer code corresponding to the JS extended interface description file;
[0008] Set up local service event monitoring;
[0009] Register the message processing function of the extension interface;
[0010] Establish two-way communication with the Web client;
[0011] Register a general message processing function;
[0012] Establish an event monitoring callback channel;
[0013] Waiting for the request message from the Web end in a loop.
[0014] In some embodiments, the execution process of the method on the Web side includes:
[0015] Add the packaged JS extension library and automatically establish a communication connection with the WebSocket server when the web page is running;
[0016] Add JS extension interface description file and initialize JS extension interface;
[0017] Register the event listeners required by the application;
[0018] Use the JS extension interface to develop applications.
[0019] In some embodiments, adding a JS extension interface description file and initializing the JS extension interface includes:
[0020] Mount the function of the JS extension interface to the JS context;
[0021] Set a sending function for the function of the JS extension interface;
[0022] Construct message parameters;
[0023] Assign a unique identifier to each request message.
[0024] In some embodiments, the event listeners required for registering the application include:
[0025] After confirming that the communication connection with the WebSocket server is successfully established, register the event listeners required by the application.
[0026] In a second aspect, an embodiment of the present application provides a Web extension device based on WebSocket, the device comprising a first execution unit, configured to execute a process on a WebSocket server, wherein the first execution unit comprises:
[0027] A definition module is used to write a JS extended interface description file in a format and generate an adaptation layer code corresponding to the JS extended interface description file;
[0028] The setting module is used to set up local service event monitoring;
[0029] The first registration module is used to register the message processing function of the extended interface;
[0030] The first establishment module is used to establish two-way communication with the Web terminal;
[0031] The second registration module is used to register a general message processing function;
[0032] The second establishment module is used to establish an event monitoring callback channel;
[0033] The waiting module is used to cyclically wait for the request message from the Web side.
[0034] In some embodiments, the device further includes a second execution unit, configured to execute the process on the Web side, wherein the second execution unit includes:
[0035] The first adding module is used to add the packaged JS extension library and automatically establish a communication connection with the WebSocket server when the web page is running;
[0036] The second adding module is used to add the JS extension interface description file and initialize the JS extension interface;
[0037] The third registration module is used to register event listeners required by the application;
[0038] A development module is used to develop applications using the JS extension interface.
[0039] In some embodiments, the second adding module includes:
[0040] The mounting submodule is used to mount the function of the JS extension interface into the JS context;
[0041] Setting a submodule, used to set a sending function for the function of the JS extension interface;
[0042] The construction submodule is used to construct message parameters;
[0043] The allocation submodule is used to allocate a unique identifier to each request message.
[0044] In some embodiments, the third registration module includes:
[0045] The registration submodule is used to register the event listeners required by the application after determining that the communication connection with the WebSocket server is successfully established.
[0046] In a third aspect, an embodiment of the present application provides a computer device, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein when the processor executes the computer program, the Web extension method based on WebSocket as described in the first aspect above is implemented.
[0047] In a fourth aspect, an embodiment of the present application provides a computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements the WebSocket-based Web extension method as described in the first aspect above.
[0048] The present application adopts the above technical scheme. Compared with the prior art, the Web extension based on WebSocket provided by the embodiment of the present application performs the following on the WebSocket server: writing the JS extension interface description file in a format, and generating the adaptation layer code corresponding to the JS extension interface description file; setting the local service event listener; registering the message processing function of the extension interface; establishing two-way communication with the Web end; registering the general message processing function; establishing an event listening callback channel; and cyclically waiting for the request message from the Web end. This solves the problem in the related art that the Web extension needs to rely on the JS engine or a host with a JS engine, and when the JS engine or its host changes, the Webapp cannot run normally directly and needs to re-implement its extension, and achieves the problem that it does not rely on the JS engine or its host and only needs to implement the extension once.
[0049] Details of one or more embodiments of the present application are set forth in the following drawings and description to make other features, objects, and advantages of the present application more readily apparent. BRIEF DESCRIPTION OF THE DRAWINGS
[0050] The drawings described herein are used to provide a further understanding of the present application and constitute a part of the present application. The illustrative embodiments of the present application and their descriptions are used to explain the present application and do not constitute an improper limitation on the present application. In the drawings:
[0051] Figure 1 is a structural block diagram of a mobile terminal according to an embodiment of the present application;
[0052] Figure 2 It is a framework structure diagram of a Web extension method based on WebSocket according to an embodiment of the present application;
[0053] Figure 3 It is a flowchart of the execution process of the Web expansion method based on WebSocket on the WebSocket server according to an embodiment of the present application;
[0054] Figure 4 It is a flowchart of the execution process of the Web expansion method based on WebSocket on the Web side according to an embodiment of the present application;
[0055] Figure 5 is a structural block diagram of a Web expansion device based on WebSocket according to an embodiment of the present application;
[0056] Figure 6is a structural block diagram of a Web expansion device based on WebSocket according to another embodiment of the present application;
[0057] Figure 7 Schematic diagram of the hardware structure of a computer device according to an embodiment of the present application. DETAILED DESCRIPTION
[0058] In order to make the purpose, technical solutions and advantages of the present application clearer, the present application is described and illustrated below in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present application and are not intended to limit the present application. Based on the embodiments provided in the present application, all other embodiments obtained by ordinary technicians in the field without making creative work are within the scope of protection of the present application.
[0059] Obviously, the drawings described below are only some examples or embodiments of the present application. For ordinary technicians in this field, the present application can also be applied to other similar scenarios based on these drawings without creative work. In addition, it can also be understood that although the efforts made in this development process may be complicated and lengthy, for ordinary technicians in this field related to the content disclosed in this application, some changes in design, manufacturing or production based on the technical content disclosed in this application are just conventional technical means, and should not be understood as insufficient content disclosed in this application.
[0060] Reference to "embodiments" in this application means that a particular feature, structure, or characteristic described in conjunction with the embodiments may be included in at least one embodiment of the present application. The appearance of the phrase in various locations in the specification does not necessarily refer to the same embodiment, nor is it an independent or alternative embodiment that is mutually exclusive with other embodiments. It is explicitly and implicitly understood by those of ordinary skill in the art that the embodiments described in this application may be combined with other embodiments without conflict.
[0061] Unless otherwise defined, the technical terms or scientific terms involved in this application should be understood by people with ordinary skills in the technical field to which this application belongs. The words "one", "a", "a", "the" and the like involved in this application do not indicate a quantitative limitation, and may represent the singular or plural. The terms "include", "comprise", "have" and any of their variations involved in this application are intended to cover non-exclusive inclusions; for example, a process, method, system, product or device that includes a series of steps or modules (units) is not limited to the listed steps or units, but may also include steps or units that are not listed, or may also include other steps or units inherent to these processes, methods, products or devices. The words "connect", "connected", "coupled" and the like involved in this application are not limited to physical or mechanical connections, but may include electrical connections, whether direct or indirect. The "multiple" involved in this application refers to two or more. "And / or" describes the association relationship of associated objects, indicating that there may be three relationships, for example, "A and / or B" can represent: A exists alone, A and B exist at the same time, and B exists alone. The character " / " generally indicates that the objects before and after are in an "or" relationship. The terms "first", "second", "third", etc. involved in this application are only used to distinguish similar objects and do not represent a specific ordering of the objects.
[0062] This embodiment provides a mobile terminal. Figure 1 is a structural block diagram of a mobile terminal according to an embodiment of the present application. Figure 1 As shown, the mobile terminal includes: a radio frequency (RF) circuit 110, a memory 120, an input unit 130, a display unit 140, a sensor 150, an audio circuit 160, a wireless fidelity (WiFi) module 170, a processor 180, and a power supply 190. Those skilled in the art will understand that Figure 1 The structure of the mobile terminal shown in the figure does not constitute a limitation on the mobile terminal, and may include more or less components than shown in the figure, or combine certain components, or arrange the components differently.
[0063] Combine the following Figure 1 The following is a detailed introduction to the various components of the mobile terminal:
[0064] The RF circuit 110 can be used for receiving and sending signals during the process of sending and receiving information or making calls. In particular, after receiving the downlink information of the base station, it is sent to the processor 180 for processing; in addition, the designed uplink data is sent to the base station. Usually, the RF circuit includes but is not limited to an antenna, at least one amplifier, a transceiver, a coupler, a low noise amplifier (Low Noise Amplifier, referred to as LNA), a duplexer, etc. In addition, the RF circuit 110 can also communicate with the network and other devices through wireless communication. The above-mentioned wireless communication can use any communication standard or protocol, including but not limited to the Global System of Mobile communication (GSM), General Packet Radio Service (GPRS), Code Division Multiple Access (CDMA), Wideband Code Division Multiple Access (WCDMA), Long Term Evolution (LTE), email, Short Messaging Service (SMS), etc.
[0065] The memory 120 can be used to store software programs and modules. The processor 180 executes various functional applications and data processing of the mobile terminal by running the software programs and modules stored in the memory 120. The memory 120 may mainly include a program storage area and a data storage area, wherein the program storage area may store an operating system, an application required for at least one function (such as a sound playback function, an image playback function, etc.), etc.; the data storage area may store data created according to the use of the mobile terminal (such as audio data, a phone book, etc.), etc. In addition, the memory 120 may include a high-speed random access memory, and may also include a non-volatile memory, such as at least one disk storage device, a flash memory device, or other volatile solid-state storage devices.
[0066] The input unit 130 can be used to receive input digital or character information, and to generate key signal input related to the user settings and function control of the mobile terminal. Specifically, the input unit 130 may include a touch panel 131 and other input devices 132. The touch panel 131, also known as a touch screen, can collect the user's touch operation on or near it (such as the user's operation on the touch panel 131 or near the touch panel 131 using any suitable object or accessory such as a finger, stylus, etc.), and drive the corresponding connection device according to a pre-set program. Optionally, the touch panel 131 may include two parts: a touch detection device and a touch controller. Among them, the touch detection device detects the user's touch orientation, detects the signal brought by the touch operation, and transmits the signal to the touch controller; the touch controller receives the touch information from the touch detection device, converts it into contact coordinates, and then sends it to the processor 180, and can receive and execute commands sent by the processor 180. In addition, the touch panel 131 can be implemented in various types such as resistive, capacitive, infrared, and surface acoustic waves. In addition to the touch panel 131, the input unit 130 may also include other input devices 132. Specifically, other input devices 132 may include but are not limited to one or more of a physical keyboard, function keys (such as volume control keys, switch keys, etc.), a trackball, a mouse, a joystick, etc.
[0067] The display unit 140 can be used to display information input by the user or information provided to the user and various menus of the mobile terminal. The display unit 140 may include a display panel 141. Optionally, the display panel 141 may be configured in the form of a liquid crystal display (LCD), an organic light-emitting diode (OLED), etc. Further, the touch panel 131 may cover the display panel 141. When the touch panel 131 detects a touch operation on or near it, it is transmitted to the processor 180 to determine the type of touch event. Subsequently, the processor 180 provides a corresponding visual output on the display panel 141 according to the type of touch event. Although in Figure 1 In the embodiment, the touch panel 131 and the display panel 141 are used as two independent components to implement the input and output functions of the mobile terminal, but in some embodiments, the touch panel 131 and the display panel 141 can be integrated to implement the input and output functions of the mobile terminal.
[0068] The mobile terminal may also include at least one sensor 150, such as a light sensor, a motion sensor, and other sensors. Specifically, the light sensor may include an ambient light sensor and a proximity sensor, wherein the ambient light sensor may adjust the brightness of the display panel 141 according to the brightness of the ambient light, and the proximity sensor may turn off the display panel 141 and / or the backlight when the mobile terminal is moved to the ear. As a type of motion sensor, the accelerometer sensor can detect the magnitude of acceleration in all directions (generally three axes), and can detect the magnitude and direction of gravity when stationary. It can be used for applications that identify the posture of the mobile terminal (such as horizontal and vertical screen switching, related games, magnetometer posture calibration), vibration recognition related functions (such as pedometer, tapping), etc.; as for other sensors that can be configured in the mobile terminal, such as gyroscopes, barometers, hygrometers, thermometers, infrared sensors, etc., they will not be repeated here.
[0069] The speaker 161 and microphone 162 in the audio circuit 160 can provide an audio interface between the user and the mobile terminal. The audio circuit 160 can transmit the received audio data to the speaker 161 after converting the received audio data into an electrical signal, which is converted into a sound signal for output; on the other hand, the microphone 162 converts the collected sound signal into an electrical signal, which is received by the audio circuit 160 and converted into audio data, and then the audio data is output to the processor 180 for processing, and then sent to another mobile terminal through the RF circuit 110, or the audio data is output to the memory 120 for further processing.
[0070] WiFi is a short-range wireless transmission technology. Mobile terminals can help users send and receive emails, browse web pages, and access streaming media through WiFi module 170. It provides users with wireless broadband Internet access. Figure 1 A WiFi module 170 is shown, but it is understandable that it is not an essential component of the mobile terminal and can be omitted as needed without changing the essence of the invention, or replaced with other short-range wireless transmission modules, such as a Zigbee module or a WAPI module.
[0071] The processor 180 is the control center of the mobile terminal. It uses various interfaces and lines to connect various parts of the entire mobile terminal. It executes various functions of the mobile terminal and processes data by running or executing software programs and / or modules stored in the memory 120, and calling data stored in the memory 120, so as to monitor the mobile terminal as a whole. Optionally, the processor 180 may include one or more processing units; preferably, the processor 180 may integrate an application processor and a modem processor, wherein the application processor mainly processes the operating system, user interface and application programs, etc., and the modem processor mainly processes wireless communications. It is understandable that the above-mentioned modem processor may not be integrated into the processor 180.
[0072] The mobile terminal also includes a power supply 190 (such as a battery) for supplying power to various components. Preferably, the power supply can be logically connected to the processor 180 through a power management system, so that the power management system can manage charging, discharging, power consumption and other functions.
[0073] Although not shown, the mobile terminal may also include a camera, a Bluetooth module, etc., which will not be described in detail here.
[0074] In this embodiment, the processor 180 is configured to: on the WebSocket server side, write the JS extended interface description file in the format and generate the adaptation layer code corresponding to the JS extended interface description file; set up local service event monitoring; register the message processing function of the extended interface; establish two-way communication with the Web side; register the general message processing function; establish an event monitoring callback channel; and loop and wait for the request message from the Web side.
[0075] In some of the embodiments, the processor 180 is also configured to: on the Web side, add a packaged JS extension library, and automatically establish a communication connection with the WebSocket server when the web page is running; add a JS extension interface description file, and initialize the JS extension interface; register event listeners required by the application; and use the JS extension interface to develop applications.
[0076] In some of the embodiments, the processor 180 is further configured to: mount the function of the JS extension interface to the JS context; set a sending function for the function of the JS extension interface; construct message parameters; and assign a unique identifier to each message request message.
[0077] In some of the embodiments, the processor 180 is further configured to: register an event listener after determining that a communication connection is successfully established with the WebSocket server.
[0078] This embodiment provides a Web expansion method based on WebSocket.
[0079] First, the technical terms appearing in the embodiments of the present application are explained as follows:
[0080] 1) WebSocket: A network technology that can communicate bidirectionally with a WebSocket Server. The present application embodiment uses the WebSocket technology to obtain service capabilities.
[0081] 2) WebSocket Server: A WebSocket-based server that responds to Web requests.
[0082] 3) JS interface list: Use JsJson technology to describe the interface information to be expanded in a certain format.
[0083] 4) JS extension interface module: It is an encapsulation of WebSocket, providing JS interface list initialization interface, adding and removing event listening interface, etc.
[0084] 5) Adaptor: Adaptation layer, which actually processes JS interface requests and connects to services.
[0085] 6) Web app: An app developed using Web technologies. Web technologies include but are not limited to H5+JS, its variants, and its subsets.
[0086] This application can be applied to all applications that want to develop local service capabilities using Web technology.
[0087] Figure 2 is a framework diagram of a Web extension method based on WebSocket according to an embodiment of the present application, such as Figure 2 As shown, the framework structure of the Web extension method based on WebSocket includes a Web end and a WebSocket server, a browser and a local service. The Web end and the WebSocket server are connected to each other in communication. The WebSocket server is provided with an adaptation layer, and the local service can be called by using the adaptation layer.
[0088] The Web extension method based on WebSocket is divided into the execution process on the WebSocket server and the execution process on the Web side.
[0089] Figure 3 : is a flowchart of the execution process of the Web expansion method based on WebSocket on the WebSocket server according to an embodiment of the present application, such as Figure 3 As shown, the execution process on the WebSocket server includes the following steps:
[0090] Step S301, writing a JS extended interface description file according to a format, and generating an adaptation layer code corresponding to the JS extended interface description file;
[0091] Step S302, setting local service event monitoring;
[0092] Step S303, registering the message processing function of the extended interface;
[0093] Step S304, establishing two-way communication with the Web terminal;
[0094] Step S305, registering a general message processing function;
[0095] Step S306, establishing an event monitoring callback channel;
[0096] Step S307, waiting in a loop for a request message from the Web end.
[0097] Figure 4 : is a flowchart of the execution process of the Web expansion method based on WebSocket on the Web side according to an embodiment of the present application, such as Figure 4 As shown, the execution process on the Web side includes the following steps:
[0098] Step S401, adding the packaged JS extension library, and automatically establishing a communication connection with the WebSocket server when the web page is running;
[0099] Step S402, adding a JS extension interface description file and initializing the JS extension interface;
[0100] Step S403, registering the event monitoring required by the application;
[0101] Step S404, developing an application using the JS extension interface.
[0102] In some embodiments, step S402 adds a JS extension interface description file and initializes the JS extension interface including:
[0103] Mount the function of the JS extension interface to the JS context;
[0104] Set a sending function for the function of the JS extension interface;
[0105] Construct message parameters;
[0106] Assign a unique identifier to each request message.
[0107] In some embodiments, the event monitoring required for registering the application in step S403 includes:
[0108] After confirming that the communication connection with the WebSocket server is successfully established, register the event listeners required by the application.
[0109] Through the above steps, the problem that the Web extension in the related art needs to rely on the JS engine or the host with the JS engine, and when the JS engine or its host changes, the Web app cannot run normally directly and needs to re-implement its extension is solved. It is achieved that it does not rely on the JS engine or its host, and only needs to implement the extension once.
[0110] The present invention uses the WebSocket method to bypass the original extension method based on the JS engine or its host.
[0111] The process on the Web side is:
[0112] a) Add the packaged JS extension library to the head of the web page. When the web page runs, it will automatically connect to the Websocket server.
[0113] b) Add the JS extension interface description file to the header of the web page;
[0114] c) Initialize JS extension interface;
[0115] d) Register event monitoring for each module;
[0116] e) Use the extended interface to develop applications.
[0117] The process on the WebSocket Server side is:
[0118] a) Write the JS extension interface description;
[0119] b) Automatic Adaptor layer code and implementation;
[0120] c) Set up monitoring of system services;
[0121] d) Register the message processing function (Adaptor) of the extended interface, and the WebSocket Server finally distributes the request through this listening container;
[0122] e) Establish a WebSocket Server;
[0123] f) Register a general message processing function, which includes an active request interface, an event listener addition interface, and an event listener removal interface. The interface described in a) is an active request interface, and will be distributed according to the input parameters during its message processing;
[0124] g) Establish an event monitoring callback channel;
[0125] h) Loop and wait for web request or notification.
[0126] Specifically:
[0127] The process on the Web side includes:
[0128] a) Add the packaged JS extension library to the head of the web page. When the web page runs, it will first run to initialize WebSocket, and the onpen, onerror, onmessage, onclose, and send encapsulation work needs to be done.
[0129] b) Add the JS extension interface description file in the header of the web page. The interface description file is provided by the extension developer and is added here to prepare for initialization.
[0130] c) Initialize the JS extension interface. Register the extension interface into the JS context by module, and then you can call the extension interface normally.
[0131] d) Register event listeners for each module. This step needs to be registered after confirming that the connection with the Websocket server is successful, otherwise the Web side saves the callback, but the related keys are not synchronized with the WebSocket Server side.
[0132] e) Use the extended interface to develop applications. The preparations are complete and you can start developing applications.
[0133] The process on the WebSocket Server side includes:
[0134] a) Write a description of the JS extension interface. Describe what module the extension interface is, what APIs are under the module, and what parameters each API has.
[0135] b) Automatically generate and implement the Adaptor layer code. Write a script to automatically parse the JS extension interface description, provide an API for registering the extension interface, and generate an empty implementation of the Adaptor.
[0136] c) Set up monitoring of system services. The Web needs to register the status of some services, which the WebSocket Server itself does not have, so it needs to register with the local service.
[0137] d) Register the message processing function (Adaptor) of the extended interface. The WebSocket Server will finally distribute the request through this listening container. Use the API of the registered extended interface in the automatically generated Adaptor code to set up the listening.
[0138] e) Establish a WebSocket Server to communicate with the Web client in both directions.
[0139] f) Register a general message processing function. There are active request interfaces, event listener addition interfaces, and event listener removal interfaces. The descriptions in a) are all active request interfaces, and they will be distributed according to the input parameters during message processing.
[0140] g) Establish an event monitoring callback channel. This is mainly when the Web end registers a local service event, but when it occurs, it needs to be notified to the Web end.
[0141] h) Loop and wait for Web requests or notifications. Keep the WebSocket active.
[0142] A specific example of the present application is that an interface is needed to obtain the time zone of a television system.
[0143] First, the execution flow of the WebSocket Server:
[0144] 1) First you need to write the JS interface description.
[0145]
[0146] 2) Generate the Adaptor code by parsing the JS interface description through script or write it yourself.
[0147] void apihnd_sraftv_timer_get_time_zone(intid,
[0148] const Json::Value¶ms,Json::Value&result){
[0149] TvTimerServicetvTimerService; / / system service
[0150] result["value"]=tvTimerService.getTimeZone();
[0151] result["status"] = "ok";
[0152] }
[0153] 3) Set up monitoring of system services.
[0154] This interface does not have a scenario for monitoring events. If other interfaces need it, they can monitor according to the local service monitoring specification and set the callback function.
[0155] 4) Register the message processing function of the extension interface.
[0156] When the WebSocket Server starts, the Adaptor corresponding to the JS interface description is stored in a map. In this example, the key can be sraftv_timer_get_time_zone. When the front-end requests, the module, submodule, and function of the interface are concatenated with an underscore and passed down as parameters. Finally, the corresponding Adaptor is found through this key value.
[0157] 5) Establish a WebSocket Server.
[0158] You can get any open source useful library, such as Libwebsockets, and build it according to its specifications. Set up the port and two-way communication processing function. The main tasks are:
[0159] a) WebSocket context information preparation and service startup.
[0160] Set the port, which can be 8989; set the processing function of the protocol status; and start a thread to start the service and wait for messages to come.
[0161] b) Processing of protocol status: Here we only describe the processing of received messages.
[0162] In this example, the state of receiving a message is LWS_CALLBACK_RECEIVE. When the front end sends a message, it puts the front end message into a queue.
[0163] 6) Web-side message queue message processing flow.
[0164] Start a new thread and check whether there are messages in the message queue. If so, take them out one by one for processing; if not, wait.
[0165] 7) General message processing function registration
[0166] Before parsing the message, let's first explain the general message processing function. In actual operation, you only need to register before the request. The general message processing functions are only controlPlatform, addPlatformListener, and removePlatformListener. All active API requests are transmitted through the former, and the latter two are used to add or remove event listeners for local system services or operating system services.
[0167] 8) Front-end message parsing and processing.
[0168] When a message comes from the front end and is taken out for processing, the first thing to do is to distribute it to a common function. In this case, it needs to be distributed to controlPlatform.
[0169] 9) General message processing function (controlPlatform)
[0170] Parse the front-end message parameters, find the extension interface KEY, find the corresponding Adaptor and execute it.
[0171] Execution process on the Web side:
[0172] 1) Add JS extension library.
[0173] a) Start the WebSocket connection and write the callbacks for each status.
[0174] The status callbacks are onopen, onerror, onmessage, and onclose. The most important one is onmessage. When this function is triggered, it means that there is a message coming from the WebSocket Server.
[0175] b) Initialization of the extended interface list.
[0176] The first step of initialization is to mount each module function to the JS context. In this example, sraftv can be used as a global variable, then timer is mounted under sraftv, and finally get_time_zone is mounted under timer; so in the future development, you can initiate a request in the form of sraftv.timer.get_time_zone, which is the same as the traditional API call method.
[0177] The second step of initialization is to set a unified sending function for each extension function. When Web developers call the extension function, it is equivalent to calling this sending function. This form can use Promise, which can be well used for asynchronous development.
[0178] The third step of initialization is to construct message parameters. First, construct the parameters of general message processing, in this case, it is controlPlatform, and then construct the Key parameters of Adaptor, in this case, it is sraftv_timer_get_timer_zone. If the extended interface has parameters, the parameters can be described in Json format, but in this case, there are no parameters.
[0179] The fourth step of initialization is to give each request a unique ID and save its callback function based on this ID so that its callback can be found when the WebSocket Server responds.
[0180] The specific code examples are as follows:
[0181]
[0182]
[0183] In the application embodiment, when the Web calls the extension API, it will be converted into a string message in a certain format according to the custom message protocol, and sent to the local WebSocket server through WebSocket technology; the WebSocket server will parse the message and distribute it to the relevant Adaptor for processing. The relevant Adaptor will then call the local system service capability to implement the request. If there is a result, the result will be returned to the Web end through Websocket technology.
[0184] Key technical points:
[0185] 1) The WebSocket technology is used to realize the communication between Web and Native, which solves the problem that the existing extension mode depends on the JS engine or its host.
[0186] 2) The extended interface description is a bridge for communication between Web capability extension developers and WebApp developers, and it is also a guarantee for the one-to-one correspondence between Web extension interfaces and Adaptor implementations.
[0187] 3) JS extension module library, which encapsulates repeated and multiple details of WebSocket, and provides JS interface list initialization interface, adding and removing event listening interface, etc., so that the Web can use the extension API through traditional API calling methods.
[0188] 4) Adaptor automatically generates scripts and quickly generates Adaptor code, so you only need to focus on the specific request implementation.
[0189] The embodiments of the present application can achieve the following technical effects:
[0190] 1) Break the dependence of web extensions on JS engines or hosts (browsers) with JS engines. This means that if you run on different browser vendors (for example), you don’t need to develop extensions for new browsers.
[0191] 2) Using JS interface description, the front-end initialization and the back-end Adaptor correspond one to one. After the overall framework is established, developers no longer need to worry about the process, making supplementary extensions very simple and quick to get started.
[0192] 3) Initialize the extension interface so that Web developers can use the extension interface in a traditional way without any learning cost. And use the Promise feature to separate asynchronous data acquisition and business logic, so that developers can focus on one of the transactions without considering both at the same time.
[0193] It should be noted that the steps shown in the above process or the flowchart in the accompanying drawings can be executed in a computer system such as a set of computer executable instructions, and although a logical order is shown in the flowchart, in some cases, the steps shown or described can be executed in an order different from that shown here.
[0194] This embodiment provides a Web expansion device based on WebSocket, which is used to implement the above embodiments and preferred implementations, and will not be repeated for what has been described. As used below, the terms "module", "unit", "subunit", etc. can be a combination of software and / or hardware that implements a predetermined function. Although the device described in the following embodiments is preferably implemented in software, the implementation of hardware, or a combination of software and hardware, is also possible and conceivable.
[0195] Figure 5 is a structural block diagram of a Web expansion device based on WebSocket according to an embodiment of the present application, such as Figure 5 As shown, the device includes a first execution unit 51, which is used to execute a process on the WebSocket server, wherein the first execution unit 51 includes:
[0196] A definition module 511 is used to write a JS extended interface description file according to a format and generate an adaptation layer code corresponding to the JS extended interface description file;
[0197] Setting module 512, used to set local service event monitoring;
[0198] The first registration module 513 is used to register the message processing function of the extended interface;
[0199] A first establishing module 514, used to establish a two-way communication with the Web terminal;
[0200] The second registration module 515 is used to register a general message processing function;
[0201] The second establishing module 516 is used to establish an event monitoring callback channel;
[0202] The waiting module 517 is used to cyclically wait for the request message from the Web end.
[0203] In some embodiments, such as Figure 6 As shown, the device further includes a second execution unit 52, which is used to execute the process on the Web side, wherein the second execution unit 52 includes:
[0204] The first adding module 521 is used to add the packaged JS extension library and automatically establish a communication connection with the WebSocket server when the web page is running;
[0205] The second adding module 522 is used to add a JS extension interface description file and initialize the JS extension interface;
[0206] The third registration module 523 is used to register the event monitoring required by the application;
[0207] The development module 524 is used to develop applications using the JS extension interface.
[0208] In some embodiments, the second adding module 522 includes:
[0209] The mounting submodule is used to mount the function of the JS extension interface into the JS context;
[0210] Setting a submodule, used to set a sending function for the function of the JS extension interface;
[0211] The construction submodule is used to construct message parameters;
[0212] The allocation submodule is used to allocate a unique identifier to each request message.
[0213] In some embodiments, the third registration module 523 includes:
[0214] The registration submodule is used to register the event listeners required by the application after determining that the communication connection with the WebSocket server is successfully established.
[0215] It should be noted that the above modules can be functional modules or program modules, and can be implemented by software or hardware. For modules implemented by hardware, the above modules can be located in the same processor; or the above modules can be located in different processors in any combination.
[0216] The embodiment provides a computer device. In combination with the WebSocket-based Web extension method of the embodiment of the present application, the computer device can be implemented. Figure 7 Schematic diagram of the hardware structure of a computer device according to an embodiment of the present application.
[0217] The computer device may include a processor 71 and a memory 72 storing computer program instructions.
[0218] Specifically, the processor 71 may include a central processing unit (CPU), or an application specific integrated circuit (ASIC), or may be configured to implement one or more integrated circuits of the embodiments of the present application.
[0219] Among them, the memory 72 may include a large-capacity memory for data or instructions. By way of example and not limitation, the memory 72 may include a hard disk drive (HDD), a floppy disk drive, a solid-state drive (SSD), a flash memory, an optical disk, a magneto-optical disk, a magnetic tape, or a universal serial bus (USB) drive, or a combination of two or more of these. Where appropriate, the memory 72 may include a removable or non-removable (or fixed) medium. Where appropriate, the memory 72 may be inside or outside the data processing device. In a specific embodiment, the memory 72 is a non-volatile memory. In a specific embodiment, the memory 72 includes a read-only memory (ROM) and a random access memory (RAM). Where appropriate, the ROM may be a mask-programmed ROM, a programmable ROM (Programmable Read-Only Memory, PROM for short), an erasable PROM (Erasable ProgrammableRead-Only Memory, EPROM for short), an electrically erasable PROM (Electrically Erasable ProgrammableRead-Only Memory, EEPROM for short), an electrically alterable ROM (Electrically Alterable Read-Only Memory, EAROM for short) or a flash memory (FLASH) or a combination of two or more of these. Under appropriate circumstances, the RAM can be a static random access memory (SRAM) or a dynamic random access memory (DRAM), wherein the DRAM can be a fast page mode dynamic random access memory (FPMDRAM), an extended data output dynamic random access memory (EDODRAM), a synchronous dynamic random access memory (SDRAM), etc.
[0220] The memory 72 may be used to store or cache various data files required for processing and / or communication, as well as possible computer program instructions executed by the processor 71 .
[0221] The processor 71 implements any one of the WebSocket-based Web extension methods in the above embodiments by reading and executing computer program instructions stored in the memory 72 .
[0222] In some embodiments, the computer device may further include a communication interface 73 and a bus 70. Figure 7 As shown, the processor 71, the memory 72, and the communication interface 73 are connected via a bus 70 and communicate with each other.
[0223] The communication interface 73 is used to implement communication between the modules, devices, units and / or equipment in the embodiment of the present application. The communication interface 73 can also implement data communication with other components such as: external devices, image / data acquisition equipment, databases, external storage, and image / data processing workstations.
[0224] The bus 70 includes hardware, software or both, and couples the components of the computer device to each other. The bus 70 includes but is not limited to at least one of the following: a data bus, an address bus, a control bus, an expansion bus, and a local bus. By way of example and not limitation, bus 70 may include an Accelerated Graphics Port (AGP) or other graphics bus, an Extended Industry Standard Architecture (EISA) bus, a Front Side Bus (FSB), a Hyper Transport (HT) interconnect, an Industry Standard Architecture (ISA) bus, an InfiniBand interconnect, a Low Pin Count (LPC) bus, a memory bus, a Micro Channel Architecture (MCA) bus, a Peripheral Component Interconnect (PCI) bus, a PCI-Express (PCI-X) bus, a Serial Advanced Technology Attachment (SATA) bus, a Video Electronics Standards Association Local Bus (VLB) bus, or other suitable buses or a combination of two or more of these. Where appropriate, bus 70 may include one or more buses. Although embodiments of the present application describe and illustrate a particular bus, the present application contemplates any suitable bus or interconnect.
[0225] In addition, in combination with the Web extension method based on WebSocket in the above embodiment, the embodiment of the present application can provide a computer-readable storage medium for implementation. The computer-readable storage medium stores computer program instructions; when the computer program instructions are executed by the processor, any one of the Web extension methods based on WebSocket in the above embodiment is implemented.
[0226] The technical features of the above-described embodiments may be arbitrarily combined. To make the description concise, not all possible combinations of the technical features in the above-described embodiments are described. However, as long as there is no contradiction in the combination of these technical features, they should be considered to be within the scope of this specification.
[0227] The above-mentioned embodiments only express several implementation methods of the present application, and the descriptions thereof are relatively specific and detailed, but they cannot be understood as limiting the scope of the invention patent. It should be pointed out that, for a person of ordinary skill in the art, several variations and improvements can be made without departing from the concept of the present application, and these all belong to the protection scope of the present application. Therefore, the protection scope of the patent of the present application shall be subject to the attached claims.
Claims
1. A Web expansion method based on WebSocket, characterized in that: The execution process of the method on the WebSocket server includes: Write a JS extended interface description file in a format, and generate an adaptation layer code corresponding to the JS extended interface description file; Set up local service event monitoring; Register the message processing function of the extension interface; Establish two-way communication with the Web client; Register a general message processing function; Establish an event monitoring callback channel; Waiting for the request message from the Web end in a loop; The execution process of the method on the Web side includes: Add the packaged JS extension library and automatically establish a communication connection with the WebSocket server when the web page is running; Add JS extension interface description file and initialize JS extension interface; Register the event listeners required by the application; Develop applications using the JS extension interface; Among them, adding the JS extension interface description file and initializing the JS extension interface include: Mount the function of the JS extension interface to the JS context; Set a sending function for the function of the JS extension interface; Construct message parameters; Assign a unique identifier to each request message; The event listeners required for registering an application include: After confirming that the communication connection with the WebSocket server is successfully established, register the event listeners required by the application.
2. A Web expansion device based on WebSocket, characterized in that: The device includes a first execution unit, which is used to execute a process on a WebSocket server, wherein the first execution unit includes: A definition module is used to write a JS extended interface description file in a format and generate an adaptation layer code corresponding to the JS extended interface description file; The setting module is used to set up local service event monitoring; The first registration module is used to register the message processing function of the extended interface; The first establishment module is used to establish two-way communication with the Web terminal; The second registration module is used to register a general message processing function; The second establishment module is used to establish an event monitoring callback channel; The waiting module is used to loop and wait for the request message from the Web end; The device further includes a second execution unit, which is used to execute the process on the Web side, wherein the second execution unit includes: The first adding module is used to add the packaged JS extension library and automatically establish a communication connection with the WebSocket server when the web page is running; The second adding module is used to add the JS extension interface description file and initialize the JS extension interface; The third registration module is used to register event listeners required by the application; A development module, used to develop applications using the JS extension interface; Wherein, the second adding module includes: The mounting submodule is used to mount the function of the JS extension interface into the JS context; Setting a submodule, used to set a sending function for the function of the JS extension interface; The construction submodule is used to construct message parameters; An allocation submodule, used to allocate a unique identifier to each request message; Wherein, the third registration module includes: The registration submodule is used to register the event listeners required by the application after determining that the communication connection with the WebSocket server is successfully established.
3. A computer device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that: When the processor executes the computer program, the Web expansion method based on WebSocket as claimed in claim 1 is implemented.
4. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the program is executed by a processor, the Web expansion method based on WebSocket as claimed in claim 1 is implemented.
Citation Information
Patent Citations
Method and system for calling end capacity ports on web page
CN104301443A
Methods, systems, and computer program products for providing configurable telecommunications detail record adapter
US20050213722A1