實作 Co-Doing API

Co-Doing API 可用於同步處理會議之間的任意資料 位參與者。可以是應用程式依附的任何資料。

您必須將資料序列化為 Uint8Array,才能傳輸。如要 請參閱 JavaScript 標準程式庫 參考資料

如果您不確定如何序列化資料,請查看下列程式碼範例。

本指南說明如何實作 Co-Doing API。

開始使用

如要使用 Co-Doing API,您必須先建構 Meet 外掛程式。一次 完成這些步驟後,即可開始使用 Co-Doing API 開啟新的外掛程式

如要使用 Co-Doing API,請先取得 AddonSession 物件, 這會做為 Google Meet 共同活動的進入點:

TypeScript

const session = await window.meet.addon.createAddonSession({
    cloudProjectNumber: "CLOUD_PROJECT_NUMBER",
});

CLOUD_PROJECT_NUMBER 替換為 管理 Google Cloud 專案

建立共同合作客戶

首先,請從 AddonSession 建立 CoDoingClient 物件。

如要建立 CoDoingClient,請呼叫 createCoDoingClient() 方法,並提供 CoDoingDelegate 物件。

CoDoingDelegate 是 Co-Doing API 採用的 會在應用程式變為新狀態時進行更新。這種情況下 這個 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 the new state to your ongoing CoDoing activity
    },
  });

ACTIVITY_TITLE 替換為活動的標題。

管理目前狀態

當使用者在應用程式中採取行動時,應用程式應該會立即啟動 呼叫 broadcastStateUpdate()敬上 方法。

以下程式碼範例顯示 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 替換為應用程式的目前狀態。