นำ 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 ของคุณ

สร้างไคลเอ็นต์การทํางานร่วมกัน

เริ่มต้นด้วยการสร้างออบเจ็กต์ CoDoingClient จาก AddonSession

หากต้องการสร้าง 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 ด้วยสถานะปัจจุบันของแอป