適用於 JavaScript 的 Azure RoomsApi 用戶端連結庫 - 1.1.1 版
此套件包含 Azure RoomsApi 用戶端的同型 SDK(在 Node.js 和瀏覽器中執行)。
通訊室用戶端
開始
目前支持的環境
- LTS 版本的 Node.js
- 最新版的 Safari、Chrome、Edge 和 Firefox。
先決條件
- Azure 訂用帳戶。
- 現有的通訊服務資源。 如果您需要建立資源,您可以使用 Azure 入口網站、[Azure PowerShell][azure_powershell],或 Azure CLI。
JavaScript 套件組合
若要在瀏覽器中使用此用戶端連結庫,您必須先使用配套程式。 如需如何執行這項操作的詳細資訊,請參閱我們的 組合檔。
安裝
npm install @azure/communication-rooms
重要概念
RoomsApiClient
RoomsClient
是開發人員使用 Azure RoomsApi 用戶端連結庫的主要介面。 探索此客戶端物件上的方法,以瞭解您可以存取之 Azure RoomsApi 服務的不同功能。
例子
認證
您可以在 Azure 入口網站中,從通訊服務資源取得金鑰和/或連接字串,。 擁有金鑰之後,您可以使用下列任一方法驗證 RoomsClient
:
在初始化用戶端之前,請先使用 AzureKeyCredential
建立 KeyCredential
import { AzureKeyCredential } from "@azure/core-auth";
import { RoomsClient } from "@azure/communication-rooms";
const credential = new AzureKeyCredential(KEY);
const client = new RoomsClient(ENDPOINT, credential);
使用連接字串
import { RoomsClient } from "@azure/communication-rooms";
const connectionString = `endpoint=ENDPOINT;accessKey=KEY`;
const client = new RoomsClient(connectionString);
使用 TokenCredential
import { DefaultAzureCredential } from "@azure/identity";
import { RoomsClient } from "@azure/communication-rooms";
const credential = new DefaultAzureCredential();
const client = new RoomsClient(ENDPOINT, credential);
如果您使用金鑰來初始化用戶端,則也需要提供適當的端點。 您可以從 Azure 入口網站中的通訊服務資源取得此端點,
用法
建立會議室
若要建立會議室,請呼叫 createRoom
方法。 所有設定都是選擇性的。
如果未提供 validFrom
,則會預設為目前的日期時間。 如果未提供 validUntil
,則預設值為 validFrom + 180 days
。
定義 participants
時,如果未指定 role
,則預設會 attendee
。
從 1.1.0 版開始,會新增 PstnDialOutEnabled
屬性,以啟用或停用會議室中的 PSTN 撥出功能。
PstnDialOutEnabled
是選擇性屬性。 如果未提供 PstnDialOutEnabled
,則 PstnDialOutEnabled
的預設值為 false。
// create users with CommunicationIdentityClient
const identityClient = new CommunicationIdentityClient(connectionString);
const user1 = await identityClient.createUserAndToken(["voip"]);
// create RoomsClient
const roomsClient: RoomsClient = new RoomsClient(CONNECTION_STRING);
const validFrom = new Date(Date.now());
const validForDays = 10;
const validUntil = new Date(validFrom.getTime());
validUntil.setDate(validFrom.getDate() + validForDays);
const pstnDialOutEnabled = true;
// options payload to create a room
const createRoomOptions: CreateRoomOptions = {
validFrom,
validUntil,
pstnDialOutEnabled,
participants: [
{
id: user1.user,
role: "Attendee",
},
],
};
// create room
const room = await roomsClient.createRoom(createRoomOptions);
在這裡尋找 CommunicationIdentityClient
更新會議室
若要更新會議室的 validFrom
和 validUntil
設定,請使用 updateRoom
方法。
從 1.1.0 版開始,會新增 PstnDialOutEnabled
屬性,以啟用或停用會議室中的 PSTN 撥出功能。
validForDays = 60;
validUntil.setDate(validFrom.getDate() + validForDays);
pstnDialOutEnabled = false;
const updateRoomOptions: UpdateRoomOptions = {
validFrom,
validUntil,
pstnDialOutEnabled,
};
// update the room using the room id from the creation operation
const updatedRoom = await roomsClient.updateRoom(room.id, updateRoomOptions);
取得房間
若要取得會議室,請使用 getRoom
方法。
const roomId = "ROOM_ID";
room = await roomsClient.getRoom(roomId);
列出會議室
使用 listRooms
方法列出所有會議室。
const roomsList = await roomsClient.listRooms();
for await (const currentRoom of roomsList) {
// access room data
console.log(`The room id is ${currentRoom.id}.`);
}
新增或更新參與者
若要新增參與者或更新現有的參與者,請使用 addOrUpdateParticipants
方法。
const user2 = await identityClient.createUserAndToken(["voip"]);
const updateParticipantsList: RoomParticipantPatch[] = [
{
id: user1.user,
role: "Presenter",
},
{
id: user2.user,
},
];
// run addOrUpdate operation
await roomsClient.addOrUpdateParticipants(room.id, updateParticipantsList);
拿掉參與者
若要移除參與者,請呼叫 removeParticipants
方法。
const participantsToRemove = [user1.user, user2.user];
await roomsClient.removeParticipants(room.id, participantsToRemove);
取得會議室中的參與者
若要列出會議室中的所有參與者,請呼叫 listParticipants
方法。
const participantsList = await roomsClient.listParticipants(room.id);
for await (const participant of participantsList) {
// access participant data
console.log(`The participant's role is ${participant.role}.`);
}
刪除會議室
使用 deleteRoom
方法來刪除會議室。
await roomsClient.deleteRoom(room.id);
故障排除
伐木
啟用記錄可能有助於找出有關失敗的實用資訊。 若要查看 HTTP 要求和回應的記錄,請將 AZURE_LOG_LEVEL
環境變數設定為 info
。 或者,您可以在運行時間啟用記錄,方法是在 @azure/logger
中呼叫 setLogLevel
:
const { setLogLevel } = require("@azure/logger");
setLogLevel("info");
如需如何啟用記錄的詳細指示,請參閱
後續步驟
如需如何使用此連結庫的詳細範例,請參閱 範例 目錄。
貢獻
如果您想要參與此連結庫,請閱讀 參與指南,以深入瞭解如何建置和測試程序代碼。
相關專案