diff --git a/ruoyi-bussiness/src/main/java/com/ruoyi/cms/config/ChatClient.java b/ruoyi-bussiness/src/main/java/com/ruoyi/cms/config/ChatClient.java index 53a09b8..bcea58d 100644 --- a/ruoyi-bussiness/src/main/java/com/ruoyi/cms/config/ChatClient.java +++ b/ruoyi-bussiness/src/main/java/com/ruoyi/cms/config/ChatClient.java @@ -4,8 +4,8 @@ import com.alibaba.fastjson2.JSONArray; import com.alibaba.fastjson2.JSONObject; import com.ruoyi.cms.domain.chat.ChatRequest; import com.ruoyi.common.utils.StringUtils; -import lombok.var; import okhttp3.*; +import okio.BufferedSource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @@ -97,18 +97,34 @@ public class ChatClient { } // 逐行读取 SSE 格式的响应 - try (var bufferedSource = body.source()) { - //判断 bufferedSource - if (bufferedSource != null) { - return; - } + BufferedSource bufferedSource = body.source(); + if (bufferedSource == null) { + System.out.println("bufferedSource 为空,直接返回"); + return; + } + try { while (!bufferedSource.exhausted()) { String chunk = bufferedSource.readUtf8Line(); if (chunk != null && !chunk.trim().isEmpty()) { callback.onData(chunk); } } + } catch (IOException e) { + System.out.println("读取流异常"+ e); + } finally { + try { + bufferedSource.close(); + } catch (IOException e) { + } } + /*try (var bufferedSource = body.source()) { + while (!bufferedSource.exhausted()) { + String chunk = bufferedSource.readUtf8Line(); + if (chunk != null && !chunk.trim().isEmpty()) { + callback.onData(chunk); + } + } + }*/ // 通知流结束 callback.onComplete();