我在這里向您詢問 Twilio 功能。現在我正在使用 Twilio Service 開發會議服務,但遇到了一個難題。當我使用連接 API 時,回呼回傳一個包含參與者身份的 JSON 物件。我想發送帶有參與者身份的參與者名稱。
這是連接功能。
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"
}
有沒有人可以幫助我解決這個問題?謝謝你。
uj5u.com熱心網友回復:
你有兩種方法來獲得它:
如果您在后面呼叫 Twilio API,然后創建提供給前面的物件,請在后面適當地更改它,以便它準確地回傳您在前端所需的內容。
如果您從前面直接呼叫 Twilio API(或者您不想在后面進行更改),您可以使用類進行適當的介面和映射。像這樣的東西:
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");
})
uj5u.com熱心網友回復:
Twilio 開發人員布道者在這里。
該Participant物件沒有participant_name可用于傳遞資料的任意屬性,例如 。
我建議您在后端創建一個 API,該 API 可以接收參與者身份并回傳有關參與者的資料。這樣,當參與者連接時,您可以向后端發出請求以獲取有關他們的更多資料。
或者,您可以使用DataTrack API將此類資料發送給其他參與者。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/421070.html
標籤:
上一篇:陣列格式問題
