|
|
@@ -1,5 +1,6 @@
|
|
|
package com.goafanti.baiduAI.controller;
|
|
|
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
import com.goafanti.baiduAI.bo.InputSendChat;
|
|
|
import com.goafanti.baiduAI.bo.Messages;
|
|
|
import com.goafanti.baiduAI.bo.SseMap;
|
|
|
@@ -20,10 +21,7 @@ import org.springframework.web.bind.annotation.*;
|
|
|
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.Calendar;
|
|
|
-import java.util.List;
|
|
|
-import java.util.UUID;
|
|
|
+import java.util.*;
|
|
|
|
|
|
@RestController
|
|
|
@RequestMapping(value = "/chat")
|
|
|
@@ -48,4 +46,56 @@ public class BaiduAiController extends CertifyApiController{
|
|
|
|
|
|
|
|
|
|
|
|
+ @RequestMapping("/start")
|
|
|
+ public SseEmitter testSseEmitter(String clientId) {
|
|
|
+ // 默认30秒超时,设置为0L则永不超时
|
|
|
+ SseEmitterUTF8 sseEmitter = new SseEmitterUTF8(3600_000L);
|
|
|
+// if (clientId==null)clientId=UUID.randomUUID().toString();
|
|
|
+ SseMap.sseEmitterMap.put(clientId, new SseResult(clientId, System.currentTimeMillis(), sseEmitter));
|
|
|
+ return sseEmitter;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 向SseEmitter对象发送数据
|
|
|
+ *
|
|
|
+ * @param clientId
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @RequestMapping("/send2")
|
|
|
+ public String setSseEmitter(String clientId) {
|
|
|
+ try {
|
|
|
+ SseResult result = SseMap.sseEmitterMap.get(clientId);
|
|
|
+ if (result != null && result.sseEmitter != null) {
|
|
|
+ long timeInMillis = Calendar.getInstance().getTimeInMillis();
|
|
|
+ String re = "{\"success\":true,\"message\":\"操作成功!\",\"code\":200,\"result\":{\"id\":1018},\"timestamp\":"+timeInMillis+"}";
|
|
|
+ Map<String,Object> map= JSON.parseObject(re);
|
|
|
+ result.sseEmitter.send(map);
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ LoggerUtils.error(getClass(),"IOException");
|
|
|
+ return "error";
|
|
|
+ }
|
|
|
+ return "Succeed!";
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将SseEmitter对象设置成完成
|
|
|
+ *
|
|
|
+ * @param clientId
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @RequestMapping("/end")
|
|
|
+ public String completeSseEmitter(String clientId) {
|
|
|
+ SseResult result = SseMap.sseEmitterMap.get(clientId);
|
|
|
+ if (result != null) {
|
|
|
+ SseMap.sseEmitterMap.remove(clientId);
|
|
|
+ result.sseEmitter.complete();
|
|
|
+ }
|
|
|
+ return "Succeed!";
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
}
|