اگر مطمئن نیستید که چگونه داده های خود را سریالی کنید، نمونه کدهای زیر را بررسی کنید.
این راهنما نحوه پیاده سازی Co-Doing API را توضیح می دهد.
شروع کنید
برای استفاده از Co-Doing API، ابتدا باید افزونه Meet را مستقر کنید . هنگامی که این مراحل را کامل کردید، می توانید از داخل افزونه جدید خود از Co-Doing API استفاده کنید.
برای استفاده از Co-Doing API، با دریافت یک شی AddonSession ، که به عنوان نقطه ورود برای فعالیت های مشترک Google Meet عمل می کند، شروع کنید:
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},});
عنوان فعالیت خود را جایگزین ACTIVITY_TITLE کنید.
وضعیت فعلی را مدیریت کنید
هنگامی که کاربران در برنامه شما اقدامی انجام می دهند، انتظار می رود که برنامه شما فوراً متد broadcastStateUpdate() فراخوانی کند.
نمونه کد زیر پیاده سازی متد broadcastStateUpdate() را نشان می دهد:
تاریخ آخرین بهروزرسانی 2025-08-29 بهوقت ساعت هماهنگ جهانی.
[[["درک آسان","easyToUnderstand","thumb-up"],["مشکلم را برطرف کرد","solvedMyProblem","thumb-up"],["غیره","otherUp","thumb-up"]],[["اطلاعاتی که نیاز دارم وجود ندارد","missingTheInformationINeed","thumb-down"],["بیشازحد پیچیده/ مراحل بسیار زیاد","tooComplicatedTooManySteps","thumb-down"],["قدیمی","outOfDate","thumb-down"],["مشکل ترجمه","translationIssue","thumb-down"],["مشکل کد / نمونهها","samplesCodeIssue","thumb-down"],["غیره","otherDown","thumb-down"]],["تاریخ آخرین بهروزرسانی 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."]]