-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_sse_streaming.html
More file actions
244 lines (221 loc) · 8.38 KB
/
test_sse_streaming.html
File metadata and controls
244 lines (221 loc) · 8.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
<!DOCTYPE html>
<html lang="ko">
<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: 0 auto;
padding: 20px;
background-color: #f5f5f5;
}
.container {
background: white;
padding: 30px;
border-radius: 10px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}
h1 {
color: #333;
text-align: center;
margin-bottom: 30px;
}
.form-group {
margin-bottom: 20px;
}
label {
display: block;
margin-bottom: 5px;
font-weight: bold;
color: #555;
}
input, select, textarea {
width: 100%;
padding: 12px;
border: 2px solid #ddd;
border-radius: 5px;
font-size: 16px;
box-sizing: border-box;
}
input:focus, select:focus, textarea:focus {
outline: none;
border-color: #007bff;
}
button {
background: #007bff;
color: white;
padding: 12px 30px;
border: none;
border-radius: 5px;
font-size: 16px;
cursor: pointer;
width: 100%;
margin-bottom: 20px;
}
button:hover {
background: #0056b3;
}
button:disabled {
background: #ccc;
cursor: not-allowed;
}
.stream-container {
background: #f8f9fa;
border: 2px solid #e9ecef;
border-radius: 5px;
padding: 20px;
margin-top: 20px;
min-height: 200px;
max-height: 400px;
overflow-y: auto;
}
.status-message {
color: #007bff;
font-weight: bold;
margin-bottom: 10px;
}
.content-chunk {
color: #333;
margin-bottom: 5px;
line-height: 1.6;
}
.error-message {
color: #dc3545;
font-weight: bold;
}
.complete-message {
color: #28a745;
font-weight: bold;
margin-top: 20px;
padding: 15px;
background: #d4edda;
border-radius: 5px;
}
.step-indicator {
display: inline-block;
background: #007bff;
color: white;
border-radius: 50%;
width: 20px;
height: 20px;
text-align: center;
line-height: 20px;
font-size: 12px;
margin-right: 10px;
}
</style>
</head>
<body>
<div class="container">
<h1>🎨 패션 추천 SSE 스트리밍 테스트</h1>
<form id="fashionForm">
<div class="form-group">
<label for="userInput">패션 질문:</label>
<textarea id="userInput" rows="3" placeholder="예: 여름에 데이트할 때 입을 옷 추천해줘" required></textarea>
</div>
<div class="form-group">
<label for="expertType">전문가 유형:</label>
<select id="expertType" required>
<option value="STYLE_ANALYST">스타일 분석가</option>
<option value="COLOR_EXPERT">컬러 전문가</option>
<option value="TREND_EXPERT">트렌드 전문가</option>
<option value="FITTING_COORDINATOR">코디네이터</option>
</select>
</div>
<div class="form-group">
<label for="roomId">방 ID:</label>
<input type="number" id="roomId" value="1" min="1" required>
</div>
<button type="submit" id="submitBtn">🚀 패션 추천 시작</button>
</form>
<div class="stream-container" id="streamContainer" style="display: none;">
<div id="streamContent"></div>
</div>
</div>
<script>
let eventSource = null;
document.getElementById('fashionForm').addEventListener('submit', async function(e) {
e.preventDefault();
const submitBtn = document.getElementById('submitBtn');
const streamContainer = document.getElementById('streamContainer');
const streamContent = document.getElementById('streamContent');
// UI 초기화
submitBtn.disabled = true;
submitBtn.textContent = '🔄 처리 중...';
streamContainer.style.display = 'block';
streamContent.innerHTML = '<div class="status-message">연결 중...</div>';
// 기존 연결 종료
if (eventSource) {
eventSource.close();
}
try {
// API 호출
const response = await fetch('/api/expert/single/stream', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
user_input: document.getElementById('userInput').value,
expert_type: document.getElementById('expertType').value,
room_id: parseInt(document.getElementById('roomId').value)
})
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
// SSE 스트림 처리
const reader = response.body.getReader();
const decoder = new TextDecoder();
while (true) {
const { done, value } = await reader.read();
if (done) break;
const chunk = decoder.decode(value);
const lines = chunk.split('\n');
for (const line of lines) {
if (line.startsWith('data: ')) {
try {
const data = JSON.parse(line.slice(6));
handleStreamData(data);
} catch (parseError) {
console.error('JSON 파싱 오류:', parseError);
}
}
}
}
} catch (error) {
console.error('스트리밍 오류:', error);
streamContent.innerHTML += `<div class="error-message">❌ 오류 발생: ${error.message}</div>`;
} finally {
submitBtn.disabled = false;
submitBtn.textContent = '🚀 패션 추천 시작';
}
});
function handleStreamData(data) {
const streamContent = document.getElementById('streamContent');
switch (data.type) {
case 'status':
const stepIndicator = `<span class="step-indicator">${data.step}</span>`;
streamContent.innerHTML += `<div class="status-message">${stepIndicator}${data.message}</div>`;
break;
case 'content':
streamContent.innerHTML += `<div class="content-chunk">${data.chunk}</div>`;
break;
case 'complete':
streamContent.innerHTML += `<div class="complete-message">✅ 분석 완료!</div>`;
// 완료된 데이터를 콘솔에 출력
console.log('완료된 데이터:', data.data);
break;
case 'error':
streamContent.innerHTML += `<div class="error-message">❌ ${data.message}</div>`;
break;
}
// 자동 스크롤
streamContent.scrollTop = streamContent.scrollHeight;
}
</script>
</body>
</html>