2024-11-16 14:00:57 +08:00

41 lines
1.3 KiB
HTML
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE html>
<html>
<head>
<title>SSE test</title>
<script type="text/javascript">
// 向后端服务器发起sse请求
const es = new EventSource("http://127.0.0.1:20201/admin/rag/default_talk");
// 监听事件流
es.onmessage = function (e) {
document.getElementById("test")
.insertAdjacentHTML("beforeend", "<li>" + e.data + "</li>");
console.log(e);
}
// 监听”chat“事件流
es.addEventListener("chat", (e) => {
document.getElementById("test")
.insertAdjacentHTML("beforeend", "<a>" + e.data + "</a>");
console.log(e)
});
es.onerror = function (e) {
// readyState说明
// 0浏览器与服务端尚未建立连接或连接已被关闭
// 1浏览器与服务端已成功连接浏览器正在处理接收到的事件及数据
// 2浏览器与服务端建立连接失败客户端不再继续建立与服务端之间的连接
console.log("readyState = " + e.currentTarget.readyState);
// 关闭连接
es.close();
}
</script>
</head>
<body>
<h1>SSE test</h1>
<div>
<ul id="test">
</ul>
</div>
</body>
</html>