Vanilla JS 예제
index.html 파일 하나로 SDK를 연동하는 전체 예제입니다. {VERSION}, YOUR_SDK_KEY, YOUR_AVATAR_ID를 실제 값으로 변경 후 브라우저에서 실행합니다.
html
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Klleon SDK - Vanilla JS</title>
<script src="https://klleon.k1.klleon.io/{VERSION}/klleon-sdk.umd.js"></script>
<style>
body { font-family: sans-serif; margin: 0; padding: 20px; background: #f4f4f4; display: flex; flex-direction: column; align-items: center; }
.container { display: flex; width: 900px; max-width: 100%; height: 600px; gap: 20px; margin-top: 20px; background: #fff; padding: 20px; border-radius: 8px; box-shadow: 0 0 10px rgba(0,0,0,0.1); box-sizing: border-box; }
.avatar-section { flex: 2; border: 1px solid #ddd; border-radius: 8px; overflow: hidden; }
.chat-section { flex: 1; display: flex; flex-direction: column; gap: 10px; }
avatar-container { width: 100%; height: 100%; background: #e0e0e0; }
chat-container { height: 400px; border: 1px solid #ddd; border-radius: 8px; overflow-y: auto; padding: 10px; }
.input-group { display: flex; }
.input-group input { flex: 1; padding: 8px; border: 1px solid #ccc; border-radius: 4px 0 0 4px; outline: none; }
.input-group button { padding: 8px 12px; border: 1px solid #ccc; border-left: none; background: #3579CC; color: #fff; cursor: pointer; border-radius: 0 4px 4px 0; }
.input-group button:disabled { background: #b7c7d9; cursor: not-allowed; }
.api-demo { display: flex; flex-direction: column; gap: 8px; }
.api-demo h2 { margin: 0; font-size: 14px; }
.api-demo p { margin: 0; font-size: 12px; color: #666; }
</style>
</head>
<body>
<h1>Klleon SDK - Vanilla JS</h1>
<div class="container">
<div class="avatar-section">
<avatar-container volume="100"></avatar-container>
</div>
<div class="chat-section">
<!-- 대화 영역: chat-container가 자체 입력 UI로 sendMessage()를 전송합니다 -->
<chat-container type="text"></chat-container>
<!-- 아래 입력창은 chat-container와 별개로 SDK 메서드를 직접 호출하는 예시입니다 -->
<div class="api-demo">
<h2>API 직접 호출 데모</h2>
<p>
sendMessage()는 LLM 응답을 생성하고, speak()는 입력한 텍스트를 그대로 읽습니다.
</p>
<div class="input-group">
<input type="text" id="msg-input" placeholder="sendMessage 내용 입력..." />
<button id="btn-send" disabled>전송</button>
</div>
<div class="input-group">
<input type="text" id="speak-input" placeholder="아바타가 읽을 텍스트..." />
<button id="btn-speak" disabled>speak</button>
</div>
</div>
</div>
</div>
<script>
document.addEventListener('DOMContentLoaded', async () => {
const SDK = window.KlleonSDK;
// 1. 이벤트 등록
SDK.onStatus((status) => {
console.log('상태:', status);
if (status === 'CONNECTED_FINISH') {
console.log('아바타 준비 완료');
}
});
SDK.onSignal((data) => {
console.log('시그널:', data.signal, data.payload);
});
SDK.onError((error) => {
console.error('에러:', error.code, error.message);
});
// 2. 초기화
try {
await SDK.init({
sdk_key: 'YOUR_SDK_KEY',
avatar_id: 'YOUR_AVATAR_ID',
});
console.log('SDK 초기화 성공');
// 초기화 성공 후에만 버튼을 활성화합니다
document.getElementById('btn-send').disabled = false;
document.getElementById('btn-speak').disabled = false;
} catch (e) {
console.error('SDK 초기화 실패:', e.message);
return;
}
// 3. 메시지 전송
document.getElementById('btn-send').addEventListener('click', () => {
const input = document.getElementById('msg-input');
if (input.value.trim()) {
SDK.sendMessage(input.value.trim());
input.value = '';
}
});
// 4. speak
document.getElementById('btn-speak').addEventListener('click', () => {
const input = document.getElementById('speak-input');
if (input.value.trim()) {
SDK.speak(input.value.trim());
input.value = '';
}
});
// 5. 페이지 이탈 시 정리
// 이 콜백에서는 비동기 종료 완료를 기다릴 수 없으므로 종료 요청만 시작합니다
window.addEventListener('beforeunload', () => {
SDK.destroy();
});
});
</script>
</body>
</html>