SseController.java 4.7 KB

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