In the absence of any refresh, the message box is automatically displayed, like a meager private message? This is not always requested using AJAX, right? Curious about how it is implemented. Wait for the answer
In the absence of any refresh, the message box is automatically displayed, like a meager private message? This is not always requested using AJAX, right? Curious about how it is implemented. Wait for the answer
Reply content:
In the absence of any refresh, the message box is automatically displayed, like a meager private message? This is not always requested using AJAX, right? Curious about how it is implemented. Wait for the answer
- Websocket
- Ajax round robin
- Persistent connection (Comet)
Don't take the kids upstairs. Polling and websocket are too server-consuming. You should use setinterval to request servers for sf.
Front-end JS can send a request to the server after capturing a specific event. For example, when the window gets the focus (window. onfocus), query the user message once:
Window. onblur = function () {document. title = 'out of focal point';}; window. onfocus = function () {document. title = 'get focal point ';};
If it is just a common message push (not highly real-time), you can use AJAX to round-robin every 10 minutes.
I have read about the implementation of Message notification on segmentfault. It can be implemented in two ways at the same time:
1. Bind a visible page event to the tab. When you switch the page to a page under the segmentfault.com domain name, the visible page event is triggered and a request is sent.
javascriptDocument. addEventListener (visibilityChange, function () {if (document [state] = "hidden ") {// The tab is invisible} else {// The tab is visible // request a user's message to the server}, false );
2. Open the network on the console and you can find that segmentfault is also sending requests to the server using setInterval (). The interval is about 35 seconds. Or use other polling technologies to send requests to the server.
Too many methods can be implemented, such as websocket or ajax polling.
You can simply use ajax. For example, if jQuery is used, there is a url in the background that can be directly polling.
It can be implemented through websocket. We have tested flask-websocket before. You can create a room on the server when logging on, and then add the room to the room, when the server receives a message, it determines whether the user's room exists and sends a message to the room. When the user closes the page, the connection is closed and the corresponding room is deleted.
It should all be message push, Search Keyword: cometd
Will html5 SEE be used?
The segmentfault community is also round-robin. You can open the developer tool and view the network item. You will see the request later.
If you think too much, you just need to round-robin!