Commit ea7f1271 by 王祖波

stream test

parent ed8ea2c1
...@@ -25,10 +25,7 @@ import org.slf4j.LoggerFactory; ...@@ -25,10 +25,7 @@ import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
import java.util.List; import java.util.List;
...@@ -155,7 +152,7 @@ public class ContentAIController { ...@@ -155,7 +152,7 @@ public class ContentAIController {
return RestResponse.successResult(cmeTaskProcess); return RestResponse.successResult(cmeTaskProcess);
} }
@RequestMapping(value = "/test", produces = "text/event-stream;charset=UTF-8") @RequestMapping(value = "/test", method = RequestMethod.GET)
public SseEmitter streamStockPrice() { public SseEmitter streamStockPrice() {
SseEmitter emitter = new SseEmitter(); SseEmitter emitter = new SseEmitter();
// 模拟生成实时股票价格并推送给客户端 // 模拟生成实时股票价格并推送给客户端
...@@ -168,7 +165,7 @@ public class ContentAIController { ...@@ -168,7 +165,7 @@ public class ContentAIController {
// 构造股票价格的消息 // 构造股票价格的消息
String message = String.format("%.2f", price); String message = String.format("%.2f", price);
// 发送消息给客户端 // 发送消息给客户端
emitter.send(SseEmitter.event().data(message)); emitter.send(SseEmitter.event().data(message,MediaType.TEXT_PLAIN));
// 休眠1秒钟 // 休眠1秒钟
Thread.sleep(1000); Thread.sleep(1000);
} }
......
...@@ -37,6 +37,7 @@ ...@@ -37,6 +37,7 @@
<list> <list>
<value>text/html;charset=UTF-8</value> <value>text/html;charset=UTF-8</value>
<value>application/x-www-form-urlencoded;charset=UTF-8</value> <value>application/x-www-form-urlencoded;charset=UTF-8</value>
<value>text/plain;charset=UTF-8</value>
<!--默认是 iso-8859-1 --> <!--默认是 iso-8859-1 -->
</list> </list>
</property> </property>
......
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name>Archetype Created Web Application</display-name> <display-name>Archetype Created Web Application</display-name>
<context-param> <context-param>
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
<param-value>/WEB-INF/spring-*.xml</param-value> <param-value>/WEB-INF/spring-*.xml</param-value>
</init-param> </init-param>
<load-on-startup>2</load-on-startup> <load-on-startup>2</load-on-startup>
<async-supported>true</async-supported>
</servlet> </servlet>
<servlet-mapping> <servlet-mapping>
<servlet-name>spring</servlet-name> <servlet-name>spring</servlet-name>
...@@ -39,6 +40,7 @@ ...@@ -39,6 +40,7 @@
<filter-mapping> <filter-mapping>
<filter-name>encodingFilter</filter-name> <filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern> <url-pattern>/*</url-pattern>
<dispatcher>ASYNC</dispatcher>
</filter-mapping> </filter-mapping>
<filter> <filter>
...@@ -48,5 +50,6 @@ ...@@ -48,5 +50,6 @@
<filter-mapping> <filter-mapping>
<filter-name>springSessionRepositoryFilter</filter-name> <filter-name>springSessionRepositoryFilter</filter-name>
<url-pattern>/*</url-pattern> <url-pattern>/*</url-pattern>
<dispatcher>ASYNC</dispatcher>
</filter-mapping> </filter-mapping>
</web-app> </web-app>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment