Files

98 lines
3.0 KiB
HTML
Raw Permalink Normal View History

<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>SSE流式输出演示</title>
<style>
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
max-width: 800px;
margin: 50px auto;
padding: 20px;
background: #f5f5f5;
}
h1 {
color: #333;
text-align: center;
margin-bottom: 30px;
}
#messages {
font-family: 'Courier New', Courier, monospace;
margin: 20px;
padding: 20px;
border: 2px solid #ddd;
border-radius: 8px;
min-height: 300px;
background: white;
line-height: 1.6;
font-size: 16px;
}
.message {
margin-bottom: 10px;
}
.done-indicator {
margin-top: 20px;
padding: 10px;
background: #d4edda;
color: #155724;
border-radius: 5px;
text-align: center;
font-weight: bold;
}
</style>
</head>
<body>
<h1>🚀 大模型流式输出演示</h1>
<div id="messages">正在连接服务器...</div>
<script>
const messageBox = document.getElementById('messages');
let currentContent = '';
// 使用相对路径,避免跨域问题
const source = new EventSource('/sse');
source.onopen = function() {
console.log('SSE连接已建立');
messageBox.innerHTML = '正在生成内容...\n\n';
};
source.onmessage = function(event) {
try {
const data = JSON.parse(event.data);
if (data.error) {
messageBox.innerHTML += '<div style="color: red;">错误: ' + data.error + '</div>';
source.close();
return;
}
if (data.content !== undefined && data.content !== null) {
// 追加内容
currentContent += data.content;
messageBox.innerHTML = currentContent;
// 自动滚动到底部
messageBox.scrollTop = messageBox.scrollHeight;
}
if (data.done) {
console.log('流式输出完成');
messageBox.innerHTML += '\n\n<div class="done-indicator">✅ 内容生成完成!</div>';
source.close();
}
} catch (e) {
console.error('解析数据失败:', e);
console.log('原始数据:', event.data);
}
};
source.onerror = function(error) {
console.error('SSE连接出错:', error);
messageBox.innerHTML += '<div style="color: red; margin-top: 20px;">❌ 连接失败</div>';
source.close();
};
</script>
</body>
</html>