HTML/JavaScript 사용 가이드
HTML/JavaScript를 사용하여 Klever One SDK를 통합하는 방법을 알아보세요.
준비사항
환경 요구사항
- 보안 컨텍스트 필수: HTTPS 또는 localhost
- 브라우저 지원: Chrome, Firefox, Safari (최신 버전)
- 권한: 마이크 접근 권한
SDK 파일 준비
예제는 게시된 SDK의 CDN을 사용합니다. 게시 전에는 npm run build:prod로 만든 UMD 파일을 로컬 경로로 지정해 테스트할 수 있습니다.
<!-- SDK 로드 -->
<script
defer
src="https://unpkg.com/@klever-one/web-sdk@0.1.0-beta.21/dist/core/klever-one-core-v0.1.0-beta.21.umd.js"
></script>
빠른 시작
기본 HTML 구조
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8" />
<meta
name="viewport"
content="width=device-width, initial-scale=1.0"
/>
<title>Klever One SDK Example</title>
<style>
#streaming-container {
width: 800px;
height: 600px;
background: #000;
margin: 20px auto;
}
.controls {
text-align: center;
margin: 20px;
}
.controls button {
margin: 5px;
padding: 10px 20px;
font-size: 16px;
}
</style>
</head>
<body>
<div id="streaming-container"></div>
<div class="controls">
<button id="connect-btn">연결</button>
<button
id="disconnect-btn"
disabled
>
연결 해제
</button>
<div id="status-display">연결 안됨</div>
</div>
<script
defer
src="https://unpkg.com/@klever-one/web-sdk@0.1.0-beta.21/dist/core/klever-one-core-v0.1.0-beta.21.umd.js"
></script>
<script
defer
src="./app.js"
></script>
</body>
</html>
기본 JavaScript 구현
// DOM 요소 참조
const elements = {
connectBtn: document.getElementById("connect-btn"),
disconnectBtn: document.getElementById("disconnect-btn"),
statusDisplay: document.getElementById("status-display"),
container: document.getElementById("streaming-container"),
};
let client = null;
// SDK 초기화 및 연결
async function initialize() {
try {
const { KleverOneClient } = window.KleverOneSdk;
client = new KleverOneClient({
apiKey: "your-api-key",
container: elements.container,
callbacks: {
onReady: () => updateStatus("준비 완료"),
onError: (error) => updateStatus(`오류: ${error.message}`),
onConnectionStatusChange: (status) => updateConnectionUI(status),
},
});
await client.connect();
} catch (error) {
updateStatus(`초기화 실패: ${error.message}`);
}
}
// 상태 표시 업데이트
function updateStatus(message) {
elements.statusDisplay.textContent = message;
}
// 연결 상태에 따른 UI 업데이트
function updateConnectionUI(status) {
const isConnected = status === "connected";
elements.connectBtn.disabled = isConnected;
elements.disconnectBtn.disabled = !isConnected;
updateStatus(isConnected ? "연결됨" : "연결 안됨");
}
// 이벤트 리스너 등록
elements.connectBtn.addEventListener("click", initialize);
elements.disconnectBtn.addEventListener("click", () => {
client?.disconnect();
updateStatus("연결 해제됨");
});
페르소나 생성과 성격 업데이트
사진·VRM은 현재 연결된 페르소나의 설정을 복사해 새 페르소나와 키를 만듭니다. 외부 페르소나 변환은 지원하는 JSON을 Klever 페르소나 형식으로 변환합니다. 세 기능 모두 로그인 액세스 토큰은 필요하지 않습니다.
| 기능 | 메서드 | 연결 필요 | 완료 결과 |
|---|---|---|---|
| 사진으로 생성 | createPersonaFromPhoto() | onReady 이후 | 새 페르소나와 personaKey |
| VRM으로 생성 | createPersonaFromVrm() | onReady 이후 | 새 페르소나와 personaKey |
| 외부 페르소나 변환 | updatePersonaPersonalityFromSurvey() | 필요 없음 | 성격 지침과 산출물 다운로드 URL |
다음 예제는 UMD 번들을 사용하는 독립된 화면입니다. 사진은 JPEG/PNG 최대 5MiB, VRM은 최대 100MiB, 설문은 UTF-8 JSON 최대 256KiB입니다. 파일 선택과 생성 버튼을 연결하고, 반환된 키 또는 성격 업데이트 결과를 표시합니다.
<div
id="persona-stream"
style="width: 800px; height: 400px"
></div>
<button
id="persona-connect"
type="button"
>
연결 시작
</button>
<button
id="persona-disconnect"
type="button"
>
연결 종료
</button>
<form id="persona-form">
<fieldset id="persona-fields">
<label
>방식
<select id="persona-source">
<option value="photo">사진</option>
<option value="vrm">VRM</option>
<option value="survey-file">페르소나 JSON 파일</option>
<option value="survey-json">페르소나 JSON 직접 입력</option>
</select>
</label>
<input
id="persona-file"
type="file"
accept="image/jpeg,image/png"
/>
<textarea
id="persona-json"
hidden
>
{"_schema":"survey/v1","answers":{"tone":"친근하고 차분한 말투"}}</textarea
>
<div id="persona-settings">
<input
id="persona-name"
aria-label="새 페르소나 이름"
value="새 페르소나"
/>
<select
id="persona-gender"
aria-label="성별"
>
<option value="F">F</option>
<option value="M">M</option>
</select>
</div>
<button
id="persona-submit"
type="submit"
disabled
>
실행
</button>
</fieldset>
</form>
<p
id="persona-status"
role="status"
></p>
<p
id="persona-error"
role="alert"
></p>
<input
id="persona-result-key"
aria-label="생성된 페르소나 키"
readonly
/>
<button
id="persona-copy"
type="button"
disabled
>
키 복사
</button>
<pre id="persona-result"></pre>
<script src="https://unpkg.com/@klever-one/web-sdk@0.1.0-beta.21/dist/core/klever-one-core-v0.1.0-beta.21.umd.js"></script>
<script>
const el = (id) => document.getElementById(id);
let busy = false;
let client;
client = new window.KleverOneSdk.KleverOneClient({
apiKey: "your-api-key",
container: el("persona-stream"),
callbacks: {
onReady: updateButton,
onDisconnect: updateButton,
onPersonaCreationStateChange: (state) => {
el("persona-status").textContent = state.status;
},
onError: (error) => {
el("persona-error").textContent = error.message;
},
},
});
function updateButton() {
const survey = el("persona-source").value.startsWith("survey");
el("persona-submit").disabled = busy || (!survey && !client?.isReady());
}
el("persona-connect").onclick = () => {
client.connect().catch((error) => {
el("persona-error").textContent = error.message;
});
};
el("persona-disconnect").onclick = () => client.disconnect();
el("persona-source").onchange = () => {
const source = el("persona-source").value;
el("persona-file").value = "";
el("persona-file").accept =
source === "vrm"
? ".vrm"
: source === "photo"
? "image/jpeg,image/png"
: ".json,application/json";
el("persona-file").hidden = source === "survey-json";
el("persona-json").hidden = source !== "survey-json";
el("persona-settings").hidden = source.startsWith("survey");
updateButton();
};
el("persona-form").onsubmit = async (event) => {
event.preventDefault();
if (busy) return;
busy = true;
el("persona-fields").disabled = true;
el("persona-error").textContent = "";
el("persona-result").textContent = "";
el("persona-result-key").value = "";
el("persona-copy").disabled = true;
el("persona-status").textContent = "처리 중";
try {
const source = el("persona-source").value;
const file = el("persona-file").files[0];
if (source !== "survey-json" && !file)
throw new Error("파일을 선택하세요.");
let result;
if (source.startsWith("survey")) {
const options =
source === "survey-json"
? { survey: JSON.parse(el("persona-json").value) }
: { file };
result = await client.updatePersonaPersonalityFromSurvey(options);
} else {
const options = {
file,
name: el("persona-name").value,
gender: el("persona-gender").value,
};
result =
source === "vrm"
? await client.createPersonaFromVrm(options)
: await client.createPersonaFromPhoto(options);
el("persona-result-key").value = result.personaKey;
el("persona-copy").disabled = false;
}
el("persona-result").textContent = JSON.stringify(result, null, 2);
el("persona-status").textContent = "완료";
} catch (error) {
el("persona-status").textContent = "실패";
el("persona-error").textContent = error.message;
} finally {
busy = false;
el("persona-fields").disabled = false;
updateButton();
}
};
el("persona-copy").onclick = async () => {
try {
await navigator.clipboard.writeText(el("persona-result-key").value);
} catch {
el("persona-error").textContent = "키를 직접 선택해 복사하세요.";
}
};
window.addEventListener("pagehide", () => client.disconnect());
</script>
사진·VRM 생성 후 새 키는 result.personaKey에서 읽습니다. 키는 결과 화면이나 앱 저장소에 보관하고, 새 페르소나로 연결할 시점은 앱에서 선택합니다. 설문 응답에는 새 키가 없으며 같은 키로 새로 연결하면 저장한 성격을 사용합니다. result.resultUrl은 완성된 member/v2 JSON을 다운로드하는 만료 없는 URL입니다.
사진·VRM 생성은 동시에 실행할 수 없습니다. 연결이 종료되면 생성 대기를 정리하며 이어하기는 제공하지 않습니다. 페이지를 새로고침하면 메모리에 보관한 생성 결과와 키가 사라집니다.
각 메서드의 입력값과 반환값은 API 문서를 참고하세요. examples/digital-human-sdk-example.html에도 파일명·크기 표시, 진행·실패·결과 및 키 복사 화면이 포함되어 있습니다.
연결 관리
연결 상태 관리
class ConnectionManager {
constructor(containerId, apiKey) {
this.container = document.getElementById(containerId);
this.apiKey = apiKey;
this.client = null;
this.status = "disconnected";
}
async connect() {
if (this.status === "connected") return;
try {
const { KleverOneClient } = window.KleverOneSdk;
this.client = new KleverOneClient({
apiKey: this.apiKey,
container: this.container,
callbacks: {
onReady: () => this.onStatusChange("connected"),
onDisconnect: () => this.onStatusChange("disconnected"),
onError: (error) => this.onError(error),
},
});
await this.client.connect();
} catch (error) {
this.onError(error);
}
}
disconnect() {
if (this.client) {
this.client.disconnect();
this.client = null;
}
}
async reconnect() {
this.disconnect();
await new Promise((resolve) => setTimeout(resolve, 1000));
await this.connect();
}
onStatusChange(newStatus) {
this.status = newStatus;
console.log("연결 상태:", newStatus);
}
onError(error) {
console.error("연결 오류:", error.message);
this.status = "error";
}
isConnected() {
return this.status === "connected";
}
}
자동 재연결 구현
class AutoReconnector {
constructor(connectionManager) {
this.connectionManager = connectionManager;
this.reconnectAttempts = 0;
this.maxRetries = 3;
this.retryInterval = 2000;
}
async startAutoReconnect() {
if (this.reconnectAttempts >= this.maxRetries) {
console.log("최대 재연결 시도 횟수 초과");
return;
}
this.reconnectAttempts++;
console.log(`재연결 시도 ${this.reconnectAttempts}/${this.maxRetries}`);
try {
await this.connectionManager.reconnect();
this.reconnectAttempts = 0; // 성공 시 카운터 리셋
} catch (error) {
console.error("재연결 실패:", error.message);
setTimeout(() => this.startAutoReconnect(), this.retryInterval);
}
}
}
메시지 전송
텍스트 메시지 전송
class MessageSender {
constructor(client) {
this.client = client;
}
async sendText(message) {
if (!this.client || !this.client.isReady()) {
throw new Error("클라이언트가 준비되지 않았습니다");
}
try {
await this.client.sendText(message);
console.log("메시지 전송 완료:", message);
return true;
} catch (error) {
console.error("메시지 전송 실패:", error.message);
return false;
}
}
speak(text) {
if (!this.client || !this.client.isReady()) {
console.warn("클라이언트가 준비되지 않았습니다");
return;
}
this.client.speak(text);
console.log("발화 요청:", text);
}
speakMultiple(texts) {
if (!this.client || !this.client.isReady()) {
console.warn("클라이언트가 준비되지 않았습니다");
return;
}
this.client.speak(texts);
console.log("순차 발화 요청:", texts.length + "개 문장");
}
}
입력 필드와 연동
function setupMessageInput() {
const messageInput = document.getElementById("message-input");
const sendBtn = document.getElementById("send-btn");
const speakBtn = document.getElementById("speak-btn");
const messageSender = new MessageSender(client);
sendBtn.addEventListener("click", async () => {
const message = messageInput.value.trim();
if (!message) return;
const success = await messageSender.sendText(message);
if (success) {
messageInput.value = "";
}
});
speakBtn.addEventListener("click", () => {
const message = messageInput.value.trim();
if (!message) return;
messageSender.speak(message);
messageInput.value = "";
});
// Enter 키로 전송
messageInput.addEventListener("keypress", (e) => {
if (e.key === "Enter" && !e.shiftKey) {
e.preventDefault();
sendBtn.click();
}
});
}
음성 녹음
녹음 관리
class RecordingManager {
constructor(client) {
this.client = client;
this.isRecording = false;
}
async startRecording() {
if (this.isRecording) return;
try {
await this.client.startRecording();
this.isRecording = true;
console.log("녹음 시작");
this.onRecordingStart();
} catch (error) {
console.error("녹음 시작 실패:", error.message);
}
}
async stopRecording() {
if (!this.isRecording) return;
try {
await this.client.stopRecording();
this.isRecording = false;
console.log("녹음 중지");
this.onRecordingStop();
} catch (error) {
console.error("녹음 중지 실패:", error.message);
}
}
async toggleRecording() {
if (this.isRecording) {
await this.stopRecording();
} else {
await this.startRecording();
}
}
onRecordingStart() {
const recordBtn = document.getElementById("record-btn");
if (recordBtn) {
recordBtn.textContent = " 녹음 중지";
recordBtn.classList.add("recording");
}
}
onRecordingStop() {
const recordBtn = document.getElementById("record-btn");
if (recordBtn) {
recordBtn.textContent = "녹음 시작";
recordBtn.classList.remove("recording");
}
}
}
녹음 버튼 구현
function setupRecordingButton() {
const recordBtn = document.createElement("button");
recordBtn.id = "record-btn";
recordBtn.textContent = "녹음 시작";
recordBtn.className = "record-button";
// 버튼 스타일
recordBtn.style.cssText = `
padding: 10px 20px;
font-size: 16px;
border: none;
border-radius: 5px;
background: #007bff;
color: white;
cursor: pointer;
transition: all 0.3s ease;
`;
const recordingManager = new RecordingManager(client);
recordBtn.addEventListener("click", () => recordingManager.toggleRecording());
document.querySelector(".controls").appendChild(recordBtn);
// 녹음 중일 때 스타일 변경
const style = document.createElement("style");
style.textContent = `
.record-button.recording {
background: #dc3545 !important;
animation: pulse 1s infinite;
}
@keyframes pulse {
0% { opacity: 1; }
50% { opacity: 0.7; }
100% { opacity: 1; }
}
`;
document.head.appendChild(style);
}
️고급 기능
이벤트 처리 시스템
class EventManager {
constructor() {
this.listeners = new Map();
}
on(event, callback) {
if (!this.listeners.has(event)) {
this.listeners.set(event, []);
}
this.listeners.get(event).push(callback);
}
off(event, callback) {
if (!this.listeners.has(event)) return;
const callbacks = this.listeners.get(event);
const index = callbacks.indexOf(callback);
if (index > -1) {
callbacks.splice(index, 1);
}
}
emit(event, data) {
if (!this.listeners.has(event)) return;
this.listeners.get(event).forEach((callback) => {
try {
callback(data);
} catch (error) {
console.error(`이벤트 핸들러 오류 (${event}):`, error);
}
});
}
setupClientEvents(client) {
const callbacks = {
onReady: () => this.emit("ready"),
onDisconnect: () => this.emit("disconnect"),
onMessageReceived: (message) => this.emit("message", message),
onError: (error) => this.emit("error", error),
};
Object.entries(callbacks).forEach(([key, callback]) => {
if (client.callbacks) {
const originalCallback = client.callbacks[key];
client.callbacks[key] = (...args) => {
originalCallback?.(...args);
callback(...args);
};
}
});
}
}
상태 모니터링 대시보드
class StatusDashboard {
constructor(client) {
this.client = client;
this.dashboardElement = null;
this.createDashboard();
this.startMonitoring();
}
createDashboard() {
this.dashboardElement = document.createElement("div");
this.dashboardElement.id = "status-dashboard";
this.dashboardElement.style.cssText = `
position: fixed;
top: 10px;
right: 10px;
background: rgba(0,0,0,0.8);
color: white;
padding: 15px;
border-radius: 5px;
font-family: monospace;
font-size: 12px;
z-index: 1000;
`;
document.body.appendChild(this.dashboardElement);
}
updateDashboard() {
if (!this.client) return;
const state = this.client.getState();
const metrics = this.client.getMetrics();
const statusHTML = `
<div><strong>상태 모니터</strong></div>
<div>연결: ${state.connection}</div>
<div>녹음: ${state.recording}</div>
<div>전송: ${metrics.messagesSent}개</div>
<div>수신: ${metrics.messagesReceived}개</div>
<div>오류: ${metrics.errorCount}개</div>
`;
this.dashboardElement.innerHTML = statusHTML;
}
startMonitoring() {
setInterval(() => this.updateDashboard(), 1000);
}
destroy() {
if (this.dashboardElement) {
document.body.removeChild(this.dashboardElement);
}
}
}
실제 사용 예제
완전한 대화형 애플리케이션
class DigitalHumanApp {
constructor() {
this.client = null;
this.eventManager = new EventManager();
this.connectionManager = null;
this.messageSender = null;
this.recordingManager = null;
this.statusDashboard = null;
this.initialize();
}
async initialize() {
try {
// UI 설정
this.setupUI();
// 연결 관리자 초기화
this.connectionManager = new ConnectionManager(
"streaming-container",
"your-api-key",
);
// 이벤트 리스너 설정
this.setupEventListeners();
console.log("애플리케이션 초기화 완료");
} catch (error) {
console.error("초기화 실패:", error);
}
}
setupUI() {
// 메시지 입력 필드 추가
const controlsDiv = document.querySelector(".controls");
controlsDiv.innerHTML += `
<div style="margin: 20px 0;">
<input type="text" id="message-input" placeholder="메시지를 입력하세요..."
style="width: 300px; padding: 10px; margin-right: 10px;">
<button id="send-btn">전송</button>
<button id="speak-btn">말하기</button>
</div>
`;
}
setupEventListeners() {
// 연결 버튼
document
.getElementById("connect-btn")
.addEventListener("click", () => this.connect());
// 연결 해제 버튼
document
.getElementById("disconnect-btn")
.addEventListener("click", () => this.disconnect());
// 이벤트 관리자 이벤트
this.eventManager.on("ready", () => {
console.log("디지털 휴먼 준비 완료!");
this.onReady();
});
this.eventManager.on("message", (message) => {
console.log("새 메시지:", message.content);
});
this.eventManager.on("error", (error) => {
console.error("오류:", error.message);
});
}
async connect() {
try {
await this.connectionManager.connect();
this.client = this.connectionManager.client;
// 매니저들 초기화
this.messageSender = new MessageSender(this.client);
this.recordingManager = new RecordingManager(this.client);
this.statusDashboard = new StatusDashboard(this.client);
// 이벤트 설정
this.eventManager.setupClientEvents(this.client);
} catch (error) {
console.error("연결 실패:", error);
}
}
disconnect() {
this.connectionManager?.disconnect();
this.statusDashboard?.destroy();
this.client = null;
this.messageSender = null;
this.recordingManager = null;
this.statusDashboard = null;
}
onReady() {
// 환영 메시지
this.messageSender?.speak("안녕하세요! 디지털 휴먼이 준비되었습니다.");
// 메시지 입력 활성화
this.setupMessageInput();
this.setupRecordingButton();
}
setupMessageInput() {
const messageInput = document.getElementById("message-input");
const sendBtn = document.getElementById("send-btn");
const speakBtn = document.getElementById("speak-btn");
sendBtn.addEventListener("click", () => {
const message = messageInput.value.trim();
if (message) {
this.messageSender.sendText(message);
messageInput.value = "";
}
});
speakBtn.addEventListener("click", () => {
const message = messageInput.value.trim();
if (message) {
this.messageSender.speak(message);
messageInput.value = "";
}
});
}
setupRecordingButton() {
setupRecordingButton(); // 위에서 정의한 함수 사용
}
}
// 애플리케이션 시작
window.addEventListener("DOMContentLoaded", () => {
window.app = new DigitalHumanApp();
});
에러 처리
글로벌 에러 핸들러
class ErrorHandler {
constructor() {
this.setupGlobalHandlers();
}
setupGlobalHandlers() {
window.addEventListener("error", (event) => {
this.handleError("스크립트 오류", event.error);
});
window.addEventListener("unhandledrejection", (event) => {
this.handleError("Promise 오류", event.reason);
});
}
handleError(type, error) {
const errorInfo = {
type,
message: error?.message || "알 수 없는 오류",
stack: error?.stack,
timestamp: new Date().toISOString(),
};
console.error("오류 발생:", errorInfo);
// 사용자에게 알림 (선택적)
this.showUserNotification(errorInfo);
}
showUserNotification(errorInfo) {
const notification = document.createElement("div");
notification.style.cssText = `
position: fixed;
bottom: 20px;
left: 20px;
background: #ff4444;
color: white;
padding: 15px;
border-radius: 5px;
z-index: 10000;
`;
notification.textContent = `오류: ${errorInfo.message}`;
document.body.appendChild(notification);
setTimeout(() => {
document.body.removeChild(notification);
}, 5000);
}
}
// 글로벌 에러 핸들러 활성화
new ErrorHandler();
주요 고려사항
성능 최적화
- lazy loading: SDK 스크립트를 필요할 때만 로드
- 이벤트 정리: 페이지 언로드 시 리스너 제거
- 메모리 관리: 사용하지 않는 객체 참조 해제
보안 고려사항
- API 키 보호: 환경 변수나 서버에서 관리
- HTTPS 사용: 프로덕션에서는 반드시 HTTPS
- 권한 요청: 마이크 권한을 명시적으로 요청
브라우저 호환성
- 모던 브라우저: ES6+ 기능 사용
- 폴리필: 필요시 적절한 폴리필 추가
- 기능 감지: 브라우저 기능 지원 여부 확인