CN110225092B - Communication method and WebSocket server - Google Patents
Communication method and WebSocket server Download PDFInfo
- Publication number
- CN110225092B CN110225092B CN201910399636.0A CN201910399636A CN110225092B CN 110225092 B CN110225092 B CN 110225092B CN 201910399636 A CN201910399636 A CN 201910399636A CN 110225092 B CN110225092 B CN 110225092B
- Authority
- CN
- China
- Prior art keywords
- websocket
- connection
- server
- http
- communication module
- Prior art date
- Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
- Active
Links
Images
Classifications
-
- H—ELECTRICITY
- H04—ELECTRIC COMMUNICATION TECHNIQUE
- H04L—TRANSMISSION OF DIGITAL INFORMATION, e.g. TELEGRAPHIC COMMUNICATION
- H04L67/00—Network arrangements or protocols for supporting network services or applications
- H04L67/01—Protocols
- H04L67/02—Protocols based on web technology, e.g. hypertext transfer protocol [HTTP]
-
- H—ELECTRICITY
- H04—ELECTRIC COMMUNICATION TECHNIQUE
- H04L—TRANSMISSION OF DIGITAL INFORMATION, e.g. TELEGRAPHIC COMMUNICATION
- H04L67/00—Network arrangements or protocols for supporting network services or applications
- H04L67/14—Session management
-
- H—ELECTRICITY
- H04—ELECTRIC COMMUNICATION TECHNIQUE
- H04L—TRANSMISSION OF DIGITAL INFORMATION, e.g. TELEGRAPHIC COMMUNICATION
- H04L67/00—Network arrangements or protocols for supporting network services or applications
- H04L67/50—Network services
- H04L67/60—Scheduling or organising the servicing of application requests, e.g. requests for application data transmissions using the analysis and optimisation of the required network resources
Landscapes
- Engineering & Computer Science (AREA)
- Computer Networks & Wireless Communication (AREA)
- Signal Processing (AREA)
- Computer And Data Communications (AREA)
Abstract
The invention relates to a communication method and a WebSocket server. The method comprises the following steps: receiving an Http request sent by an Http server, wherein the Http request comprises a calling parameter; selecting a corresponding WebSocket connection from a connection pool of the WebSocket server according to the calling parameter; processing the http request through the WebSocket connection to obtain a processing result; and returning the processing result to the Http server. The technical scheme realizes the access function of the HTT server on the single machine or the distributed nodes to the WebSocket server, and can utilize the flexibility of the HTTP protocol and the server push function of the WebSocket connection in a plurality of use scenes.
Description
Technical Field
The invention relates to the technical field of network communication, in particular to a communication method, a WebSocket server method and a WebSocket server device.
Background
In the backend service development, a situation in which the Http service and the WebSocket service coexist often occurs. The Http service provides a general interface service of a page, and the WebSocket service maintains a long connection with a client to perform debugging, pushing, and the like. The two services have communication requirements in some scenarios, such as modifying the relevant state of the WebSocket service through the Http interface, executing a relevant task that takes a long time, and the like, and therefore Http and WebSocket contextual communication is a problem that must be solved. The industry currently does not provide a relevant solution.
Disclosure of Invention
The embodiment of the invention provides a communication method and a WebSocket server. The technical scheme is as follows:
according to a first aspect of the embodiments of the present invention, there is provided a communication method applied to a WebSocket server, the method including:
receiving an Http request sent by an Http server, wherein the Http request comprises a calling parameter;
selecting a corresponding WebSocket connection from a connection pool of the WebSocket server according to the calling parameter;
processing the http request through the WebSocket connection to obtain a processing result;
and returning the processing result to the Http server.
Optionally, the invoking parameter includes: the method comprises the steps of obtaining an event name, a parameter type and parameter data, wherein the parameter data comprise a target client IP address needing to be communicated;
and each WebSocket connection in the connection pool is stored with a client IP address.
Optionally, selecting a corresponding WebSocket connection from a connection pool of the WebSocket server according to the call parameter includes:
and screening the WebSocket connection in which the IP address of the target client is stored from the connection pool.
Optionally, receiving an Http request sent by the Http server includes:
and intercepting messages through the same communication module as the Http server, wherein the communication module is an event-based asynchronous communication module.
Optionally, the method further includes:
and when the heartbeat of the WebSocket server corresponding to the browser stops, destroying the WebSocket connection corresponding to the browser from the connection pool, and releasing connection resources.
According to a second aspect of embodiments of the present invention, there is provided a WebSocket server including:
the communication module is used for receiving an Http request sent by an Http server, wherein the Http request comprises a calling parameter;
the connection pool is used for caching the currently established WebSocket connection;
the screening module is used for selecting corresponding WebSocket connection from the connection pool of the WebSocket server according to the calling parameters;
the processing module is used for processing the http request through the WebSocket connection to obtain a processing result;
and the communication module is used for returning the processing result to the Http server.
Optionally, the invoking parameter includes: the method comprises the steps of obtaining an event name, a parameter type and parameter data, wherein the parameter data comprise a target client IP address needing to be communicated;
and each WebSocket connection in the connection pool is stored with a client IP address.
Optionally, the screening module is configured to screen, from the connection pool, the WebSocket connection in which the IP address of the target client is stored.
Optionally, the communication module is the same as the communication module in the Http server, and the communication module is an event-based asynchronous communication module.
Optionally, the connection pool is further configured to destroy, when the heartbeat of the WebSocket server corresponding to the browser stops, the WebSocket connection corresponding to the browser from the connection pool, and release the connection resource.
The technical scheme provided by the embodiment of the invention can have the following beneficial effects: the access function of the HTT server to the WebSocket server on a single machine or distributed nodes is realized, and the flexibility of an HTTP protocol and the server push function of WebSocket connection can be utilized in a plurality of using scenes.
It is to be understood that both the foregoing general description and the following detailed description are exemplary and explanatory only and are not restrictive of the invention, as claimed.
Drawings
The accompanying drawings, which are incorporated in and constitute a part of this specification, illustrate embodiments consistent with the invention and together with the description, serve to explain the principles of the invention.
Fig. 1 is a flow chart of a communication method according to an embodiment of the present invention.
Fig. 2 is a block diagram of a WebSocket server according to an embodiment of the present invention.
Fig. 3 is a schematic diagram of interaction between an Http server and a WebSocket server in the embodiment of the present invention.
Detailed Description
Reference will now be made in detail to the exemplary embodiments, examples of which are illustrated in the accompanying drawings. When the following description refers to the accompanying drawings, like numbers in different drawings represent the same or similar elements unless otherwise indicated. The embodiments described in the following exemplary embodiments do not represent all embodiments consistent with the present invention. Rather, they are merely examples of apparatus and methods consistent with certain aspects of the invention, as detailed in the appended claims.
The application provides a communication method between an Http server and a WebSocket server, and the Http server and the WebSocket server can be connected in a bidirectional mode through a pipeline. Because the Http server and the WebSocket server are not necessarily on the same machine, two implementation mechanisms are provided between the Http server and the WebSocket server, if both the Http server and the WebSocket server are local, full-duplex communication is performed through a shared memory, and if the two services are distributed in multiple machines, a message mechanism is adopted.
Fig. 1 is a flowchart of a communication method according to an embodiment of the present invention, and as shown in fig. 1, the communication method is applied to a WebSocket server, and includes the following steps:
step S11, receiving an Http request sent by an Http server, wherein the Http request comprises a calling parameter;
step S12, selecting corresponding WebSocket connection from the connection pool of the WebSocket server according to the calling parameter;
step S13, processing the http request through WebSocket connection to obtain a processing result;
in step S14, the processing result is returned to the Http server.
In another embodiment, invoking the parameters includes: the method comprises the steps of obtaining an event name, a parameter type and parameter data, wherein the parameter data comprise a target client IP address needing to be communicated; and each WebSocket connection in the connection pool stores a client IP address.
In another embodiment, selecting a corresponding WebSocket connection from a connection pool of a WebSocket server according to a call parameter includes: and screening out the WebSocket connection with the IP address of the target client from the connection pool.
In another embodiment, receiving an Http request sent by an Http server includes: the messages are intercepted by the same communication module as the Http server, which is an event-based asynchronous communication module.
In another embodiment, the method further comprises: and when the heartbeat of the WebSocket server corresponding to the browser stops, destroying the WebSocket connection corresponding to the browser from the connection pool, and releasing connection resources.
The connection pool maintains all browsers connected with the WebSocket service, destroying the WebSocket connection means that the client disconnects the WebSocket connection with the WebSocket server, the destruction is processed by the WebSocket server, the bottom TCP connection is disconnected after 4 times of waving, and connection related resources such as file descriptors are released. The WebSocket server and the browser carry out heartbeat keeping, and once the browser is closed or other situations occur and the heartbeat cannot be kept continuously, the server is disconnected and resources are cleaned.
The method can realize the function of accessing the WebSocket server by the Http server on a single machine or a distributed node, and can utilize the flexibility of the Http protocol and the server push function of WebSocket connection in a plurality of use scenes.
The following are embodiments of the apparatus of the present invention that may be used to perform embodiments of the method of the present invention.
Fig. 2 is a block diagram of a WebSocket server according to an embodiment of the present invention, where the WebSocket server may be implemented as part of or all of an electronic device through software, hardware, or a combination of the software and the hardware. As shown in fig. 2, the WebSocket server includes:
the communication module 21 is configured to receive an Http request sent by an Http server, where the Http request includes a call parameter;
the connection pool 22 is used for caching the currently established WebSocket connection;
the screening module 23 is configured to select a corresponding WebSocket connection from the connection pool 22 according to the call parameter;
the processing module 24 is configured to process the http request through WebSocket connection to obtain a processing result;
and the communication module 21 is configured to return the processing result to the Http server.
Optionally, the invoking parameter includes: the method comprises the steps of obtaining an event name, a parameter type and parameter data, wherein the parameter data comprise a target client IP address needing communication;
each WebSocket connection in the connection pool 22 has a client IP address stored therein.
Optionally, the screening module 23 is configured to screen, from the connection pool 22, a WebSocket connection in which the IP address of the target client is stored.
Optionally, the communication module 21 is the same as the communication module in the Http server, and the communication module 21 is an event-based asynchronous communication module.
Optionally, the connection pool 22 is further configured to destroy, when the WebSocket server stops corresponding to the heartbeat of the browser, the WebSocket connection corresponding to the browser from the connection pool 22, and release the connection resource.
In one embodiment, assume that the initialization environment is that there is currently one HTTP request, while there are 2 WebSocket connections. The HTTP server and the WebSocket server are in bidirectional connection through a pipeline.
Fig. 3 is a schematic view of interaction between an Http server and a WebSocket server in the embodiment of the present invention, and as shown in fig. 3, an interaction flow between the Http server and the WebSocket server is specifically as follows:
the Http server sends a calling parameter through an event-based asynchronous communication module, wherein the calling parameter comprises: the event name, the parameter type and the parameter data, wherein the parameter data comprises the IP address of the target client needing to communicate.
And 2, the WebSocket server intercepts messages through the same communication module as the Http server to obtain the calling parameters.
And 3, selecting the WebSocket connection corresponding to the calling parameter from the locally maintained connection pool by the WebSocket server.
The currently established WebSocket connections are cached in the connection pool, and each WebSocket connection stores a client IP address. And screening out the WebSocket connection with the IP address of the target client from the connection pool.
4. And processing the http request through the WebSocket connection obtained through screening to obtain a processing result.
And 5, the WebSocket server returns the processing result to the Http server through the communication module.
And 6, the Http server receives the processing result through the communication module.
7. The flow ends.
Other embodiments of the invention will be apparent to those skilled in the art from consideration of the specification and practice of the disclosure disclosed herein. This application is intended to cover any variations, uses, or adaptations of the invention following, in general, the principles of the invention and including such departures from the present disclosure as come within known or customary practice within the art to which the invention pertains. It is intended that the specification and examples be considered as exemplary only, with a true scope and spirit of the invention being indicated by the following claims.
It will be understood that the invention is not limited to the precise arrangements described above and shown in the drawings and that various modifications and changes may be made without departing from the scope thereof. The scope of the invention is limited only by the appended claims.
Claims (10)
1. A communication method is characterized in that the communication method is applied to a WebSocket server under the condition that WebSocket service and HTTP service coexist in the development of backend service, wherein the HTTP service is used for providing general interface service of pages, and the WebSocket service is used for keeping long connection with a client, and the method comprises the following steps:
receiving an Http request sent by an Http server through an event-based asynchronous communication module, wherein the Http request comprises a calling parameter;
selecting a corresponding WebSocket connection from a connection pool of the WebSocket server according to the calling parameter; the connection pool of the WebSocket server caches the currently established WebSocket connection;
processing the http request through the WebSocket connection to obtain a processing result;
and returning the processing result to the Http server.
2. The method of claim 1, wherein invoking the parameter comprises: the method comprises the steps of obtaining an event name, a parameter type and parameter data, wherein the parameter data comprise a target client IP address needing to be communicated;
and each WebSocket connection in the connection pool is stored with a client IP address.
3. The method of claim 2, wherein selecting a corresponding WebSocket connection from a connection pool of the WebSocket server according to the invocation parameter comprises:
and screening the WebSocket connection in which the IP address of the target client is stored from the connection pool.
4. The method of claim 1, wherein receiving an Http request sent by an Http server comprises:
and intercepting messages through the same communication module as the Http server, wherein the communication module is an event-based asynchronous communication module.
5. The method of claim 1, further comprising:
and when the heartbeat of the WebSocket server corresponding to the browser stops, destroying the WebSocket connection corresponding to the browser from the connection pool, and releasing connection resources.
6. The WebSocket server is characterized in that the WebSocket server is a WebSocket server under the condition that WebSocket service and HTTP service coexist in backend service development, wherein the HTTP service is used for providing general interface service of pages, the WebSocket service is used for keeping long connection with a client, and the WebSocket server comprises:
the communication module is used for receiving an Http request sent by an Http server through an event-based asynchronous communication module, wherein the Http request comprises a calling parameter;
the connection pool is used for caching the currently established WebSocket connection;
the screening module is used for selecting corresponding WebSocket connection from the connection pool of the WebSocket server according to the calling parameters;
the processing module is used for processing the http request through the WebSocket connection to obtain a processing result;
and the communication module is used for returning the processing result to the Http server.
7. The server according to claim 6, wherein the invocation parameters include: the method comprises the steps of obtaining an event name, a parameter type and parameter data, wherein the parameter data comprise a target client IP address needing to be communicated;
and each WebSocket connection in the connection pool is stored with a client IP address.
8. The server of claim 7, wherein the screening module is configured to screen the WebSocket connection storing the IP address of the target client from the connection pool.
9. The server according to claim 6,
the communication module is the same as that in the Http server, and the communication module is an event-based asynchronous communication module.
10. The server according to claim 6, wherein the connection pool is further configured to destroy, when the WebSocket server stops corresponding to a heartbeat of a browser, the WebSocket connection corresponding to the browser from the connection pool, and release connection resources.
Priority Applications (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN201910399636.0A CN110225092B (en) | 2019-05-14 | 2019-05-14 | Communication method and WebSocket server |
Applications Claiming Priority (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN201910399636.0A CN110225092B (en) | 2019-05-14 | 2019-05-14 | Communication method and WebSocket server |
Publications (2)
Publication Number | Publication Date |
---|---|
CN110225092A CN110225092A (en) | 2019-09-10 |
CN110225092B true CN110225092B (en) | 2022-06-14 |
Family
ID=67821254
Family Applications (1)
Application Number | Title | Priority Date | Filing Date |
---|---|---|---|
CN201910399636.0A Active CN110225092B (en) | 2019-05-14 | 2019-05-14 | Communication method and WebSocket server |
Country Status (1)
Country | Link |
---|---|
CN (1) | CN110225092B (en) |
Citations (4)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN101990256A (en) * | 2010-08-27 | 2011-03-23 | 中兴通讯股份有限公司 | Long-connection management device and method for managing link resources of long-connection communication |
CN105306609A (en) * | 2014-06-26 | 2016-02-03 | 优视科技有限公司 | Network loading processing method and device |
CN106331215A (en) * | 2016-08-30 | 2017-01-11 | 常州化龙网络科技股份有限公司 | Data request processing system and processing method |
CN109309728A (en) * | 2018-10-26 | 2019-02-05 | 金蝶软件(中国)有限公司 | A kind of data processing method and relevant apparatus |
Family Cites Families (7)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
JP5597230B2 (en) * | 2012-08-17 | 2014-10-01 | 株式会社東芝 | Information operation device, information output device, and information operation program |
CN103108037B (en) * | 2013-01-22 | 2015-12-02 | 华为技术有限公司 | A kind of communication means, Web server and Web communication system |
CN106134217B (en) * | 2014-03-17 | 2019-11-29 | 三菱电机株式会社 | Management system, gateway apparatus, server unit, management processing execution method |
CN105743852B (en) * | 2014-12-09 | 2020-10-27 | 航天信息股份有限公司 | Method and system for realizing Socket connection maintaining communication across network gate through http |
CN105119986B (en) * | 2015-08-12 | 2018-04-03 | 国家电网公司 | A kind of Web reverse proxy methods based on pre-connection |
CN107426233A (en) * | 2017-08-08 | 2017-12-01 | 深圳先进技术研究院 | Data communication system, method, Web server and monitoring system based on B/S frameworks |
US10915506B2 (en) * | 2017-09-06 | 2021-02-09 | Oracle International Corporation | System and method for row buffering in a database environment |
-
2019
- 2019-05-14 CN CN201910399636.0A patent/CN110225092B/en active Active
Patent Citations (5)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN101990256A (en) * | 2010-08-27 | 2011-03-23 | 中兴通讯股份有限公司 | Long-connection management device and method for managing link resources of long-connection communication |
EP2563062A1 (en) * | 2010-08-27 | 2013-02-27 | ZTE Corporation | Long connection management apparatus and link resource management method for long connection communication |
CN105306609A (en) * | 2014-06-26 | 2016-02-03 | 优视科技有限公司 | Network loading processing method and device |
CN106331215A (en) * | 2016-08-30 | 2017-01-11 | 常州化龙网络科技股份有限公司 | Data request processing system and processing method |
CN109309728A (en) * | 2018-10-26 | 2019-02-05 | 金蝶软件(中国)有限公司 | A kind of data processing method and relevant apparatus |
Non-Patent Citations (1)
Title |
---|
Communicating and Displaying Real-Time Data with WebSocket;Victoria Pimentel;《IEEE Internet Computing》;20120523;全文 * |
Also Published As
Publication number | Publication date |
---|---|
CN110225092A (en) | 2019-09-10 |
Similar Documents
Publication | Publication Date | Title |
---|---|---|
EP3837604B1 (en) | In situ triggered function as a service within a service mesh | |
US7337237B2 (en) | Mechanism to provide callback capabilities for unreachable network clients | |
CN108200165B (en) | Request Transmission system, method, apparatus and storage medium | |
CN106790629A (en) | Data synchronization unit and its realize the method for data syn-chronization, client access system | |
CN108093098A (en) | A kind of domain name mapping request sending method and device | |
CN109302437B (en) | Method and device for redirecting website | |
CN108256069A (en) | A kind of method and apparatus of the cross-domain access of the page | |
CN112565406B (en) | Gray release method, gray release system and electronic equipment | |
CN106970843B (en) | Remote calling method and device | |
CN108989420A (en) | The method and system of registration service, the method and system for calling service | |
JP5213066B2 (en) | Device for changing the EPR representing a web service endpoint | |
US11516280B2 (en) | Configuration change processing for content request handling | |
CN116633993B (en) | Cross-network micro-service calling method, device, equipment and storage medium | |
CN112468585B (en) | Data transmission method and device, electronic equipment and storage medium | |
CN105677688B (en) | Page data loading method and system | |
CN110225092B (en) | Communication method and WebSocket server | |
CN113254227B (en) | Method and system for remote method call across data centers | |
CN108200126A (en) | Object method for uploading and device | |
US11621995B2 (en) | Preloading on-demand code pieces in a distributed cloud computing network | |
KR20040095653A (en) | A method and apparatus for providing a client-side local proxy object for a distributed object-oriented system | |
CN111371823A (en) | Method for client to access micro-service in non-WEB scene | |
AU2018373682A1 (en) | Method for remote management of a device connected to a residential gateway | |
CN114760312B (en) | Distributed task coordination method, device, equipment and medium | |
CN114945023B (en) | Network connection multiplexing method, device, equipment and medium | |
CN111556374B (en) | Method and device for accessing application server and video networking system |
Legal Events
Date | Code | Title | Description |
---|---|---|---|
PB01 | Publication | ||
PB01 | Publication | ||
SE01 | Entry into force of request for substantive examination | ||
SE01 | Entry into force of request for substantive examination | ||
GR01 | Patent grant | ||
GR01 | Patent grant |