SseController.java 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package com.fdage.controller;
  2. import io.swagger.annotations.ApiOperation;
  3. import org.springframework.stereotype.Controller;
  4. import org.springframework.web.bind.annotation.GetMapping;
  5. import org.springframework.web.bind.annotation.PostMapping;
  6. import org.springframework.web.bind.annotation.ResponseBody;
  7. import springfox.documentation.annotations.ApiIgnore;
  8. import javax.servlet.AsyncContext;
  9. import javax.servlet.AsyncEvent;
  10. import javax.servlet.AsyncListener;
  11. import javax.servlet.ServletException;
  12. import javax.servlet.annotation.WebServlet;
  13. import javax.servlet.http.HttpServlet;
  14. import javax.servlet.http.HttpServletRequest;
  15. import javax.servlet.http.HttpServletResponse;
  16. import java.io.IOException;
  17. import java.io.PrintWriter;
  18. import java.util.ArrayList;
  19. import java.util.Date;
  20. import java.util.List;
  21. /**
  22. * Created by Hb_zzZ on 2019/9/16.
  23. */
  24. @ApiIgnore
  25. @WebServlet(urlPatterns = { "/sendMessage" }, asyncSupported = true)
  26. @Controller
  27. public class SseController extends HttpServlet {
  28. private static final long serialVersionUID = 1L;
  29. private final static int DEFAULT_TIME_OUT = 10 * 60 * 1000;
  30. public static List<AsyncContext> actxList =new ArrayList<AsyncContext>();
  31. @Override
  32. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  33. // TODO Auto-generated method stub
  34. resp.setContentType("text/event-stream");
  35. resp.setCharacterEncoding("UTF-8");
  36. req.setAttribute("org.apache.catalina.ASYNC_SUPPORTED", true);//注意这里
  37. AsyncContext actx = req.startAsync(req, resp);
  38. actx.setTimeout(DEFAULT_TIME_OUT);
  39. actx.addListener(new AsyncListener() {
  40. @Override
  41. public void onComplete(AsyncEvent arg0) throws IOException {
  42. // TODO Auto-generated method stub
  43. System.out.println("[echo]event complete:" + arg0.getSuppliedRequest().getRemoteAddr());
  44. }
  45. @Override
  46. public void onError(AsyncEvent arg0) throws IOException {
  47. // TODO Auto-generated method stub
  48. System.out.println("[echo]event has error");
  49. }
  50. @Override
  51. public void onStartAsync(AsyncEvent arg0) throws IOException {
  52. // TODO Auto-generated method stub
  53. System.out.println("[echo]event start:" + arg0.getSuppliedRequest().getRemoteAddr());
  54. }
  55. @Override
  56. public void onTimeout(AsyncEvent arg0) throws IOException {
  57. // TODO Auto-generated method stub
  58. System.out.println("[echo]event time lost");
  59. }
  60. });
  61. actxList.add(actx);
  62. // PrintWriter out = actx.getResponse().getWriter();
  63. // out.println("data:" + new Date().getTime()); //js页面EventSource接收数据格式:data:数据 + "\r\n"
  64. // out.flush();
  65. // new Thread(new AsyncWebService(actx)).start();
  66. }
  67. @GetMapping("send")
  68. @ResponseBody
  69. public static String send(String msg){
  70. //打印当前AsyncContext
  71. System.out.println(actxList.size());
  72. if(actxList.size()>0){
  73. for(int i = 0;i<actxList.size();i++){
  74. AsyncContext obj = actxList.get(i);
  75. PrintWriter out;
  76. try {
  77. if (obj.getTimeout()>0){
  78. out = obj.getResponse().getWriter();
  79. out.println(msg); //js页面EventSource接收数据格式:data:数据 + "\r\n"
  80. out.flush();
  81. }
  82. } catch (Exception e) {
  83. // TODO Auto-generated catch block
  84. e.printStackTrace();
  85. }
  86. }
  87. }
  88. return "发送消息";
  89. }
  90. // class AsyncWebService implements Runnable {
  91. // AsyncContext ctx;
  92. //
  93. // public AsyncWebService(AsyncContext ctx) {
  94. // this.ctx = ctx;
  95. // }
  96. //
  97. // public void run() {
  98. // try {
  99. // //等待十秒钟,以模拟业务方法的执行
  100. // Thread.sleep(10000);
  101. // PrintWriter out = ctx.getResponse().getWriter();
  102. // out.println("data:中文" + new Date() + "\r\n"); //js页面EventSource接收数据格式:data:数据 + "\r\n"
  103. //
  104. // out.flush();
  105. // ctx.complete();
  106. // } catch (Exception e) {
  107. // e.printStackTrace();
  108. // }
  109. //
  110. // }
  111. //
  112. // }
  113. }