feat: init
This commit is contained in:
8
src/test/java/AnsMsgHandler.java
Normal file
8
src/test/java/AnsMsgHandler.java
Normal file
@@ -0,0 +1,8 @@
|
||||
|
||||
import java.io.InputStream;
|
||||
|
||||
public interface AnsMsgHandler {
|
||||
|
||||
void actMsg(InputStream is, String line);
|
||||
|
||||
}
|
||||
16
src/test/java/SSeTest.java
Normal file
16
src/test/java/SSeTest.java
Normal file
@@ -0,0 +1,16 @@
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.metis.sseclient.check.SseCheck;
|
||||
import dev.langchain4j.agent.tool.ToolSpecification;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
||||
@Slf4j
|
||||
public class SSeTest {
|
||||
public static void main(String[] args) throws JsonProcessingException {
|
||||
SseCheck sseCheck = new SseCheck("http://localhost:8081/sse");
|
||||
List<ToolSpecification> listTools = sseCheck.listTools();
|
||||
System.out.println(listTools);
|
||||
}
|
||||
}
|
||||
77
src/test/java/SseClient.java
Normal file
77
src/test/java/SseClient.java
Normal file
@@ -0,0 +1,77 @@
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author hyd
|
||||
*
|
||||
*/
|
||||
public class SseClient {
|
||||
|
||||
/**
|
||||
* 获取SSE输入流。
|
||||
*
|
||||
* @param urlPath
|
||||
* @return
|
||||
* @throws IOException
|
||||
*/
|
||||
public static InputStream getSseInputStream(String urlPath) throws IOException {
|
||||
URL url = new URL(urlPath);
|
||||
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
|
||||
// 这儿根据自己的情况选择get或post
|
||||
urlConnection.setRequestMethod("GET");
|
||||
urlConnection.setDoOutput(true);
|
||||
urlConnection.setDoInput(true);
|
||||
urlConnection.setUseCaches(false);
|
||||
urlConnection.setRequestProperty("Connection", "Keep-Alive");
|
||||
urlConnection.setRequestProperty("Charset", "UTF-8");
|
||||
//读取过期时间(很重要,建议加上)
|
||||
urlConnection.setReadTimeout(60 * 1000);
|
||||
// text/plain模式
|
||||
urlConnection.setRequestProperty("Content-Type", "text/plain; charset=UTF-8");
|
||||
InputStream inputStream = urlConnection.getInputStream();
|
||||
InputStream is = new BufferedInputStream(inputStream);
|
||||
return is;
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取数据。
|
||||
*
|
||||
* @param is
|
||||
* @param ansMsgHandler
|
||||
* @throws IOException
|
||||
*/
|
||||
public static void readStream(InputStream is, AnsMsgHandler ansMsgHandler) throws IOException {
|
||||
try {
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
|
||||
String line = "";
|
||||
while ((line = reader.readLine()) != null) {
|
||||
// 处理数据接口
|
||||
ansMsgHandler.actMsg(is, line);
|
||||
}
|
||||
// 当服务器端主动关闭的时候,客户端无法获取到信号。现在还不清楚原因。所以无法执行的此处。
|
||||
reader.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
throw new IOException("关闭数据流!");
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
String urlPath = "http://localhost:8081/sse";
|
||||
InputStream inputStream = getSseInputStream(urlPath);
|
||||
readStream(inputStream, new AnsMsgHandler() {
|
||||
|
||||
public void actMsg(InputStream is, String line) {
|
||||
System.out.println(line);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user