How to analyze the implementation process of SpringMVC4.1 server-side push
This article introduces how to analyze the SpringMVC4.1 server-side push implementation process, the content is very detailed, interested friends can refer to, hope to be helpful to you.
Server-side push
SSE (server send event) is a technology that server-side pushes messages to browsers, rather than our regular browsers like server request and then respond. When we need to use server to actively push data to browsers, please consider using this technology, rather than considering websocket; with two-way communication function, we can also use ajax polling server before, the burden on the server is heavy. The principle of sse is that after making a request to server, server will hold on to the request (the request status in the browser is pending), wait for the data to be returned to the browser, and then initiate the next request, and so on. All major browsers support sending events to the server, except Internet Explorer (6pm7d8p9).
Example
Server push controller
Package com.wisely.web;import java.util.Random;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;@Controllerpublic class SSEController {/ / Note produces= "text/event-stream" @ RequestMapping (value= "/ push", produces= "text/event-stream") public @ ResponseBody String push () {Random r = new Random (); try {Thread.sleep (5000);} catch (InterruptedException e) {e.printStackTrace () } return "data:Testing 1jue 2jue 3" + r.nextInt () + "\ n\ n";}}
Page code
If (!! window.EventSource) {var source = new EventSource ('push'); / / for http://localhost:8080/testSpringMVC/push slots. Data + "$(" # msg_from_server ") .html (s); source.addEventListener (' open', function (e) {console.log (" connection opened. ");}, false) Source.addEventListener ('error', function (e) {if (e.readyState = = EventSource.CLOSED) {console.log ("connection closed");} else {console.log (e.readyState);}}, false);} else {console.log ("no sse");}
On how to parse the SpringMVC4.1 server-side push implementation process is shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.