|
@@ -6,11 +6,13 @@ import com.goafanti.baiduAI.BaiduChatErrorEnums;
|
|
|
import com.goafanti.baiduAI.bo.*;
|
|
import com.goafanti.baiduAI.bo.*;
|
|
|
import com.goafanti.common.error.BusinessException;
|
|
import com.goafanti.common.error.BusinessException;
|
|
|
import okhttp3.*;
|
|
import okhttp3.*;
|
|
|
|
|
+import org.apache.http.client.ClientProtocolException;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.scheduling.annotation.Async;
|
|
import org.springframework.scheduling.annotation.Async;
|
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.stereotype.Component;
|
|
|
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
|
|
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
|
|
|
|
|
+import retrofit2.http.GET;
|
|
|
|
|
|
|
|
import java.io.BufferedReader;
|
|
import java.io.BufferedReader;
|
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
@@ -86,6 +88,8 @@ public class BaiduChatUtils {
|
|
|
|
|
|
|
|
@Async
|
|
@Async
|
|
|
public void sendBaiduAiStream(InputSendChat inputSendChat) throws IOException {
|
|
public void sendBaiduAiStream(InputSendChat inputSendChat) throws IOException {
|
|
|
|
|
+ SseResult res = null;
|
|
|
|
|
+ BufferedReader reader=null;
|
|
|
try {
|
|
try {
|
|
|
String baidu_url=BAIDU_CHAT_WXYY_URL+getRedisBaiduAccessToken();
|
|
String baidu_url=BAIDU_CHAT_WXYY_URL+getRedisBaiduAccessToken();
|
|
|
URL url = new URL(baidu_url);
|
|
URL url = new URL(baidu_url);
|
|
@@ -101,60 +105,57 @@ public class BaiduChatUtils {
|
|
|
connection.setRequestProperty("Content-Length", String.valueOf(postData.length));
|
|
connection.setRequestProperty("Content-Length", String.valueOf(postData.length));
|
|
|
connection.getOutputStream().write(postData);
|
|
connection.getOutputStream().write(postData);
|
|
|
InputStream responseStream = connection.getInputStream();
|
|
InputStream responseStream = connection.getInputStream();
|
|
|
- BufferedReader reader = new BufferedReader(new InputStreamReader(responseStream,"UTF-8"));
|
|
|
|
|
|
|
+ reader = new BufferedReader(new InputStreamReader(responseStream,"UTF-8"));
|
|
|
String line;
|
|
String line;
|
|
|
- SseResult res = SseMap.sseEmitterMap.get(inputSendChat.getUserId());
|
|
|
|
|
|
|
+ res = SseMap.sseEmitterMap.get(inputSendChat.getUserId());
|
|
|
while ((line = reader.readLine())!= null) {
|
|
while ((line = reader.readLine())!= null) {
|
|
|
// 每行数据中以 "data:" 开头的部分即为实际的响应数据
|
|
// 每行数据中以 "data:" 开头的部分即为实际的响应数据
|
|
|
- System.out.println("relut="+line+"。");
|
|
|
|
|
- if (line.startsWith("data:")) {
|
|
|
|
|
- String data = line.substring("data:".length()).trim();
|
|
|
|
|
- JSONObject jsonObject = JSONObject.parseObject(data);
|
|
|
|
|
- Boolean isEnd = jsonObject.getBoolean("is_end");
|
|
|
|
|
- if(isEnd){
|
|
|
|
|
- break;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ if (StringUtils.isNotBlank(line)){
|
|
|
|
|
+ System.out.println(line);
|
|
|
|
|
+ if (line.startsWith("data:")) {
|
|
|
|
|
+ String data = line.substring("data:".length()).trim();
|
|
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(data);
|
|
|
|
|
+ Boolean isEnd = jsonObject.getBoolean("is_end");
|
|
|
// OutSendChatOK out =jsonObject.toJavaObject(OutSendChatOK.class);
|
|
// OutSendChatOK out =jsonObject.toJavaObject(OutSendChatOK.class);
|
|
|
// System.out.println(data);
|
|
// 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.send(data);
|
|
|
|
|
+ if(isEnd){
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+ }else if(line.startsWith("{")) {
|
|
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(line);
|
|
|
|
|
+ Integer errorCode = jsonObject.getInteger("error_code");
|
|
|
|
|
+ if (errorCode!=null){
|
|
|
|
|
+ OutChatER out=new OutChatER();
|
|
|
|
|
+ out.setError_code(errorCode);
|
|
|
|
|
+ if (errorCode.equals("336003")){
|
|
|
|
|
+ out.setError_msg(BaiduChatErrorEnums.BycodeGetMsg(errorCode)+jsonObject.getString("error_msg"));
|
|
|
|
|
+ }else {
|
|
|
|
|
+ out.setError_msg(BaiduChatErrorEnums.BycodeGetMsg(errorCode));
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ res.sseEmitter.send(JSON.toJSONString(errorCode));
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
res.sseEmitter.complete();
|
|
res.sseEmitter.complete();
|
|
|
- reader.close();
|
|
|
|
|
} catch (MalformedURLException e) {
|
|
} catch (MalformedURLException e) {
|
|
|
e.printStackTrace();
|
|
e.printStackTrace();
|
|
|
sendJitaoBaiWen(inputSendChat.getUserId(),"data:{\"error_code\":\"2\",\"error_msg\":\"域名解析异常\"}");
|
|
sendJitaoBaiWen(inputSendChat.getUserId(),"data:{\"error_code\":\"2\",\"error_msg\":\"域名解析异常\"}");
|
|
|
} catch (ProtocolException e) {
|
|
} catch (ProtocolException e) {
|
|
|
e.printStackTrace();
|
|
e.printStackTrace();
|
|
|
- }
|
|
|
|
|
- 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;
|
|
|
|
|
|
|
+ }catch (IllegalStateException e){
|
|
|
|
|
+ LoggerUtils.debug(getClass(),"前端网页已关闭");
|
|
|
|
|
+ }finally {
|
|
|
|
|
+ reader.close();
|
|
|
|
|
+ res.sseEmitter.complete();
|
|
|
|
|
+ SseMap.sseEmitterMap.remove(inputSendChat.getUserId());
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+
|
|
|
private void sendJitaoBaiWen(String userId, String s) throws IOException {
|
|
private void sendJitaoBaiWen(String userId, String s) throws IOException {
|
|
|
SseResult res = SseMap.sseEmitterMap.get(userId);
|
|
SseResult res = SseMap.sseEmitterMap.get(userId);
|
|
|
res.sseEmitter.send(s);
|
|
res.sseEmitter.send(s);
|