Procházet zdrojové kódy

新增技淘百问处理

anderx před 2 roky
rodič
revize
84c77b330b

+ 0 - 19
src/main/java/com/goafanti/baiduAI/service/impl/BaiduAiServiceImpl.java

@@ -48,26 +48,7 @@ public class BaiduAiServiceImpl implements BaiduAiService {
     }
 
 
-    private OutSendChat pushResultToOutSendChat(String result) {
 
-        Map<String ,Object> resultMap=JSON.parseObject(result, Map.class);
-        Integer errorCode= (Integer) resultMap.get("error_code");
-        if (errorCode!=null){
-            OutChatER res=new OutChatER();
-            res.setErrorCode(errorCode);
-            if (errorCode.equals("336003")){
-                res.setErrorMsg(BaiduChatErrorEnums.BycodeGetMsg(errorCode)+resultMap.get("error_msg"));
-            }else {
-            res.setErrorMsg(BaiduChatErrorEnums.BycodeGetMsg(errorCode));
-            }
-            return res;
-        }else {
-            OutSendChatOK res=new OutSendChatOK();
-            res=JSON.parseObject(result,OutSendChatOK.class);
-            return res;
-        }
-
-    }
 
 
 }

+ 29 - 4
src/main/java/com/goafanti/common/utils/BaiduChatUtils.java

@@ -2,10 +2,8 @@ package com.goafanti.common.utils;
 
 import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONObject;
-import com.goafanti.baiduAI.bo.InputSendChat;
-import com.goafanti.baiduAI.bo.OutSendChatOK;
-import com.goafanti.baiduAI.bo.SseMap;
-import com.goafanti.baiduAI.bo.SseResult;
+import com.goafanti.baiduAI.BaiduChatErrorEnums;
+import com.goafanti.baiduAI.bo.*;
 import com.goafanti.common.error.BusinessException;
 import okhttp3.*;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -25,6 +23,7 @@ import java.net.URL;
 import java.nio.charset.StandardCharsets;
 import java.util.Calendar;
 import java.util.HashMap;
+import java.util.Map;
 import java.util.concurrent.TimeUnit;
 
 
@@ -107,6 +106,7 @@ public class BaiduChatUtils {
             SseResult res =  SseMap.sseEmitterMap.get(inputSendChat.getUserId());
             while ((line = reader.readLine())!= null) {
                 // 每行数据中以 "data:" 开头的部分即为实际的响应数据
+                System.out.println("relut="+line+"。");
                 if (line.startsWith("data:")) {
                     String data = line.substring("data:".length()).trim();
                     JSONObject jsonObject = JSONObject.parseObject(data);
@@ -117,6 +117,10 @@ public class BaiduChatUtils {
 //                  OutSendChatOK out =jsonObject.toJavaObject(OutSendChatOK.class);
 //                    System.out.println(data);
                     res.sseEmitter.send(data);
+                }else if(line.startsWith("{")) {
+                    JSONObject jsonObject = JSONObject.parseObject(line);
+                    jsonObject.getString("error_code");
+                    res.sseEmitter.send(line);
                 }
             }
             res.sseEmitter.complete();
@@ -130,6 +134,27 @@ public class BaiduChatUtils {
         SseMap.sseEmitterMap.remove(inputSendChat.getUserId());
     }
 
+    private OutSendChat pushResultToOutSendChat(String result) {
+
+        Map<String ,Object> resultMap=JSON.parseObject(result, Map.class);
+        Integer errorCode= (Integer) resultMap.get("error_code");
+        if (errorCode!=null){
+            OutChatER res=new OutChatER();
+            res.setErrorCode(errorCode);
+            if (errorCode.equals("336003")){
+                res.setErrorMsg(BaiduChatErrorEnums.BycodeGetMsg(errorCode)+resultMap.get("error_msg"));
+            }else {
+                res.setErrorMsg(BaiduChatErrorEnums.BycodeGetMsg(errorCode));
+            }
+            return res;
+        }else {
+            OutSendChatOK res=new OutSendChatOK();
+            res=JSON.parseObject(result,OutSendChatOK.class);
+            return res;
+        }
+
+    }
+
     private void sendJitaoBaiWen(String userId, String s) throws IOException {
         SseResult res =  SseMap.sseEmitterMap.get(userId);
         res.sseEmitter.send(s);

+ 36 - 60
src/main/webapp/WEB-INF/views/portal/test.html

@@ -1,78 +1,54 @@
 <!DOCTYPE html>
-<html lang="en">
 <head>
     <meta charset="UTF-8">
-    <title>SseEmitter</title>
+    <title>测试</title>
 </head>
 <body>
-<button onclick="closeSse()">关闭连接</button>
-<div id="message"></div>
+<div>服务器端推送测试</div>
+<div id="serverSendEventDiv"></div>
 </body>
-<script>
-    let source = null;
-
-    // 用时间戳模拟登录用户
-    const userId = new Date().getTime();
-
-    console.log(userId);
-
-
-    if (!!window.EventSource) {
-
-        // 建立连接
-        source = new EventSource('http://localhost:8080/chat/send');
-
-        /**
-         * 连接一旦建立,就会触发open事件
-         * 另一种写法:source.onopen = function (event) {}
-         */
+<script type="text/javascript">
+    if(window.EventSource) {
+        const source = new EventSource("//172.16.0.255:8080/chat/start?clientId=123");
+        let s = '';
+        // 监听打开连接
         source.addEventListener('open', function (e) {
-            setMessageInnerHTML("建立连接。。。");
+            console.log("连接打开")
         }, false);
-
-        /**
-         * 客户端收到服务器发来的数据
-         * 另一种写法:source.onmessage = function (event) {}
-         */
         source.addEventListener('message', function (e) {
-            setMessageInnerHTML(e.data);
-        });
-
-
-        /**
-         * 如果发生通信错误(比如连接中断),就会触发error事件
-         * 或者:
-         * 另一种写法:source.onerror = function (event) {}
-         */
-        source.addEventListener('error', function (e) {
+            s += e.data + '<br/>';
+            document.getElementById("serverSendEventDiv").innerHTML = s;
+        })
+        // 监听关闭连接
+        source.addEventListener('close', function (e) {
             if (e.readyState === EventSource.CLOSED) {
-                setMessageInnerHTML("连接关闭");
+                console.log("连接关闭")
             } else {
-                console.log(e);
+                console.log(e.readyState)
             }
         }, false);
-
+        source.addEventListener("error", function(err) {
+            console.log(JSON.stringify(err))
+            console.log(err)
+            // 类似的返回信息验证,这里是实例
+            err && err.status === 401 && console.log('not authorized')
+        })
     } else {
-        setMessageInnerHTML("你的浏览器不支持SSE");
+        alert("你的浏览器不支持sse")
     }
+</script>
 
-    // // 监听窗口关闭事件,主动去关闭sse连接,如果服务端设置永不过期,浏览器关闭后手动清理服务端数据
-    // window.onbeforeunload = function () {
-    //     closeSse();
-    // };
 
-    // 关闭Sse连接
-    // function closeSse() {
-    //     source.close();
-    //     const httpRequest = new XMLHttpRequest();
-    //     httpRequest.open('GET', 'http://localhost:8080/chat/end?clientId' + userId, true);
-    //     httpRequest.send();
-    //     console.log("close");
-    // }
+<!--<script>-->
+<!--    const evtSource = new EventSource("//172.16.0.255:8080//chat/start",{ withCredentials: true });-->
+
+<!--    evtSource.onmessage=function (event) {-->
+<!--        const newElement=document.createElement("li");-->
+<!--        const eventList=document.getElementById("list");-->
+
+<!--        newElement.innerHTML="message:"+event.data;-->
+<!--        eventList.append(newElement);-->
+<!--    }-->
+<!--</script>-->
+
 
-    // 将消息显示在网页上
-    function setMessageInnerHTML(innerHTML) {
-        document.getElementById('message').innerHTML += innerHTML + '<br/>';
-    }
-</script>
-</html>