1

我在 ReactJS 应用程序中有以下函数,它应该初始化我正在使用的 Twilio 服务。但是,似乎没有正确访问 Twilio 频道。这是我的代码:

componentDidMount() {
    let chatClient = this;
    $.ajax({
        method: "GET",
        url: 'get_twilio_token',
        data: {device: chatClient.device},
        success: (data) => {
            let accessManager = new Twilio.AccessManager(data.token);
            //let messagingClient = new Twilio.Chat.Client(data.token);
            let messagingClient = new Twilio.Chat.Client.create(data.token).then(client => {
                client.getUserChannelDescriptors().then(channels => {
                    let channelsHash = {};
                    console.log('inside callback of messagingClient2')
                    channels.items.map(channel => {
                        channel.on('messageAdded', () => {})
                        channelsHash[channel.uniqueName] = channel;
                    });
                });
            });
        }
    });
}

TypeError: channel.on is not a function此函数在行中抛出一条错误消息channel.on('messageAdded', () => {})

任何帮助表示赞赏。谢谢。

4

2 回答 2

5

getUserChannelDescriptors()返回ChannelDescriptorsChannels。为了让Channel你必须调用getChannel描述符:https ://media.twiliocdn.com/sdk/js/chat/releases/1.0.0/docs/ChannelDescriptor.html#getChannel__anchor

于 2017-09-03T19:12:24.157 回答
0

这就是我的做法

async function getChannels() {

// Initialize the chat client
this.chatClient = new Chat(this.state.twilioToken);
await this.chatClient.initialize();

// Get channel descriptors
this.chatClient.getUserChannelDescriptors().then(paginator => {
    let channels = [];
    let channelsBulkFetch = [];

    if (paginator.items.length) {
        channels = paginator.items;
        // Loop through all channels and call getChannel() for each cahnnel
        for (let i = 0; i < paginator.items.length; i++) {
            channelsBulkFetch.push(channels[i].getChannel());
        }

        // Loop through each channel detailed object and perform various operations
        channels.map(channel => {
            // Do whatever you want with channel object

            channel.on('messageAdded', this.messageAdded);
        });
    }
 })
}

对于与最后消息时间戳相对应的排序通道

async function getSortedChannels() {

// Initialize the chat client
this.chatClient = new Chat(this.state.twilioToken);
await this.chatClient.initialize();

// Get channel descriptors
this.chatClient.getUserChannelDescriptors().then(paginator => {
    let channels = [];
    let sortedChannels = [];
    let channelsBulkFetch = [];

    if (paginator.items.length) {
        channels = paginator.items;
        // Loop through all channels and call getChannel() for each cahnnel
        for (let i = 0; i < paginator.items.length; i++) {
            channelsBulkFetch.push(channels[i].getChannel());
        }

        /**
         * Additional part for sorting
         */
        sortedChannels = channels.sort(function (a, b) {
            // Turn strings into dates, and then subtract them
            // If channel doesn't have any message consider the dateDreated for sorting
            return new Date(b.lastMessage ? b.lastMessage.timestamp : b.dateCreated) - new Date(a.lastMessage ? a.lastMessage.timestamp : a.dateCreated);
        });

        // Loop through each channel detailed object and perform various operations
        sortedChannels.map(channel => {
            // Do whatever you want with channel object

            channel.on('messageAdded', this.messageAdded);
        });
    }
 })
}
于 2019-01-10T07:18:40.420 回答