Node.js 版快速入门

本指南介绍如何设置和运行示例 Google Meet 插件。

目标

  • 设置示例。
  • 运行该示例。

前提条件

设置示例

  1. 在工作目录中,运行以下命令以初始化您的 项目:

     npm init
    
  2. 在您的工作目录中,安装 Express.js

     npm install express --save
    
  3. 安装 Meet 插件 Web SDK

     npm install @googleworkspace/meet-addons --save
    
  4. 在您的工作目录中,创建一个名为 index.js 的文件并将 以下代码:

     const express = require('express');
     const path = require('path');
    
     var app = express();
     app = require("https-localhost")();
    
     app.use(express.static(path.join(__dirname, '/')));
     app.use('/', express.static(__dirname + '/node_modules/@googleworkspace/meet-addons'));
    
     app.get('/sidepanel', function(req, res){
       res.render(path.join(__dirname, 'sidepanel.html'));
     });
    
     app.get('/mainstage', function(req, res){
       res.render(path.join(__dirname, 'mainstage.html'));
     });
    
     app.listen(3000);
    
  5. 在您的工作目录中,创建一个名为 mainstage.html 的文件并将 以下代码:

     <html style="width: 100%; height: 100%">
    
     <head>
         <title>Main Stage Add On</title>
         <script src="meet.addons.js"></script>
     </head>
    
     <body style="width: 100%; height: 100%; margin: 0; background: white;">
         <div style="display: flex; flex-direction: column; height: 100%">
             <h1>Main Stage Add-on</h1>
             <div>
                 <div>
                     <button id="get-collaboration-starting-state">
                         getCollaborationStartingState
                     </button>
                 </div>
                 <div id="receivedCollaborationStartingState"
                     style="margin-left: 20px; width: 300px; overflow-wrap: anywhere"></div>
             </div>
         </div>
    
         <script>
             let mainStageClient;
             async function init() {
                 const session = await window.meet.addon.createAddonSession({
                     cloudProjectNumber: "CLOUD_PROJECT_NUMBER",
                 });
                 console.log("Successfully constructed the add-on session.");
                 const mainStageClient = await session.createMainStageClient();
                 console.log("Successfully constructed main stage client.");
                 document
                     .getElementById('get-collaboration-starting-state')
                     .addEventListener(
                         'click', async () => {
                             document.getElementById(
                                 'receivedCollaborationStartingState').textContent =
                                 JSON.stringify(
                                     await mainStageClient.getCollaborationStartingState());
                         });
             }
             document.body.onload = () => {
                 init();
             };
         </script>
     </body>
    
     </html>
    
  6. 在您的工作目录中,创建一个名为 sidepanel.html 的文件并将 以下代码:

     <html style="width: 100%; height: 100%">
    
     <head>
         <title>Side Panel Add-on</title>
         <script src="meet.addons.js"></script>
     </head>
    
     <body style="width: 100%; height: 100%; margin: 0">
         <div style="display: flex; flex-direction: column; height: 100%">
             <h1>Side Panel Add-on</h1>
             <div>
                 <div>
                     <button id="set-collaboration-starting-state">
                         setCollaborationStartingState
                     </button>
                 </div>
                 <div>
                     <input type="text" id="sidePanelIframeUrl" style="margin-left: 20px"
                         value="https://localhost:3000/sidepanel.html" />
                 </div>
                 <div>
                     <input type="text" id="mainStageIframeUrl" style="margin-left: 20px"
                         value="https://localhost:3000/mainstage.html" />
                 </div>
                 <div>
                     <input type="text" id="additionalData" style="margin-left: 20px" value="additional data" />
                 </div>
             </div>
         </div>
    
         <script>
             let sidePanelClient;
             async function init() {
                 const session = await window.meet.addon.createAddonSession({
                     cloudProjectNumber: "CLOUD_PROJECT_NUMBER",
                 });
                 console.log("Successfully constructed the add-on session.");
                 sidePanelClient = await session.createSidePanelClient();
                 console.log("Successfully constructed side panel client.");
    
                 document
                     .getElementById('set-collaboration-starting-state')
                     .addEventListener(
                         'click', async () => {
                             const sidePanelIframeUrlInputElement =
                                 document.getElementById('sidePanelIframeUrl');
                             const mainStageIframeUrlInputElement =
                                 document.getElementById('mainStageIframeUrl');
                             const additionalDataInputElement =
                                 document.getElementById('additionalData');
                             await sidePanelClient.setCollaborationStartingState({
                                 sidePanelUrl: sidePanelIframeUrlInputElement.value,
                                 mainStageUrl: mainStageIframeUrlInputElement.value,
                                 additionalData: additionalDataInputElement.value,
                             });
                         });
             }
             document.body.onload = () => {
                 init();
             };
         </script>
    
     </body>
    
     </html>
    
  7. 替换 mainstage.htmlsidepanel.html 文件的以下内容:

    • CLOUD_PROJECT_NUMBER:您的 Google Cloud 项目
  8. 在您的工作目录中,安装 https-localhost 软件包:

     npm install https-localhost --save
    
  9. 在您的工作目录中,运行该示例:

     node index.js
    
  10. 在浏览器中,前往 https://localhost:3000/mainstage.html,然后 https://localhost:3000/sidepanel.html 以验证网页。

创建 Meet 插件

按照 有关如何构建 Meet 插件的说明。

运行示例

  1. 转到 Meet

  2. 点击活动图标 活动的图标。

  3. 您的插件部分,您应该会看到 Google Meet Add-ons Quickstart。选中该节点即可运行 插件。