Real-time picture data transmission method based on Websocket protocol
By setting up user subscription lists in the WebSocket protocol and using asynchronous tasks to manage image data transmission, the connection stability, subscription management, task scheduling and fault tolerance of image transmission in the existing technology is solved, and efficient and reliable real-time image data transmission is achieved. , suitable for smart home monitoring and other scenarios.
Patent Information
- Application Number
- CN202411419112.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-10-11
- Publication Date
- 2025-01-14
AI Technical Summary
When using the WebSocket protocol for image transmission, there are problems such as poor connection stability, inflexible subscription management, low task scheduling efficiency, insufficient fault-tolerant processing, and poor resource management, resulting in interruption of data transmission, degradation of system performance and stability. .
Use the WebSocket protocol to establish a server and client connection, set up a user subscription list, use asynchronous tasks to manage image data acquisition and push, design exception handling and resource management mechanisms, and ensure real-time and stability of data transmission.
It realizes efficient and reliable real-time image data transmission, improves the performance and stability of the system in high concurrent scenarios, ensures the continuity and reliability of data transmission, and avoids the problems of resource leakage and excessive system load.
Smart Images

Figure CN119316494A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of picture transmission, and in particular to a real-time picture data transmission method based on the Websocket protocol. Background Art
[0002] In modern network applications, real-time data transmission is becoming more and more important. The traditional HTTP protocol has limitations in real-time and continuous connection, and cannot meet the high-frequency, low-latency data transmission requirements in some application scenarios. To solve these problems, the WebSocket protocol can be used. WebSocket is a full-duplex communication protocol on a single TCP connection. It establishes a persistent connection through a handshake, which can significantly reduce communication delays and overhead.
[0003] Real-time image transmission is a core requirement in many application scenarios, such as smart home monitoring, remote medical image transmission, industrial equipment status monitoring, etc. In these scenarios, the device needs to continuously transmit the captured image data to the server, and the server pushes the corresponding image data to the user in real time based on the client's subscription request. However, in actual use, the traditional HTTP method has bottlenecks in connection maintenance, data transmission efficiency and real-time performance.
[0004] The WebSocket protocol is an ideal choice for real-time image transmission due to its efficient two-way communication characteristics. However, there are still many challenges in implementing an efficient and reliable WebSocket image transmission system. First, the system needs to support dynamic subscription management for multiple users to ensure that users can subscribe or unsubscribe from image data in real time. Second, in order to improve the performance and stability of the system, an efficient task scheduling mechanism must be designed to ensure the timely acquisition and transmission of image data. Finally, in the event of network fluctuations or device abnormalities, the system should have good fault tolerance and automatic recovery mechanisms to ensure the continuity and reliability of data transmission.
[0005] In the prior art, an example of using the WebSocket protocol to transmit an image is as follows:
[0006] Step 1: Use the WebSocket protocol to establish a connection between the server and the client. The server uses the WebSocket library to listen for connection requests and accept client connections.
[0007] Step 2: Maintain a user subscription list on the server side and store the device ID subscribed by each connected client.
[0008] Step 3: The client sends a subscription request via WebSocket, and the server updates the user subscription list based on the request, including adding, deleting, or clearing the subscription device ID.
[0009] Step 4: The server periodically obtains image data from the specified device through a scheduled task or an asynchronous task, and the image data is processed using the Base64 encoding format.
[0010] Step 5: The server pushes the acquired image data to the corresponding client through WebSocket based on the user subscription list.
[0011] Step 6: After the client receives the image data pushed by the server, it decodes and displays it in the user interface.
[0012] The above method of using the WebSocket protocol to transmit images has the following disadvantages:
[0013] 1. Poor connection stability: The existing technology does not have a disconnection and reconnection mechanism for WebSocket connections. In the event of network fluctuations or connection interruptions, the connection cannot be automatically restored, resulting in data transmission interruption.
[0014] 2. Lack of subscription management: In the existing solution, the management of user subscription relationships is not flexible enough, and it is impossible to dynamically add, delete or update subscription devices, which reduces the flexibility of the system and user experience.
[0015] 3. Low task scheduling efficiency: In the existing technology, image data acquisition and push tasks may adopt synchronous methods or inefficient asynchronous task management, resulting in reduced system performance and failure to meet the needs of real-time data transmission in high-concurrency scenarios.
[0016] 4. Insufficient fault-tolerant processing: There is no well-designed exception handling mechanism. In the event of image data acquisition failure or transmission error, effective error handling and recovery cannot be performed, affecting the stability and reliability of the system.
[0017] 5. Poor resource management: Existing technologies do not consider the reasonable release and management of resources, which may lead to resource leakage or excessive system load, affecting the long-term stable operation of the system.
[0018] In summary, in order to meet the needs of real-time image transmission applications, an efficient and reliable real-time image data transmission system and method based on the WebSocket protocol is urgently needed. This system can not only realize dynamic subscription management of multiple users, but also ensure the real-time, stability and fault tolerance of data transmission through asynchronous tasks and exception handling mechanisms. Summary of the invention
[0019] The present invention provides a real-time picture data transmission method based on the Websocket protocol, which is used to solve the technical problems existing in the above-mentioned prior art.
[0020] To achieve the above object, the present invention provides a real-time picture data transmission method based on the Websocket protocol, which comprises:
[0021] S1: Use the WebSocket protocol to establish a connection between the server and at least one client. The server uses the WebSocket library to listen to the connection request from the client and accept the client connection;
[0022] S2: A user subscription list is set in the server. The user subscription list is used to store user subscription data. The server sets up a piece of user subscription data for each client connected to the server. Each piece of user subscription data includes a subscription device ID and subscription content in the client. The subscription content is at least one channel name subscribed by the client. Each channel name corresponds to an image device. The initial state of the user subscription list is empty.
[0023] S3: The client sends a subscription request to the server through the WebSocket protocol. The server updates the user subscription list in real time according to the subscription request. Updating the user subscription list includes adding the subscription device ID to the user subscription list, deleting the subscription device ID from the user subscription list, or keeping the existing user subscription list unchanged.
[0024] S4: When the status of the user subscription list changes from empty to non-empty, an asynchronous task is created in the server, and the server sends corresponding image data to each client connected to it in real time according to the user subscription list;
[0025] S5: When the status of the user subscription list changes from non-empty to empty, the server actively ends the asynchronous task and releases the memory.
[0026] In one embodiment of the present invention, step S4 includes the following sub-steps:
[0027] S41: The server periodically obtains image data from the corresponding image device through an asynchronous task according to the user subscription list;
[0028] S42: The server pushes the acquired image data to the corresponding client via the WebSocket protocol;
[0029] S43: After receiving the image data pushed by the server, the client decodes and displays it in the user interface.
[0030] In one embodiment of the present invention, in step S41, the server processes the image data using the Base64 encoding format and then pushes it to the client.
[0031] The real-time picture data transmission method based on the Websocket protocol provided by the present invention has the following advantages:
[0032] (1) An efficient asynchronous task scheduling mechanism is adopted to optimize the execution efficiency of image data acquisition and push tasks, so that the system can maintain good performance and stability in high-concurrency scenarios. Through a reasonable task creation and deletion mechanism and parallel processing, the system's concurrent processing capability and task execution efficiency are improved, ensuring that timely data transmission services can still be provided in a high-concurrency environment.
[0033] (2) Designed reasonable resource management and optimization strategies to avoid resource leakage and system overload problems, ensure efficient use and reasonable release of resources, prevent resource leakage, and ensure the long-term stable operation of the system.
[0034] (3) A comprehensive exception handling and recovery mechanism has been designed to ensure that the system can recover in time in the event of image data acquisition failure or transmission error, thereby improving the reliability and stability of the system.
[0035] (4) It is an efficient and reliable real-time transmission solution for image data, which is suitable for a variety of application scenarios, such as smart home monitoring, telemedicine, industrial equipment monitoring, etc. The image device side continuously transmits the captured image data to the server side, and the server pushes it to the user in real time based on the user's subscription request, realizing the real-time transmission of image data from the image device side to the user side. BRIEF DESCRIPTION OF THE DRAWINGS
[0036] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.
[0037] Figure 1 This is a flowchart of a real-time picture data transmission method based on the Websocket protocol according to an embodiment of the present invention;
[0038] Figure 2 This is a schematic diagram of a user subscription data. DETAILED DESCRIPTION
[0039] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0040] Figure 1FIG. 1 is a flowchart of a method for transmitting real-time picture data based on the Websocket protocol according to an embodiment of the present invention. Figure 1 As shown, the real-time picture data transmission method based on the Websocket protocol provided by the present invention includes:
[0041] S1: Use the WebSocket protocol to establish a connection between the server and at least one client. The server uses the WebSocket library to listen to the connection request from the client and accept the client connection;
[0042] S2: A user subscription list is set up in the server. The user subscription list is used to store user subscription data. The server sets up a user subscription data for each client connected to the server, such as Figure 2 As shown, each user subscription data includes the subscription device ID and subscription content in the client. The subscription content is at least one channel name subscribed by the client. Each channel name corresponds to an image device. The initial state of the user subscription list is empty.
[0043] The picture device is connected to the server, and the subscription content in each piece of user subscription data includes at least a channel name, that is, each client requests picture data from at least one picture device.
[0044] S3: The client sends a subscription request to the server through the WebSocket protocol. The server updates the user subscription list in real time according to the subscription request. Updating the user subscription list includes adding the subscription device ID to the user subscription list, deleting the subscription device ID from the user subscription list, or keeping the existing user subscription list unchanged.
[0045] Once a client has a subscription requirement, it can initiate a subscription request to the server at any time. If a client initiates a subscription request to the server for the first time, the user subscription list in the server does not include the user subscription data set up for the client. At this time, a new user subscription data corresponding to the client will be added to the user subscription list. If the client no longer needs the server to send image data, the client can set a command in the subscription request to delete the corresponding user subscription data from the server.
[0046] S4: When the status of the user subscription list changes from empty to non-empty, an asynchronous task is created in the server, and the server sends corresponding image data to each client connected to it in real time according to the user subscription list;
[0047] The status of the user subscription list is consistent with the existence of the asynchronous task. When the status of the user subscription list is empty, the asynchronous task ends. When the status of the user subscription list is not empty, the asynchronous task is created.
[0048] S5: When the status of the user subscription list changes from non-empty to empty, the server actively ends the asynchronous task and releases the memory.
[0049] When the user subscription list is empty, that is, no client requests image data from the server, the asynchronous task ends.
[0050] In one embodiment of the present invention, step S4 includes the following sub-steps:
[0051] S41: The server periodically obtains image data from the corresponding image device through an asynchronous task according to the user subscription list;
[0052] S42: The server pushes the acquired image data to the corresponding client via the WebSocket protocol;
[0053] S43: After receiving the image data pushed by the server, the client decodes and displays it in the user interface.
[0054] In one embodiment of the present invention, in step S41, the server processes the image data using the Base64 encoding format and then pushes it to the client.
[0055] The real-time picture data transmission method based on the Websocket protocol provided by the present invention has the following advantages:
[0056] (1) An efficient asynchronous task scheduling mechanism is adopted to optimize the execution efficiency of image data acquisition and push tasks, so that the system can maintain good performance and stability in high-concurrency scenarios. Through a reasonable task creation and deletion mechanism and parallel processing, the system's concurrent processing capability and task execution efficiency are improved, ensuring that timely data transmission services can still be provided in a high-concurrency environment.
[0057] (2) Designed reasonable resource management and optimization strategies to avoid resource leakage and system overload problems, ensure efficient use and reasonable release of resources, prevent resource leakage, and ensure the long-term stable operation of the system.
[0058] (3) A comprehensive exception handling and recovery mechanism has been designed to ensure that the system can recover in time in the event of image data acquisition failure or transmission error, thereby improving the reliability and stability of the system.
[0059] (4) It is an efficient and reliable real-time transmission solution for image data, which is suitable for a variety of application scenarios, such as smart home monitoring, telemedicine, industrial equipment monitoring, etc. The image device side continuously transmits the captured image data to the server side, and the server pushes it to the user in real time based on the user's subscription request, realizing the real-time transmission of image data from the image device side to the user side.
[0060] Those skilled in the art can understand that the accompanying drawings are only schematic diagrams of an embodiment, and the modules or processes in the accompanying drawings are not necessarily required to implement the present invention.
[0061] Those skilled in the art can understand that the modules in the device in the embodiment can be distributed in the device in the embodiment according to the description of the embodiment, or can be changed accordingly and located in one or more devices different from the embodiment. The modules in the above embodiment can be combined into one module, or can be further divided into multiple sub-modules.
[0062] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit it. Although the present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or make equivalent replacements for some of the technical features therein. However, these modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present invention.
Claims
1. A real-time picture data transmission method based on Websocket protocol, characterized in that: include: S1: Use the WebSocket protocol to establish a connection between the server and at least one client. The server uses the WebSocket library to listen to the connection request from the client and accept the client connection; S2: A user subscription list is set in the server. The user subscription list is used to store user subscription data. The server sets up a piece of user subscription data for each client connected to the server. Each piece of user subscription data includes a subscription device ID and subscription content in the client. The subscription content is at least one channel name subscribed by the client. Each channel name corresponds to an image device. The initial state of the user subscription list is empty. S3: The client sends a subscription request to the server through the WebSocket protocol. The server updates the user subscription list in real time according to the subscription request. Updating the user subscription list includes adding the subscription device ID to the user subscription list, deleting the subscription device ID from the user subscription list, or keeping the existing user subscription list unchanged. S4: When the status of the user subscription list changes from empty to non-empty, an asynchronous task is created in the server, and the server sends corresponding image data to each client connected to it in real time according to the user subscription list; S5: When the status of the user subscription list changes from non-empty to empty, the server actively ends the asynchronous task and releases the memory.
2. The real-time picture data transmission method based on the Websocket protocol according to claim 1 is characterized in that: Step S4 includes the following sub-steps: S41: The server periodically obtains image data from the corresponding image device through an asynchronous task according to the user subscription list; S42: The server pushes the acquired image data to the corresponding client via the WebSocket protocol; S43: After receiving the image data pushed by the server, the client decodes and displays it in the user interface.
3. The real-time picture data transmission method based on the Websocket protocol according to claim 1 is characterized in that: In step S41, the server processes the image data using the Base64 encoding format and then pushes it to the client.
Citation Information
Cited By
Real-time data transmission method, device, electronic equipment and system
CN120602465A