Commit ea7f1271 by 王祖波

stream test

parent ed8ea2c1
......@@ -25,10 +25,7 @@ import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
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.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
import java.util.List;
......@@ -155,7 +152,7 @@ public class ContentAIController {
return RestResponse.successResult(cmeTaskProcess);
}
@RequestMapping(value = "/test", produces = "text/event-stream;charset=UTF-8")
@RequestMapping(value = "/test", method = RequestMethod.GET)
public SseEmitter streamStockPrice() {
SseEmitter emitter = new SseEmitter();
// 模拟生成实时股票价格并推送给客户端
......@@ -168,7 +165,7 @@ public class ContentAIController {
// 构造股票价格的消息
String message = String.format("%.2f", price);
// 发送消息给客户端
emitter.send(SseEmitter.event().data(message));
emitter.send(SseEmitter.event().data(message,MediaType.TEXT_PLAIN));
// 休眠1秒钟
Thread.sleep(1000);
}
......
......@@ -37,6 +37,7 @@
<list>
<value>text/html;charset=UTF-8</value>
<value>application/x-www-form-urlencoded;charset=UTF-8</value>
<value>text/plain;charset=UTF-8</value>
<!--默认是 iso-8859-1 -->
</list>
</property>
......
<?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"
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>
<context-param>
......@@ -23,6 +23,7 @@
<param-value>/WEB-INF/spring-*.xml</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
......@@ -39,6 +40,7 @@
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>ASYNC</dispatcher>
</filter-mapping>
<filter>
......@@ -48,5 +50,6 @@
<filter-mapping>
<filter-name>springSessionRepositoryFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>ASYNC</dispatcher>
</filter-mapping>
</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