Skip to content

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; }
  </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 type="text"></chat-container>
      <div class="input-group">
        <input type="text" id="msg-input" placeholder="메시지 입력..." />
        <button id="btn-send">전송</button>
      </div>
      <div class="input-group">
        <input type="text" id="speak-input" placeholder="아바타가 읽을 텍스트..." />
        <button id="btn-speak">speak</button>
      </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 초기화 성공');
      } 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>