簡體   English   中英

Twilio 2.x:通過連接發送自定義參數 api

[英]Twilio 2.x: Send custom parameters via connect api

我是來問你關於 Twilio 功能的。 現在我正在使用 Twilio 服務開發會議服務,但遇到了一個難題。 當我使用連接 API 時,回調返回一個包含參與者身份的 JSON object。 我想發送帶有參與者身份的參與者名稱。

這是連接 function。

createLocalTracks(options)
  .then(localTracks => {
    return connect(room.accessToken, {
      name: room.roomName,
      tracks: localTracks,
    })
  }).then(room => {
    console.log(room.data);    // Returns JSON Object that includes only participant_identity.
    this.participants.push("You");
  })

控制台結果如下。

{
  dominantSpeaker: null
  isRecording: true
  localParticipant: {
    ...,
    identity: 'xxx',
    ...
  }
  mediaRegion: "us1"
  name: "Soundblock.Project.2608117C-F92E-442A-A67D-4ED428522CE0"
  participants: []
  sid: "RM6336d72cb198fa58aa37f66af9eaf02d"
  state: "connected"
}

我想獲得帶有身份的參與者名稱。 所以結果一定是這樣的。

{
  dominantSpeaker: null
  isRecording: true
  localParticipant: {
    ...,
    identity: 'xxx',
    participant_name: 'ABC'
    ...
  }
  mediaRegion: "us1"
  name: "Soundblock.Project.2608117C-F92E-442A-A67D-4ED428522CE0"
  participants: [
    { identity: 'YYY', participant_name: 'BCD' },
    { identity: 'ZZZ', participant_name: 'CDE' },
    ...
  ]
  sid: "RM6336d72cb198fa58aa37f66af9eaf02d"
  state: "connected"
}

有沒有人可以幫助我解決這個問題? 謝謝你。

你有兩種方法來獲得它:

  1. 如果您在后面調用 Twilio API 然后創建在前面提供的 object,請在后面適當地更改它,以便它在前端返回您需要的內容。

  2. 如果您從前面直接調用 Twilio API(或者您不想在后面更改),您可以使用 class 進行適當的接口和映射。 像這樣的東西:

interface ParticipantI {
  // NOTE: Put the right types on it
  dominantSpeaker: string;
  isRecording: boolean;
  localParticipant: {
    ...,
    identity: string;
    ...
  }
  mediaRegion: string;
  name: string;
  participants: {identity: 'string'; participant_name: 'string';}[]
  sid: string;
  state: string;
}

export class Participant {

  dominantSpeaker: string;
  isRecording: boolean;
  localParticipant: {
    ...,
    identity: string;
    participant_name?: string;
    ...
  }
  mediaRegion: string;
  name: string;
  participants: {identity: 'string'; participant_name: 'string';}[]
  sid: string;
  state: string;

  constructor (participant:ParticipantI) {

  this.dominantSpeaker = participant.dominantSpeaker;
  this.isRecording= participant.isRecording;
  this.localParticipant = participant.localParticipant;
  this.participants = participant.participants;
  ... // Complete the assignment of the rest of the fields

  // Managing the transformations
  // Look up for 'participant_name'
  const _participant_name = this.participants.find( ({ identity }) => identity === this.localParticipant.identity );
  // Add it to localParticipant
  this.localParticipant.participant_name = _participant_name.participant_name;
  // Delete it from this.participants
  this.participants = this.participants.filter(participan => participan.identity != _participant_name.identity);
 

  }

}

管理您對 API 的呼叫:

createLocalTracks(options)
  .then(localTracks => {
    return connect(room.accessToken, {
      name: room.roomName,
      tracks: localTracks,
    })
  }).then(room => {
    console.log(room.data);    // Returns JSON Object that includes only participant_identity.


    const myExample: Participant  = new Participant(room.data);
    console.log(myExample);


    this.participants.push("You");
  })

Twilio 開發人員布道師在這里。

Participant object沒有可用於傳遞數據的任意屬性,例如participant_name名稱。

我建議您在后端創建一個 API,它可以接收參與者身份並返回有關參與者的數據。 這樣,當參與者連接時,您可以向后端發出請求以獲取有關他們的更多數據。

或者,您可以使用DataTrack API將此類數據發送給其他參與者。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM