Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to analyze the implementation process of SpringMVC4.1 server-side push

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report