0
Follow
0
View

How should a servlet-based Java Web project realize the server's active message push to users?

dengh_dhsea 注册会员
2023-02-28 01:10

The server sends messages to the client to learn about websocket

davy445 注册会员
2023-02-28 01:10

For reference only:
In servlet-based Java Web projects, the server actively push messages to users, usually can use the following ways:

1. Polling
The client sends a request to the server through AJAX or WebSocket. The server returns a response message.

This method has the advantages of good compatibility and does not require special technical support. However, frequent network requests are required, which increases the burden on the server, and the real-time performance is not high enough.

2. Long connection
A long connection is established between the client and the server. The client sends a request to the server and maintains the connection, and the server pushes a message to the client after receiving it.

This mode has the advantage of high real-time, the client and the server can interact in real time, but it needs to maintain a long connection, increasing the burden of the server, and requires special technical support.

3.WebSocket
WebSocket is an HTML5 protocol that establishes a two-way real-time communication connection between a client and a server that can send and receive messages directly.

The advantage of this method is high real-time, the client and server can interact in real time, and at the same time does not need frequent network requests and maintain a long connection, but needs special technical support.

For your specific problem, if you have implemented the server to periodically check the inventory function, you can consider using polling or long connection mode, so as to realize the server to actively push messages to users. The specific implementation process can refer to the following steps:

  • The client sends a request to the server through AJAX or WebSocket to set up a connection.

  • After receiving the request, the server sends a response message to the client, indicating that the connection is successfully established.

  • The server implements a message push method, periodically checking the inventory and pushing the latest message to the client.

  • After receiving the message pushed by the server, the client processes the message as required.

It should be noted that if polling is used, the polling frequency should be controlled to avoid frequent network requests and increase the server burden. If you use the long connection or WebSocket, maintain and manage the long connection to avoid connection interruption.

About the Author

Question Info

Publish Time
2023-02-28 01:10
Update Time
2023-02-28 01:10