Server communication method and device, storage medium and equipment
By using key-value pair storage and message processing of request identifiers and object instances in server communication, the code complexity and high maintenance costs caused by callback nesting are solved, and concise chain calls and low-cost code are realized. maintain.
Patent Information
- Application Number
- CN202010554699.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2020-06-17
- Publication Date
- 2024-11-26
- Estimated Expiration
- 2040-06-17
AI Technical Summary
In the prior art, when the front-end and the back-end server communicate, the code logic is complex due to callback nesting, and the maintenance cost is high. The traditional callback function writing makes the code confusing and difficult to maintain.
Using a server communication method, by generating key-value pairs of request identification and object instance storage, sending message messages and receiving return messages, determining the operation result of messages based on request identification, avoiding callback nesting, simplifying code logic and reducing Maintenance cost.
Nesting the code's callback into chain calls, simplifying the code structure, reducing maintenance costs, and improving the readability and maintainability of the code.
Smart Images

Figure CN113778698B_ABST
Abstract
Description
Background Art
[0002] With the rapid development and progress of computer and Internet technology, users can query various multimedia information through browsers and even interact with data online. The effectiveness of exchanging data between front-end service programs and back-end services based on the Web (World Wide Web, Global Wide Area Network, referred to as: Web) is becoming more and more important. In some specific application scenarios, the front end needs to communicate with the back-end server in a callback manner. For example, when the front end communicates with the back-end server through AJAX (asynchronous JavaScript and XML technology) or WebSocket (a new network protocol based on TCP (Transmission Control Protocol, referred to as: TCP, Transmission Control Protocol), which realizes full-duplex communication between browsers and servers, allowing servers to actively send information to clients) to interact with data, the front end must asynchronously wait for data to return.
[0003] Currently, when there is a callback function dependency, the communication between the front-end and back-end servers must be achieved through callback nesting. However, callback nesting makes the code logic complex, and when a data result changes, the entire logic needs to be reorganized, resulting in extremely high code maintenance costs.
[0004] In view of this, there is an urgent need in the art to develop a new server communication method and device.
[0005] It should be noted that the information disclosed in the above background technology section is only used to enhance the understanding of the background of the present disclosure. Summary of the invention
[0006] The purpose of the present disclosure is to provide a server communication method, a server communication device, a computer storage medium and an electronic device, thereby avoiding the defects of complex code logic and high maintenance cost in the related art at least to a certain extent.
[0007] Other features and advantages of the present disclosure will become apparent from the following detailed description, or may be learned in part by the practice of the present disclosure.
[0008] According to a first aspect of the present disclosure, a server communication method is provided, including: sending a data request and generating a first request identifier; creating an object instance, and storing the first request identifier and the object instance in a corresponding form of a key-value pair; sending a first message message containing the first request identifier to the back-end server, and receiving a second message message returned by the back-end server; when a second request identifier corresponding to the second message message is consistent with the first request identifier, determining the second message message as the operation result of the object instance.
[0009] In an exemplary embodiment of the present disclosure, the storing of the first request identifier and the object instance in the form of a key-value pair includes: creating a message pool, the message pool being a key-value pair set with the request identifier as a keyword and the object instance as a key value; storing the first request identifier and the object instance in the message pool in the form of a key-value pair.
[0010] In an exemplary embodiment of the present disclosure, when the second request identifier corresponding to the second message message is consistent with the first request identifier, the second message message is determined as the operation result of the object instance, including: when the second request identifier corresponding to the second message message is consistent with the first request identifier, obtaining the object instance corresponding to the first request identifier from the message pool and calling the callback function bound to the object instance; based on the callback function, transmitting the second message message to the object instance to determine the second message message as the operation result of the object instance.
[0011] In an exemplary embodiment of the present disclosure, after sending a data request to a back-end server and generating a first request identifier, the method further includes: obtaining a request duration corresponding to the data request; when the request duration is greater than a preset duration threshold, determining that the data request has timed out and disconnecting the session connection with the back-end server.
[0012] In an exemplary embodiment of the present disclosure, the method also includes: when the second request identifier is inconsistent with the first request identifier, determining whether the second message message corresponding to the second request identifier is a valid message; if the second message message is a valid message, storing the second message message in a memory buffer; if the second message message is an invalid message, discarding the second message message.
[0013] In an exemplary embodiment of the present disclosure, the method also includes: obtaining data request information, the data request information including at least one of the following: request content, data request type, sending timestamp, information sending status, and recipient information; encapsulating and packaging the data request information and the first request identifier to obtain the first message packet.
[0014] In an exemplary embodiment of the present disclosure, sending the first message packet including the first request identifier to the backend server includes: sending the first message packet including the first request identifier to the backend server based on a full-duplex communication protocol.
[0015] According to a second aspect of the present disclosure, a server communication device is provided, comprising: a data request module, used to send a data request to a back-end server and generate a first request identifier; an object creation module, used to create an object instance, and store the first request identifier and the object instance in a corresponding form of a key-value pair; a message sending module, used to send a first message message containing the first request identifier to the back-end server, and receive a second message message returned by the back-end server; and a determination module, used to determine the second message message as the operation result of the object instance if the second request identifier corresponding to the second message message is consistent with the first request identifier.
[0016] According to a third aspect of the present disclosure, a computer storage medium is provided, on which a computer program is stored. When the computer program is executed by a processor, the server communication method described in the first aspect is implemented.
[0017] According to a fourth aspect of the present disclosure, an electronic device is provided, comprising: a processor; and a memory for storing executable instructions of the processor; wherein the processor is configured to execute the server communication method described in the first aspect above by executing the executable instructions.
[0018] It can be seen from the above technical solutions that the server communication method, server communication device, computer storage medium and electronic device in the exemplary embodiments of the present disclosure have at least the following advantages and positive effects:
[0019] In the technical solutions provided by some embodiments of the present disclosure, on the one hand, a data request is sent to a backend server and a first request identifier is generated, and an object instance is created. The first request identifier and the object instance are stored in the form of a key-value pair, which can facilitate the subsequent rapid search for the object instance according to the request identifier. On the other hand, a first message message containing a first request identifier is sent to the backend server, and a second message message returned by the backend server is received. When the second request identifier corresponding to the second message message is consistent with the first request identifier, the second message message is determined as the operation result of the object instance. Thus, the technical problems in the related technology of deep callback nesting and complex code logic caused by data interdependence can be solved, and the callback nesting of the code is transformed into a chain call, making the code structure more beautiful and clear, simplifying the code writing logic, and reducing the maintenance cost of the code.
[0020] It is to be understood that the foregoing general description and the following detailed description are exemplary and explanatory only and are not restrictive of the present disclosure. BRIEF DESCRIPTION OF THE DRAWINGS
[0021] The accompanying drawings herein are incorporated into the specification and constitute a part of the specification, illustrate embodiments consistent with the present disclosure, and together with the specification are used to explain the principles of the present disclosure. Obviously, the accompanying drawings described below are only some embodiments of the present disclosure, and for ordinary technicians in this field, other accompanying drawings can be obtained based on these accompanying drawings without creative work.
[0022] Figure 1 A schematic diagram showing a flow chart of a server communication method in an exemplary embodiment of the present disclosure;
[0023] Figure 2 A schematic diagram showing the overall interaction flow of a server communication method in an exemplary embodiment of the present disclosure is shown;
[0024] Figure 3 A schematic diagram showing the structure of a server communication device in an exemplary embodiment of the present disclosure is shown;
[0025] Figure 4 A schematic diagram showing the structure of a computer storage medium in an exemplary embodiment of the present disclosure;
[0026] Figure 5 A schematic structural diagram of an electronic device in an exemplary embodiment of the present disclosure is shown. DETAILED DESCRIPTION
[0027] Example embodiments will now be described more fully with reference to the accompanying drawings. However, example embodiments can be implemented in a variety of forms and should not be construed as being limited to the examples set forth herein; on the contrary, these embodiments are provided so that the present disclosure will be more comprehensive and complete, and the concepts of the example embodiments are fully conveyed to those skilled in the art. The described features, structures, or characteristics may be combined in one or more embodiments in any suitable manner. In the following description, many specific details are provided to provide a full understanding of the embodiments of the present disclosure. However, those skilled in the art will appreciate that the technical solutions of the present disclosure may be practiced while omitting one or more of the specific details, or other methods, components, devices, steps, etc. may be adopted. In other cases, known technical solutions are not shown or described in detail to avoid obscuring various aspects of the present disclosure.
[0028] The terms "a", "an", "the" and "said" are used in this specification to indicate the presence of one or more elements / components / etc.; the terms "including" and "having" are used to express an open-ended inclusion and mean that additional elements / components / etc. may exist in addition to the listed elements / components / etc.; the terms "first" and "second" etc. are used only as labels and are not intended to limit the quantity of their objects.
[0029] In addition, the accompanying drawings are only schematic illustrations of the present disclosure and are not necessarily drawn to scale. The same reference numerals in the drawings represent the same or similar parts, and their repeated descriptions will be omitted. Some of the block diagrams shown in the accompanying drawings are functional entities and do not necessarily correspond to physically or logically independent entities.
[0030] At present, in some specific application scenarios (for example, the front end uses AJAX or WebSocket to interact with data, implements front-end animation effects, and implements timers and other asynchronous scenarios), the front end needs to write asynchronous code to communicate with the back-end server. The front end writes asynchronous code mainly through callbacks. If there is a callback function dependency, it must be implemented through nested callbacks.
[0031] However, on the one hand, in medium and large front-end projects, data is interdependent. When making a data request, the previous data is the dependency of the next request, which will inevitably lead to nested callbacks in the data request interface. When the nesting is too deep, the code logic is extremely complex. Especially in IM applications (Instant Messaging, Instant Messaging, referred to as: IM), where data interaction or data synchronization with the back-end server is more frequent, we need to know whether the message is sent successfully or wait for the back-end server data to return, which makes the application implementation very complex and difficult to maintain. Furthermore, if a data structure changes, the entire logic may be reorganized and re-implemented, resulting in extremely high code maintenance costs and cannot be easily changed. On the other hand, the traditional callback function writing method makes the code mixed up, and it develops horizontally instead of downward, resulting in unclear meaning and ugly code.
[0032] In the embodiments of the present disclosure, a server communication method is firstly provided, which at least to some extent overcomes the defects of the related art in that the code layers are nested, the logic is complex and the maintenance cost is high.
[0033] Figure 1 A flow chart of a server communication method in an exemplary embodiment of the present disclosure is shown. The execution subject of the server communication method may be a front end communicating with a back end server.
[0034] refer to Figure 1 According to an embodiment of the present disclosure, a server communication method comprises the following steps:
[0035] Step S110, sending a data request to a backend server and generating a first request identifier;
[0036] Step S120, creating an object instance, and storing the first request identifier and the object instance in a corresponding form of a key-value pair;
[0037] Step S130, sending a first message packet including a first request identifier to a backend server, and receiving a second message packet returned by the backend server;
[0038] Step S140: When the second request identifier corresponding to the second message packet is consistent with the first request identifier, the second message packet is determined as the operation result of the object instance.
[0039] exist Figure 1 In the technical solution provided by the illustrated embodiment, on the one hand, a data request is sent to the back-end server and a first request identifier is generated, and an object instance is created. The first request identifier and the object instance are stored in the form of a key-value pair, which can facilitate the subsequent rapid search for the object instance according to the request identifier. On the other hand, a first message message containing the first request identifier is sent to the back-end server, and a second message message returned by the back-end server is received. When the second request identifier corresponding to the second message message is consistent with the first request identifier, the second message message is determined as the operation result of the object instance. Thus, the technical problems in the related technology of deep callback nesting and complex code logic caused by data interdependence can be solved, and the callback nesting of the code is transformed into a chain call, making the code structure more beautiful and clear, simplifying the code writing logic, and reducing the maintenance cost of the code.
[0040] The following Figure 1 The specific implementation process of each step is described in detail:
[0041] In the exemplary embodiments of the present disclosure, the front end is the front-end part of the website, which runs on a browser such as a PC (Personal Computer, referred to as PC) or a mobile terminal to display web pages for users to browse. The front end can display relevant pages or information, and then the user can perform interactive operations (such as manual input or touch input information) on the front end to add, delete, search, and modify the above-mentioned relevant pages and information.
[0042] The back-end server may be a server that provides relevant feedback response information to the front-end. When the user performs interactive operations on the front-end and queries relevant resources, the front-end may send a message message containing the above query content to the back-end server. Furthermore, the back-end server may call the database associated with it to query and obtain feedback response information corresponding to the above query content, and return the feedback response information to the front-end to achieve a response to the user's interactive operation. For example, when the user performs interactive operations on the front-end and enters "What is artificial intelligence technology", the front-end may send a message containing the query content "What is artificial intelligence" to the back-end server, and then the back-end server may call the associated database to query and obtain the feedback response information "Artificial Intelligence (Artificial Intelligence), abbreviated as AI in English. It is a new technical science that studies and develops theories, methods, technologies and application systems for simulating, extending and expanding human intelligence", and returns the feedback response information to the front-end to achieve a response to the user's interactive operation.
[0043] In step S110, a data request is sent to a backend server and a first request identifier is generated.
[0044] In an exemplary embodiment of the present disclosure, the front end may establish a Websocket persistent connection with the back end server (i.e., establish a connection through the WebSocket protocol). Furthermore, for example, when user A at the front end needs to query the phone number of user B, the front end may send a data request to the back end server and generate a first request identifier, wherein the first request identifier may be a UUID (Universally Unique Identifier, abbreviated as: UUID) that identifies this data request.
[0045] When the front end sends a data request to the back end server, it can monitor the request duration corresponding to the data request in real time and set a preset duration threshold. Then, when it is monitored that the request duration is greater than the preset duration threshold, it can be determined that the session between the front end and the back end server has timed out and disconnect the session connection between the front end device and the back end server. In this way, it can avoid the technical problem that other tasks cannot be executed due to errors in the back end server or network problems, and ensure the normal operation of the front end. Exemplarily, the specific pseudo code (pseudo code is an informal language similar to English structure used to describe module structure diagrams) to disconnect the session connection when the session times out can be:
[0046] setTimeout(()=>{
[0047] / / Return false if timeout
[0048] resolve(false);
[0049] })
[0050] In step S120, an object instance is created, and the first request identifier and the object instance are stored in correspondence with each other in the form of a key-value pair.
[0051] In an exemplary embodiment of the present disclosure, the front end can create an object instance, and illustratively, a Promise object instance can be created. The Promise object instance is used to represent the final state (completion or failure) of an asynchronous operation, as well as the result value of the asynchronous operation. Through the Promise object instance, the callback nesting of the code can be turned into a chain call, simplifying the code writing of layered callbacks, making the code more concise and easier to maintain. Exemplarily, the specific pseudocode implementation of creating an object instance can be:
[0052] var resolve = null;
[0053] var promise=new Promise((resolve)=>{resolve=resolve})
[0054] Furthermore, the first request identifier and the created Promise object instance may be stored in a pre-created message pool in the form of a key-value pair. Exemplarily, a pseudo code implementation of creating a message pool may be: var hashMap = new Map().
[0055] Specifically, a message pool (keyword-key value set) can be created based on the Map object in JavaScript, where the Map object is an object that maps keys to values, the keyword key is the request identifier, and the key value value is the object instance created above. Exemplarily, referring to Table 1, the data stored in the key-value pairs stored in the message pool can be:
[0056] Table 1
[0057] Keyword Key value Request ID Promise object instance
[0058] It should be noted that there are only three states for Promise object instances: asynchronous operation "unfinished" (pending), asynchronous operation "completed" (resolved, also known as fulfilled), and asynchronous operation "failed" (rejected). Among them, this change can only occur once. Once the current state becomes "completed" or "failed", it means that there will be no new state changes. Therefore, there are only two final results of the Promise object instance. When the asynchronous operation is successful, resolve is called (that is, the state of the Promise object instance is changed from "unfinished" to "completed", and pending is changed to resolved), and the result of the asynchronous operation is passed as a parameter. When the asynchronous operation fails, reject is called (that is, the state of the Promise object instance is changed from "unfinished" to "failed", and pending is changed to rejected), and the error reported by the asynchronous operation is passed as a parameter. However, whether the asynchronous operation succeeds or fails, data can be returned through the callback function resolve. Therefore, the Promise object instance in the present disclosure can be bound to the callback function resolve.
[0059] In view of this, the data structure of the Promise object instance in the above key value can be: [Promise object instance, resolve callback function bound to the Promise object instance], and then, the data structure of the above first request identifier and the Promise object instance (which can be called PromiseBundle in the specific programming language) can be stored in the above message pool in the form of key-value pairs, so as to quickly find the object instance corresponding to the request identifier later. Exemplarily, the specific pseudocode implementation of storing the associative array of the first request identifier and the Promise object instance in the form of key-value pairs in the above message pool can be: hashMap.set(message.id,[promise,resolve]).
[0060] Thus, referring to Table 2, the data stored in the key-value pairs stored in the above message pool may be:
[0061] Table 2
[0062]
[0063]
[0064] In step S130, a first message packet including a first request identifier is sent to a backend server, and a second message packet returned by the backend server is received.
[0065] In an exemplary embodiment of the present disclosure, the front end may send a first message packet containing a first request identifier to a back-end server, wherein the message packet is a data unit exchanged and transmitted in a network, i.e., a data block to be sent by a station at one time. The message packet contains complete data information to be sent, and its length is very inconsistent, and the length is unlimited and variable.
[0066] Specifically, the data request information and the above-mentioned first request identifier can be encapsulated and packaged to obtain a first message message. The data request information may include one of the following: request content, data request type, sending timestamp, information sending status, and receiver information. Exemplarily, when the front-end user A needs to query the telephone number of user B, the request content may be "the telephone number of B", and the data request type may include: chat type, system notification type, session type, etc. The sending timestamp is the time when the first message message is sent. The information sending status may include: sending, sending successfully, and sending failed. The receiver information is the receiver of the first message message, for example: the unique identification code or IP address (Internet Protocol Address, full name Internet Protocol address, abbreviated as: IP address) of the back-end server that receives the first message message.
[0067] Encapsulation is an information hiding technology. In Java, encapsulation is achieved through the keywords private, protected and public. Encapsulation is to surround the process and data, and the access to the data can only be through the defined interface. Exemplary, the specific pseudo code implementation of creating the first message message can be: var message = new Message (str), where the str string represents the first message message containing the above data request information and the first request identifier.
[0068] By encapsulating and packaging the data request information and the first request identifier, on the one hand, the various problems caused by the separation of data and operations in the traditional structural method are completely eliminated, the reusability and maintainability of the program are improved, and the burden of programmers to maintain data and operation content is reduced. On the other hand, private data and public data can be separated, private data is protected, and possible interference between modules is reduced, thereby achieving the purpose of reducing program complexity and improving controllability.
[0069] The front end can send the first message message containing the first request identifier to the back end server through the full-duplex communication protocol. After receiving the first message message, the back end server can parse and process the first message message to obtain the first request identifier and request content contained in the first message message. Then, the back end server can query and obtain the feedback response information corresponding to the request content (for example: query and obtain the telephone number of the user B), and encapsulate and package the feedback response information and the generated second request identifier (in order to distinguish it from the request identifier generated by the front end device, the second request identifier can be consistent with the first request identifier in actual processing) to obtain the second message message. Furthermore, the second message message can be sent to the front end through the full-duplex communication protocol.
[0070] In step S140, when the second request identifier corresponding to the second message packet is consistent with the first request identifier, the second message packet is determined as the operation result of the object instance.
[0071] In an exemplary embodiment of the present disclosure, after the front end receives the second message message sent by the back end server through the full-duplex communication protocol, the second message message can be parsed to obtain the second request identifier corresponding to the second message message. Exemplarily, the specific pseudo code implementation of the front end receiving the second message message and parsing the second message message to obtain the second request identifier can be:
[0072] function Receive(message){
[0073] / / Get the session request ID
[0074] var id = message.id;
[0075] Furthermore, the second request identifier can be compared with the first request identifier. If the comparison result is that the second request identifier is consistent with the first request identifier, the second message message can be determined as the operation result of the object instance. Specifically, the Promise object instance corresponding to the first request identifier can be obtained from the message pool, and the resolve callback function bound to the Promise object instance can be called. Based on the resolve callback function, the second message message is transmitted to the Promise object instance to determine the second message message as the operation result of the Promise object instance.
[0076] Exemplarily, the front end can receive the second message and search the message pool for a request identifier that is consistent with the second request identifier. If not, the second request identifier is ignored and the next request processing is directly entered. If it is, the resolve callback function in the PromiseBundle corresponding to the first request identifier can be taken out of the message pool, and the second message is returned based on the resolve callback function. The specific pseudo code implementation of this process can be:
[0077] / / Define a receiving function, which is the callback function of WebSocket's listening and receiving messages
[0078] function Receive(message){
[0079] / / Get the session request ID
[0080] var id = message.id;
[0081] / / Search the message pool for a request identifier that matches the second request identifier
[0082] var promiseBundle=hashMap.get(id);
[0083] / / If it does not exist, directly proceed to the next request processing
[0084] if(!promiseBundle)return next(message);
[0085] / / If it exists, take out the resolve in promiseBundle and return
[0086] var[promise,resolve]=promiseBundle;
[0087] resolve(message)
[0088] }
[0089] If the comparison result is: the second request identifier is inconsistent with the first request identifier, then it can be determined whether the second message message corresponding to the second request identifier is a valid message. Specifically, it can be determined whether the second request identifier is consistent with the request identifiers corresponding to other data requests to identify whether the second message message is a valid message. When the second message message is identified as a valid message, the second message message can be stored in a memory buffer (a buffer is a memory area, which is used between input and output devices and the CPU (Central Processing Unit, central processing unit, referred to as: CPU) to store data). Thereby, the loss of valid information can be avoided, and the low-speed input and output devices and the high-speed CPU can be coordinated to avoid the low-speed input and output devices occupying the CPU, freeing up the CPU so that it can work efficiently. When the second message message is identified as an invalid message, the second message message can be discarded to avoid memory occupation of invalid information and improve the system operation speed.
[0090] In an exemplary embodiment of the present disclosure, the async and await syntax can be used to synchronously wait for the result of the current session operation. Await syntax can only be used in the context of an async function. Await can be followed by any JS (short for JavaScript, which is a high-level, multi-paradigm, interpreted programming language) expression, but await is mainly used to wait for a Promise object instance to become resolved. When await is for a promise object instance, the asynchronous function will stop executing and wait for the result of the operation of the Promise object instance. Exemplarily, the specific pseudocode implementation of synchronously waiting for the result of the operation corresponding to this request using async and await syntax can be:
[0091] / / In the business logic code, define a session operation and use the async function at the call point
[0092] async function Work(){
[0093] / / Synchronously wait for the result of this session
[0094] var resultMessage=await Send('test');
[0095] / / Get the return value of this session for the next step
[0096] }
[0097] Furthermore, when the await syntax receives the return value of the above-mentioned resolve callback function (the response feedback information contained in the second message packet), the front end can enter the next business processing logic.
[0098] Based on the above technical solutions and the above implementation codes, the present disclosure can solve the technical problems in the related technologies that the code is nested in layers, resulting in complex logic, data affecting the entire system, and difficult code maintenance, simplify the code structure and code writing logic, avoid the code writing method of layered callbacks, and reduce the code maintenance cost.
[0099] For example, please refer to Figure 2 , Figure 2 The overall flow diagram of the server communication method in an exemplary embodiment of the present disclosure is shown, including steps S201-S208. Figure 2 Specific implementation methods are explained.
[0100] In step S201, start;
[0101] In step S202, the front end obtains data request information, which includes at least one of the following: request content, data request type, sending timestamp, information sending status, and recipient information;
[0102] In step S203, the front end generates a first request identifier, and encapsulates and packages the first request identifier with the above data request information to obtain a first message packet, and sends the first message packet to the back end server;
[0103] In step S204, the front end creates an object instance, and stores the first request identifier and the object instance in a corresponding manner in the message pool;
[0104] In step S205, the front end receives the second message packet returned by the back end server, and parses it to obtain a second request identifier;
[0105] In step S206, the front end determines whether the second request identifier is registered (ie, whether the second request identifier is consistent with the first request identifier) through the above message pool;
[0106] In step S207, if yes, the callback function bound to the above object instance is called, and the second message packet is transmitted to the object instance based on the callback function to determine the second message packet as the operation result of the object instance.
[0107] In step S208, end;
[0108] If not, jump to step S208 and end.
[0109] The present disclosure also provides a server communication device, Figure 3A schematic diagram showing the structure of a server communication device in an exemplary embodiment of the present disclosure is shown; Figure 3 As shown, the server communication device 300 may include a data request module 301, an object creation module 302, a message sending module 303 and a determination module 304. Among them:
[0110] The data request module 301 is used to send a data request to a backend server and generate a first request identifier.
[0111] In an exemplary embodiment of the present disclosure, the data request module is used to send a data request to a backend server and generate a first request identifier.
[0112] The object creation module 302 is used to create an object instance and store the first request identifier and the object instance in a corresponding form of a key-value pair.
[0113] In an exemplary embodiment of the present disclosure, the object creation module is used to create a message pool, which is a key-value pair set with a request identifier as a keyword and an object instance as a key value; the first request identifier and the object instance are stored in the message pool in the form of a key-value pair.
[0114] The message sending module 303 is used to send a first message message including a first request identifier to a backend server, and receive a second message message returned by the backend server.
[0115] In an exemplary embodiment of the present disclosure, a message sending module is used to obtain data request information, and the data request information includes at least one of the following: request content, data request type, sending timestamp, information sending status, and recipient information; the data request information and the first request identifier are encapsulated and packaged to obtain a first message message.
[0116] In an exemplary embodiment of the present disclosure, the message sending module is used to send a first message message including a first request identifier to a backend server based on a full-duplex communication protocol.
[0117] The determination module 304 is configured to determine the second message as an operation result of the object instance if the second request identifier corresponding to the second message is consistent with the first request identifier.
[0118] In an exemplary embodiment of the present disclosure, the determination module is used to obtain an object instance corresponding to the first request identifier from a message pool and call a callback function bound to the object instance when the second request identifier corresponding to the second message message is consistent with the first request identifier; based on the callback function, the second message message is transmitted to the object instance to determine the second message message as the operation result of the object instance.
[0119] In an exemplary embodiment of the present disclosure, after sending a data request to a back-end server and generating a first request identifier, a determination module is used to obtain a request duration corresponding to the data request; when the request duration is greater than a preset duration threshold, it is determined that the data request has timed out and the session connection between the front-end and the back-end server is disconnected.
[0120] In an exemplary embodiment of the present disclosure, the determination module is used to determine whether the second message message corresponding to the second request identifier is a valid message when the second request identifier is inconsistent with the first request identifier; if the second message message is a valid message, the second message message is stored in a memory buffer; if the second message message is an invalid message, the second message message is discarded.
[0121] The specific details of each module in the above-mentioned server communication device have been described in detail in the corresponding server communication method, so they will not be repeated here.
[0122] It should be noted that, although several modules or units of the device for action execution are mentioned in the above detailed description, this division is not mandatory. In fact, according to the embodiments of the present disclosure, the features and functions of two or more modules or units described above can be embodied in one module or unit. On the contrary, the features and functions of one module or unit described above can be further divided into multiple modules or units to be embodied.
[0123] In addition, although the steps of the method in the present disclosure are described in a specific order in the drawings, this does not require or imply that the steps must be performed in this specific order, or that all the steps shown must be performed to achieve the desired results. Additionally or alternatively, some steps may be omitted, multiple steps may be combined into one step, and / or one step may be decomposed into multiple steps, etc.
[0124] Through the description of the above implementation, it is easy for those skilled in the art to understand that the example implementation described here can be implemented by software, or by software combined with necessary hardware. Therefore, the technical solution according to the implementation of the present disclosure can be embodied in the form of a software product, which can be stored in a non-volatile storage medium (which can be a CD-ROM, a USB flash drive, a mobile hard disk, etc.) or on a network, including several instructions to enable a computing device (which can be a personal computer, a server, a mobile terminal, or a network device, etc.) to execute the method according to the implementation of the present disclosure.
[0125] In the exemplary embodiments of the present disclosure, a computer storage medium capable of implementing the above method is also provided. A program product capable of implementing the above method of this specification is stored thereon. In some possible embodiments, various aspects of the present disclosure may also be implemented in the form of a program product, which includes a program code, and when the program product is run on a terminal device, the program code is used to enable the terminal device to perform the steps according to various exemplary embodiments of the present disclosure described in the above "Exemplary Method" section of this specification.
[0126] refer to Figure 4 As shown, a program product 400 for implementing the above method according to an embodiment of the present disclosure is described, which can adopt a portable compact disk read-only memory (CD-ROM) and include program code, and can be run on a terminal device, such as a personal computer. However, the program product of the present disclosure is not limited thereto. In this document, a readable storage medium can be any tangible medium containing or storing a program, which can be used by or in combination with an instruction execution system, an apparatus or a device.
[0127] The program product may use any combination of one or more readable media. The readable medium may be a readable signal medium or a readable storage medium. The readable storage medium may be, for example, but not limited to, an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, device or device, or any combination of the above. More specific examples (non-exhaustive list) of readable storage media include: an electrical connection with one or more wires, a portable disk, 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 disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the above.
[0128] Computer readable signal media may include data signals propagated in baseband or as part of a carrier wave, in which readable program code is carried. Such propagated data signals may take a variety of forms, including but not limited to electromagnetic signals, optical signals, or any suitable combination of the above. Readable signal media may also be any readable medium other than a readable storage medium, which may send, propagate, or transmit a program for use by or in conjunction with an instruction execution system, apparatus, or device.
[0129] The program code embodied on the readable medium may be transmitted using any appropriate medium, including but not limited to wireless, wired, optical cable, RF, etc., or any suitable combination of the foregoing.
[0130] Program code for performing the operations of the present disclosure may be written in any combination of one or more programming languages, including object-oriented programming languages such as Java, C++, etc., and conventional procedural programming languages such as "C" or similar programming languages. The program code may be executed entirely on the user computing device, partially on the user device, as a separate software package, partially on the user computing device and partially on a remote computing device, or entirely on a remote computing device or server. In cases involving a remote computing device, the remote computing device may be connected to the user computing device through any type of network, including a local area network (LAN) or a wide area network (WAN), or may be connected to an external computing device (e.g., through the Internet using an Internet service provider).
[0131] In addition, in an exemplary embodiment of the present disclosure, an electronic device capable of implementing the above method is also provided.
[0132] Those skilled in the art will appreciate that various aspects of the present disclosure may be implemented as systems, methods or program products. Therefore, various aspects of the present disclosure may be specifically implemented in the following forms, namely: complete hardware implementation, complete software implementation (including firmware, microcode, etc.), or a combination of hardware and software, which may be collectively referred to herein as "circuits", "modules" or "systems".
[0133] Refer to the following Figure 5 The electronic device 500 according to this embodiment of the present disclosure is described. Figure 5 The electronic device 500 shown is merely an example and should not bring any limitation to the functions and scope of use of the embodiments of the present disclosure.
[0134] like Figure 5 As shown, the electronic device 500 is in the form of a general computing device. The components of the electronic device 500 may include, but are not limited to: the at least one processing unit 510, the at least one storage unit 520, a bus 530 connecting different system components (including the storage unit 520 and the processing unit 510), and a display unit 540.
[0135] The storage unit stores program codes, which can be executed by the processing unit 510, so that the processing unit 510 performs the steps according to various exemplary embodiments of the present disclosure described in the above “Exemplary Method” section of this specification. For example, the processing unit 510 can perform the following steps: Figure 1As shown in: Step S110, sending a data request to the backend server and generating a first request identifier; Step S120, creating an object instance, and storing the first request identifier and the object instance in the form of a key-value pair; Step S130, sending a first message message including the first request identifier to the backend server, and receiving a second message message returned by the backend server; Step S140, when the second request identifier corresponding to the second message message is consistent with the first request identifier, determining the second message message as the operation result of the object instance.
[0136] The storage unit 520 may include a readable medium in the form of a volatile storage unit, such as a random access storage unit (RAM) 5201 and / or a cache storage unit 5202 , and may further include a read-only storage unit (ROM) 5203 .
[0137] The storage unit 520 may also include a program / utility 5204 having a set (at least one) of program modules 5205, such program modules 5205 including but not limited to: an operating system, one or more application programs, other program modules, and program data, each of which or some combination may include an implementation of a network environment.
[0138] Bus 530 may represent one or more of several types of bus structures, including a memory unit bus or memory unit controller, a peripheral bus, an accelerated graphics port, a processing unit, or a local bus using any of a variety of bus architectures.
[0139] The electronic device 500 may also communicate with one or more external devices 600 (e.g., keyboards, pointing devices, Bluetooth devices, etc.), may also communicate with one or more devices that enable a user to interact with the electronic device 500, and / or may communicate with any device that enables the electronic device 500 to communicate with one or more other computing devices (e.g., routers, modems, etc.). Such communication may be performed via an input / output (I / O) interface 550. Furthermore, the electronic device 500 may also communicate with one or more networks (e.g., local area networks (LANs), wide area networks (WANs), and / or public networks, such as the Internet) via a network adapter 560. As shown, the network adapter 560 communicates with other modules of the electronic device 500 via a bus 530. It should be understood that, although not shown in the figure, other hardware and / or software modules may be used in conjunction with the electronic device 500, including but not limited to: microcode, device drivers, redundant processing units, external disk drive arrays, RAID systems, tape drives, and data backup storage systems.
[0140] Through the description of the above implementation, it is easy for those skilled in the art to understand that the example implementation described here can be implemented by software, or by software combined with necessary hardware. Therefore, the technical solution according to the implementation of the present disclosure can be embodied in the form of a software product, which can be stored in a non-volatile storage medium (which can be a CD-ROM, a USB flash drive, a mobile hard disk, etc.) or on a network, including several instructions to enable a computing device (which can be a personal computer, a server, a terminal device, or a network device, etc.) to execute the method according to the implementation of the present disclosure.
[0141] In addition, the above-mentioned figures are only schematic illustrations of the processes included in the method according to the exemplary embodiments of the present disclosure, and are not intended to be limiting. It is easy to understand that the processes shown in the above-mentioned figures do not indicate or limit the time sequence of these processes. In addition, it is also easy to understand that these processes can be performed synchronously or asynchronously, for example, in multiple modules.
[0142] Those skilled in the art will readily appreciate other embodiments of the present disclosure after considering the specification and practicing the invention disclosed herein. This application is intended to cover any variations, uses, or adaptations of the present disclosure that follow the general principles of the present disclosure and include common knowledge or customary technical means in the art that are not disclosed in the present disclosure. The specification and examples are to be considered as exemplary only, and the true scope and spirit of the present disclosure are indicated by the claims.
Claims
1. A server communication method, characterized in that: include: Sending a data request to a backend server and generating a first request identifier; Creating an object instance, and storing the first request identifier and the object instance in a corresponding form of a key-value pair; Sending a first message packet including the first request identifier to the backend server, and receiving a second message packet returned by the backend server; When the second request identifier corresponding to the second message is consistent with the first request identifier, obtaining the object instance corresponding to the first request identifier and calling the callback function bound to the object instance; transmitting the second message message to the object instance based on the callback function, so as to determine the second message message as an operation result of the object instance; When the second request identifier is inconsistent with the first request identifier, determining whether the second message message corresponding to the second request identifier is a valid message; If the second message is a valid message, storing the second message in a memory buffer; If the second message packet is an invalid message, the second message packet is discarded.
2. The method according to claim 1, characterized in that The storing the first request identifier and the object instance in a corresponding form of a key-value pair includes: Creating a message pool, wherein the message pool is a key-value pair set with the request identifier as a keyword and the object instance as a key value; The first request identifier and the object instance are stored in the message pool in a corresponding form of a key-value pair.
3. The method according to claim 2, characterized in that The obtaining of the object instance corresponding to the first request identifier and calling the callback function bound to the object instance includes: When the second request identifier corresponding to the second message is consistent with the first request identifier, an object instance corresponding to the first request identifier is obtained from the message pool and a callback function bound to the object instance is called.
4. The method according to any one of claims 1 to 3, characterized in that: After sending the data request to the backend server and generating the first request identifier, the method further includes: Obtaining the request duration corresponding to the data request; When the request duration is greater than a preset duration threshold, it is determined that the data request has timed out and the session connection with the backend server is disconnected.
5. The method according to any one of claims 1 to 3, characterized in that: The method further comprises: Obtaining data request information, wherein the data request information includes at least one of the following: request content, data request type, sending timestamp, information sending status, and recipient information; The data request information and the first request identifier are encapsulated and packaged to obtain the first message packet.
6. The method according to any one of claims 1 to 3, characterized in that: The sending the first message message including the first request identifier to the backend server includes: Based on the full-duplex communication protocol, a first message packet including the first request identifier is sent to the back-end server.
7. A server communication device, characterized in that: include: A data request module, used to send a data request to a backend server and generate a first request identifier; An object creation module, used for creating an object instance, and storing the first request identifier and the object instance in a corresponding form of a key-value pair; A message sending module, used for sending a first message message including the first request identifier to the back-end server, and receiving a second message message returned by the back-end server; A determination module, configured to obtain an object instance corresponding to the first request identifier and call a callback function bound to the object instance if the second request identifier corresponding to the second message message is consistent with the first request identifier; transmit the second message message to the object instance based on the callback function to determine the second message message as an operation result of the object instance; The determination module is further used to determine whether the second message message corresponding to the second request identifier is a valid message when the second request identifier is inconsistent with the first request identifier; If the second message is a valid message, storing the second message in a memory buffer; If the second message packet is an invalid message, the second message packet is discarded.
8. A computer storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the server communication method according to any one of claims 1 to 6 is implemented.
9. An electronic device, characterized in that: include: processor; as well as A memory, configured to store executable instructions of the processor; The processor is configured to execute the server communication method according to any one of claims 1 to 6 by executing the executable instructions.
