【发布时间】:2017-06-22 10:14:15
【问题描述】:
我在javascript上成功集成了twilio chat api,但是我在.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
【问题讨论】:
-
只是为了确认一下,当您以其他用户身份登录并开始输入时,您是否发现没有为一个用户触发输入事件?
-
是的,你是对的,应该从其他聊天用户那里进行测试,而不是在我们自己的聊天窗口中进行测试
标签: javascript twilio twilio-api