Commit ed8ea2c1 by 王祖波

stream test

parent cc679c41
...@@ -23,13 +23,16 @@ import com.gic.thirdparty.cloudfile.pojo.cme.CmeTaskProcess; ...@@ -23,13 +23,16 @@ import com.gic.thirdparty.cloudfile.pojo.cme.CmeTaskProcess;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; 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.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
import java.util.List; import java.util.List;
import java.util.Random;
/** /**
* 好办-AI创作 * 好办-AI创作
...@@ -152,4 +155,27 @@ public class ContentAIController { ...@@ -152,4 +155,27 @@ public class ContentAIController {
return RestResponse.successResult(cmeTaskProcess); return RestResponse.successResult(cmeTaskProcess);
} }
@RequestMapping(value = "/test", produces = "text/event-stream;charset=UTF-8")
public SseEmitter streamStockPrice() {
SseEmitter emitter = new SseEmitter();
// 模拟生成实时股票价格并推送给客户端
Random random = new Random();
new Thread(() -> {
try {
while (true) {
// 生成随机的股票价格
double price = 100 + random.nextDouble() * 10;
// 构造股票价格的消息
String message = String.format("%.2f", price);
// 发送消息给客户端
emitter.send(SseEmitter.event().data(message));
// 休眠1秒钟
Thread.sleep(1000);
}
} catch (Exception e) {
emitter.completeWithError(e);
}
}).start();
return emitter;
}
} }
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