In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
How the springboot server pushes SSE? in view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.
Package com.demo.action;import com.demo.serviceI.DemoService;import com.fasterxml.jackson.core.JsonProcessingException;import com.fasterxml.jackson.databind.ObjectMapper;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.jackson.JsonObjectDeserializer;import org.springframework.http.MediaType;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;import org.springframework.web.bind.annotation.RestController;import java.util.HashMap;import java.util.Map;import java.util.Random @ RestControllerpublic class DemoAction {@ Autowired private DemoService demoService / * Health check json string Simulation * @ return * / @ RequestMapping (value = "health.json") public String healt () {return "{\" status\ ":\" UP\ ",\" diskSpace\ ": {\" status\ ":\" UP\ ",\" total\ ": 249769230336,\" free\ ": 71914618880,\" threshold\ ": 10485760},\" db\ ": {\" status\ ":\" UP\ " \ "database\":\ "MySQL\",\ "hello\": 1}} " } / * * conditional annotations use * @ return * / @ RequestMapping (value = "user/info") public String info () {return demoService.info ();} / * Asynchronous invocation method * / @ RequestMapping (value = "print") public void print () {for (int I = 0; I < 100) Map demo +) {demoService.print (I);} / * * Server push technology * / @ RequestMapping (value = "serverPush", produces = {MediaType.TEXT_EVENT_STREAM_VALUE}) public String serverPush () {Map demo = new HashMap (1); demo.put ("name", "Zhang San" + new Random (). NextInt ()); ObjectMapper objectMapper = new ObjectMapper () String s = "data:"; try {s + = objectMapper.writeValueAsString (demo) + "\ n\ n"; System.out.println (s);} catch (JsonProcessingException e) {e.printStackTrace ();} return s;}}
Page code
Title if (! window.EventSource) {var event = new EventSource ("/ serverPush"); event.addEventListener ('message', function (t) {var data = t.data; [xss_clean] (data+ "")}) Event.addEventListener ("open", function (t) {console.log ("on");}, false); event.addEventListener ("error", function (t) {if (t.readyState = = EventSource.CLOSED) {console.log ("off");}}, false) } else {console.error ("browser does not support");}
Pay attention to the problems that are easy to encounter during use:
1. Because the return type uses text/event-stream, the response data on the server side must use String or other text types
two。 When returning data, you must start and end with data: and\ n\ nrespectively; for example: String.format ("data:%s\ n\ n", data), which makes me miserable. Online and book materials only give an example without a specific explanation. After looking for a long time to find the reason and the front-end link is successful, the message event cannot be triggered.
This is the answer to the question about how the springboot server pushes SSE. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.