SseController.java 4.3 KB

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