Twilio聊天打字()不起作用

时间:2017-06-22 10:14:15

标签: javascript twilio twilio-api

我成功地将twilio chat api整合到javascript上,但是我在.typing()函数上遇到了问题,似乎打字功能没有触发

'typingStarted'和'typingEnded',我可以提一些建议吗?

这是我的代码

  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();
    }
  });

即时通讯使用此版本的api

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

1 个答案:

答案 0 :(得分:1)

我的错,代码还可以,但我尝试使用错误的用例,我们需要从聊天用户的另一端进行测试,而不是在我们自己的聊天窗口进行测试

欢呼声