CoDoingDelegate 是 Co-Doing API 在有新状态可用时更新应用的方式。预计当 onCoDoingStateChanged() 方法被调用时,您的应用会立即应用新状态。
以下代码示例展示了如何使用 Co-Doing API:
TypeScript
interfaceMyState{someString:string;someNumber:number;}/** * Serialize/deserialize using JSON.stringify * You can use any method you want; this is included for as an example */functiontoBytes(state:MyState):Uint8Array{returnnewTextEncoder().encode(JSON.stringify(state));}functionfromBytes(bytes:Uint8Array):MyState{returnJSON.parse(newTextDecoder().decode(bytes))asMyState;}constcoDoingClient=awaitaddonSession.createCoDoingClient({activityTitle:"ACTIVITY_TITLE",onCoDoingStateChanged(coDoingState:CoDoingState){constnewState=fromBytes(coDoingState.bytes);// This function should apply the new state to your ongoing CoDoing activity},});
[null,null,["最后更新时间 (UTC):2025-08-29。"],[],[],null,["# Implement the Co-Doing API\n\n| **Early Access Program:** This feature was only available in limited preview, through an Early Access Program. This program is now closed to new signups.\n\nThe Co-Doing API is used to synchronize arbitrary data between meeting\nparticipants. This can be any data that your app depends on.\n\nYou must serialize the data to a `Uint8Array` for it to be transmitted. For more\ninformation, see the [JavaScript standard library\nreference](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array).\n\nIf you aren't sure how to serialize your data, review the code samples\nfollowing.\n\nThis guide explains how to implement the Co-Doing API.\n\nGet started\n-----------\n\nTo use the Co-Doing API, you first must [Deploy a\nMeet add-on](/workspace/meet/add-ons/guides/deploy-add-on). Once\nyou've completed those steps, you can start using the Co-Doing API\nfrom within your new add-on.\n\nTo use the Co-Doing API, start by getting an\n[`AddonSession`](/workspace/meet/add-ons/reference/websdk/addon_sdk.addonsession) object,\nwhich serves as the entry point for Google Meet co-activities: \n\n### TypeScript\n\n const session = await window.meet.addon.createAddonSession({\n cloudProjectNumber: \"\u003cvar translate=\"no\"\u003eCLOUD_PROJECT_NUMBER\u003c/var\u003e\",\n });\n\nReplace \u003cvar translate=\"no\"\u003eCLOUD_PROJECT_NUMBER\u003c/var\u003e with the project number of\nyour Google Cloud project.\n\nCreate a co-doing client\n------------------------\n\nTo get started, create a\n[`CoDoingClient`](/workspace/meet/add-ons/reference/websdk/live_sharing_sdk.codoingclient)\nobject from your `AddonSession`.\n\nTo create a `CoDoingClient`, call the\n[`createCoDoingClient()`](/workspace/meet/add-ons/reference/websdk/addon_sdk.addonsession.createcodoingclient)\nmethod and provide a\n[`CoDoingDelegate`](/workspace/meet/add-ons/reference/websdk/live_sharing_sdk.codoingdelegate)\nobject.\n\nThe `CoDoingDelegate` is how the Co-Doing API\nupdates your app whenever it has a new state available. It's expected that, when\nthe\n[`onCoDoingStateChanged()`](/workspace/meet/add-ons/reference/websdk/live_sharing_sdk.codoingdelegate.oncodoingstatechanged)\nmethod is called, your app immediately applies the new state.\n\nThe following code sample shows how to use the Co-Doing API: \n\n### TypeScript\n\n interface MyState {\n someString: string;\n someNumber: number;\n }\n\n /**\n * Serialize/deserialize using JSON.stringify\n * You can use any method you want; this is included for as an example\n */\n function toBytes(state: MyState): Uint8Array {\n return new TextEncoder().encode(JSON.stringify(state));\n }\n\n function fromBytes(bytes: Uint8Array): MyState {\n return JSON.parse(new TextDecoder().decode(bytes)) as MyState;\n }\n\n const coDoingClient = await addonSession.createCoDoingClient({\n activityTitle: \"\u003cvar translate=\"no\"\u003eACTIVITY_TITLE\u003c/var\u003e\",\n onCoDoingStateChanged(coDoingState: CoDoingState) {\n const newState = fromBytes(coDoingState.bytes);\n // This function should apply the new state to your ongoing CoDoing activity\n },\n });\n\nReplace \u003cvar translate=\"no\"\u003eACTIVITY_TITLE\u003c/var\u003e with the title of your activity.\n\nManage current state\n--------------------\n\nWhen users take action in your app, it's expected that your app immediately\ncalls the\n[`broadcastStateUpdate()`](/workspace/meet/add-ons/reference/websdk/live_sharing_sdk.codoingclient.broadcaststateupdate)\nmethod.\n\nThe following code sample shows an implementation of the\n`broadcastStateUpdate()` method: \n\n### TypeScript\n\n const myState: MyState = {\n someString: \"\u003cvar translate=\"no\"\u003eSOME_STRING\u003c/var\u003e\",\n someNumber: 0\n };\n\n document.getElementById('some-button').onClick(() =\u003e {\n myState.someNumber = myState.someNumber + 1;\n coDoingClient.broadcastStateUpdate({ bytes: toBytes(myState) });\n });\n\nReplace \u003cvar translate=\"no\"\u003eSOME_STRING\u003c/var\u003e with the app's current state."]]