นำ 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

สร้างไคลเอ็นต์ Co-Doing

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