2017-06-22 61 views
0

我正在成功地集成於JavaScript的twilio聊天API,但是我對.typing()函數有問題,這似乎打字功能不觸發Twilio聊天打字()不工作

「typingStarted」和'打字',我可以有一些建議嗎?

這裏是我的代碼使用這個版本的API

https://media.twiliocdn.com/sdk/js/chat/v1.0/twilio-chat.js

+0

只是爲了確認,當您以不同的用戶身份登錄並開始輸入時,是否發現打字事件不會爲一名用戶開火? – philnash

+1

是的你是對的,它應該從其他聊天用戶進行測試,而不是在我們自己的聊天窗口中進行測試 –

回答

1

我不好,代碼還行,但是我試圖用一個錯誤的使用情況下,我們需要測試

var chatChannel; 
    var chatClient; 
    var username; 

    $.post("/tokens", function(data) { 
    username = data.username; 
    chatClient = new Twilio.Chat.Client(data.token); 
    chatClient.getSubscribedChannels().then(createOrJoinGeneralChannel); 
    }); 

    function createOrJoinGeneralChannel() { 
    // Get the general chat channel, which is where all the messages are 
    // sent in this simple application 
    // print('Attempting to join "general" chat channel...'); 
    var promise = chatClient.getChannelByUniqueName("#{params[:chat_channel]}"); 
    promise.then(function(channel) { 
     chatChannel = channel; 
     console.log("#{params[:chat_channel]} is exist"); 
     console.log(chatChannel); 
     setupChannel(); 
    }).catch(function() { 
     // If it doesn't exist, let's create it 
     console.log("creating #{params[:chat_channel]} channel"); 
     chatClient.createChannel({ 
      uniqueName: "#{params[:chat_channel]}", 
      friendlyName: 'General Chat Channel' 
     }).then(function(channel) { 
      console.log("Created #{params[:chat_channel]} channel:"); 
      console.log(channel); 
      chatChannel = channel; 
      setupChannel(); 
     }); 
    }); 
    } 

    function setupChannel() { 
    chatChannel.join().then(function(channel) { 
     printMessage(username + ' joined the chat.'); 
     chatChannel.on('typingStarted', showTypingStarted); 
     chatChannel.on('typingEnded', hideTypingStarted); 
    }); 
    chatChannel.on('messageAdded', function(message) { 
     printMessage(message.author + ": " + message.body); 
    }); 
    } 

    function showTypingStarted(member) { 
    console.log('somebody is typing'); 
    $('#is_typing').html(member.identity + ' is typing...') 
    } 

    function hideTypingStarted(member) { 
    $('#is_typing').html(''); 
    } 

    var $input = $('#chat-input'); 
    $input.on('keydown', function(e) { 
    if (e.keyCode == 13) { 
     chatChannel.sendMessage($input.val()); 
     $input.val(''); 
    } else { 
     //console.log('typing'); 
     chatChannel.typing(); 
    } 
    }); 

IM它來自聊天用戶的另一邊,而不是我們自己的聊天窗口

歡呼聲