Klleon Chat SDK
Korean
Korean
  • ✨New Javascript SDK [1.2.0]
    • 시작하기
    • 이벤트
    • UI
    • 메서드
      • 라이프 사이클 메서드
      • 텍스트 대화
      • 음성대화
      • 에코 대화
      • 오디오 에코 대화
      • 기타 메서드
    • 타입스크립트 지원
    • 예제
      • VanillaJS
      • React
      • Nextjs
    • 로그 시스템
    • 업데이트 내역
  • JavaScript SDK [v0.x.x] (deprecated 예정)
    • 시작하기
    • 초기화
    • 아바타 스트리밍
      • 스트리밍 시작하기
      • 스트리밍 중단
      • 스트리밍 화면 출력 제어
    • 채팅 화면
    • 텍스트 메시지
    • 보이스 메시지
    • 에코 - 발화
    • 이벤트 구독
    • 업데이트 내역
  • 서비스 호출 구조
Powered by GitBook
LogoLogo

Products

  • Pricing

Websites

  • Homepage
  • Youtube
  • Linkedin

Copyright © Klleon. All rights reserved

On this page
  1. New Javascript SDK [1.2.0]
  2. 예제

React

// index.html

<!doctype html>
<html lang="en">
  <head>
    <script src="https://web.sdk.klleon.io/1.0.0/klleon-chat.umd.js"></script>
  </head>
  <body>
    <div id="root"></div>
    <script type="module" src="/src/main.tsx"></script>
  </body>
</html>

// App.tsx

interface AvatarProps {
  videoStyle?: CSSProperties;
}

interface ChatProps {
  delay?: number;
  type?: "voice" | "text";
  isShowCount?: boolean;
}

function App() {
  const avatarRef = useRef<HTMLElement & AvatarProps>(null);
  const chatRef = useRef<HTMLElement & ChatProps>(null);

  useEffect(() => {
    // <avatar-container /> props
    if (avatarRef.current) {
      avatarRef.current.videoStyle = {
        borderRadius: "30px",
        objectFit: "contain",
      };
    }
    // <chat-container /> props
    if (chatRef.current) {
      chatRef.current.delay = 10;
      chatRef.current.type = "voice";
      chatRef.current.isShowCount = false;
    }

    const initKlleonChat = async () => {
      const { KlleonChat } = window;
      KlleonChat.onStatusEvent((data) => {
        console.log(data, "status data");
      });
      KlleonChat.onChatEvent((data) => {
        console.log(data, "chat data");
      });

      await KlleonChat.init({
        sdk_key: "your sdk_key",
        avatar_id: "your avatar_id",
      });
    };
    initKlleonChat();

    return () => {
      //
      const { KlleonChat } = window;
      KlleonChat.destroy();
    };
  }, []);

  const sdkHandler = {
    handleEcho: () => {
      const { KlleonChat } = window;
      KlleonChat.echo("hello world");
    },
    handleAvatarChange: (avatar_id:string) => {
      const { KlleonChat } = window;
      KlleonChat.changeAvatar({
        avatar_id,
      });
    },
    handleClear: () => {
      const { KlleonChat } = window;
      KlleonChat.clearMessageList();
    },
  };

  return (
    <main>
      <div
        style={{
          width: "100vw",
          height: "100vh",
        }}
      >
        <div
          style={{
            display: "flex",
            flexDirection: "column",
            gap: "24px",
            width: "100%",
            height: "100%",
          }}
        >
          <div
            style={{
              display: "flex",
              height: "800px",
            }}
          >
            <div
              style={{
                flex: 1,
              }}
            >
              <avatar-container
                ref={avatarRef}
                style={{
                  borderRadius: "24px",
                }}
                class="your class"
              />
            </div>
            <div
              style={{
                flex: 1,
              }}
            >
              <chat-container ref={chatRef} class="your class" />
            </div>
          </div>
          <div
            style={{
              display: "flex",
              gap: "24px",
            }}
          >
            <button onClick={sdkHandler.handleEcho}>echo</button>
            <button onClick={() => sdkHandler.handleAvatarChange('change-avatar-id')}>
              change avatar
            </button>
            <button onClick={sdkHandler.handleClear}>clear message</button>
          </div>
        </div>
      </div>
    </main>
  );
}

export default App;
PreviousVanillaJSNextNextjs

Last updated 4 months ago

✨