นำ Co-Doing API ไปใช้

ใช้ API การแชร์สดของ Google Meet เพื่อซิงค์ข้อมูลที่กำหนดเองระหว่างการประชุม คน ซึ่งอาจเป็นข้อมูลที่แอปของคุณต้องใช้

คุณต้องเรียงลำดับข้อมูลลงใน Uint8Array เพื่อส่ง สำหรับ สำหรับข้อมูลเพิ่มเติม โปรดดูที่ไลบรารีมาตรฐาน JavaScript ข้อมูลอ้างอิง

หากคุณไม่แน่ใจว่าจะเรียงลำดับข้อมูลอย่างไร ให้ตรวจสอบตัวอย่างโค้ดด้านล่าง

คู่มือนี้จะอธิบายวิธีใช้งาน Co-Doing API

สร้าง CoDoingClient

ในการเริ่มต้นใช้งาน ให้สร้าง CoDoingClient จากAddonSession ที่คุณสร้างไว้ในเริ่มต้นใช้งาน

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