Co-Doing API 구현

Google Meet Live Sharing 공동 작업 API는 회의 참여자 간에 임의의 데이터를 동기화하는 데 사용됩니다. 앱에서 사용하는 모든 데이터를 포함할 수 있습니다.

데이터를 전송하려면 데이터를 Uint8Array로 직렬화해야 합니다. 자세한 내용은 JavaScript 표준 라이브러리 참조를 확인하세요.

데이터를 직렬화하는 방법을 잘 모르겠다면 아래의 코드 샘플을 검토하세요.

이 가이드에서는 Co-Doing API를 구현하는 방법을 설명합니다.

CoDoingClient를 만드는 방법

시작하려면 시작하기에서 만든 AddonSession에서 CoDoingClient를 만듭니다.

CoDoingClient를 만들려면 AddonSession.createCoDoingClient 메서드를 호출하고 CoDoingDelegate를 제공합니다.

CoDoingDelegate는 새로운 상태가 제공될 때마다 Co-Doing API가 애플리케이션을 업데이트하는 방법입니다. CoDoingDelegate.onCoDoingStateChanged 메서드가 호출되면 애플리케이션에서 즉시 새로운 상태를 적용합니다.

다음 코드 샘플은 Co-Doing API를 사용하는 방법을 보여줍니다.

TypeScript

interface MyState {
  someString: string;
  someNumber: number;
}

/**
 * Serialize/deserialize using JSON.stringify
 * You can use any method you want; this is included for as an example
 */
function toBytes(state: MyState): Uint8Array {
  return new TextEncoder().encode(JSON.stringify(state));
}

function fromBytes(bytes: Uint8Array): MyState {
  return JSON.parse(new TextDecoder().decode(bytes)) as MyState;
}

  const coDoingClient = await addonSession.createCoDoingClient({
    activityTitle: "ACTIVITY_TITLE",
    onCoDoingStateChanged(coDoingState: CoDoingState) {
      const newState = fromBytes(coDoingState.bytes);
      // This function should apply newState to your ongoing CoDoing activity
    },
  });

ACTIVITY_TITLE를 활동 제목으로 바꿉니다.

현재 상태 관리

사용자가 애플리케이션에서 작업을 실행하면 애플리케이션이 즉시 CoDoingClient.broadcastStateUpdate를 호출해야 합니다.

다음 코드 샘플은 CoDoingClient.broadcastStateUpdate의 구현을 보여줍니다.

TypeScript

const myState: MyState = {
  someString: "SOME_STRING",
  someNumber: 0
};

document.getElementById('some-button').onClick(() => {
  myState.someNumber = myState.someNumber + 1;
  coDoingClient.broadcastStateUpdate({ bytes: toBytes(myState) });
});

SOME_STRING를 앱의 현재 상태로 바꿉니다.