我正在开发一个聊天功能,我希望在同一个广播频道中有几个独立的活动:
随着消息的定期流动。我已经明白这些事件中的每一个都正确地点击received: (data) ->
回调,并正确显示给正确的用户。
所有事件都是在客户端使用不同的触发器提交的,例如keydown,submit等,并以不同的方式处理,但它们最终点击同一个收到的回调。如何在同一个received(data) ->
回调中找出这些事件的范围,以涵盖用户何时停止输入(但未发送消息?)的功能?
例如,当用户输入时:
$(".new_im_message").on("focusin",function(e) {
var values = $("#typing_user").val();
App.instantmessage.is_typing(values);
});
适当处理,然后点击收到的回调:
received: (data) ->
$(".user_is_typing").show();
用户不再输入内容,也不会发送任何消息
$(".new_im_message").on("blur",function(e) {
var values = $("#typing_user").val();
App.instantmessage.is_blur(values);
});
适当处理,然后点击收到:
received: (data) ->
$(".user_is_typing").show();
$(".user_is_typing").hide(); <<<< can't hide and show at the same time..
如何分割事件?
received1(data)
received2(data)
等。谢谢!
答案 0 :(得分:2)
每当你在JS代码中调用perform()
(即用户输入时)时,我建议传递动作;即它可能看起来像
@perform('typing')
然后在你的频道(ruby代码)中,你需要有一个方法来响应上面的动作:
def subscribed
stream_from 'someidentifier'
# this line is optional. I just added this to immediately notify everyone in the chatroom that a new user joined in to the chatroom
ActionCable.server.broadcast 'someidentifier', action: 'subscribed', user_id: current_user.id
end
# this will be called by the JS `@perform('typing')` above
def typing
# we then broadcast to everyone in that chat room (a.k.a "someidentifier" chatroom), that a specific user is currently typing a message. Modify this as you wish
ActionCable.server.broadcast 'someidentifier', action: 'typing', user_id: current_user.id
end
def blur
ActionCable.server.broadcast 'someidentifer', action: 'blur', user_id: current_user.id
end
然后回到JS代码中的received(data)
函数中,我们将操作分离到适当的响应逻辑中:
received: (data) ->
switch data.action
when 'subscribed'
# do something when a new user (data.user_id) has joined in the chatroom
console.log(data.user_id)
when 'typing'
# do something when a user (data.user_id) is typing
console.log(data.user_id)
when 'blur'
# do something when a user (data.user_id) stopped typing
console.log(data.user_id)