基本上我想在我的Rails网站上添加实时聊天室。在我的应用中,有两种型号:Client
和Professionnel
。客户可以向专业人士询问报价。 (编辑:报价也是一个模型)
对于每个报价,我希望在客户和专业人员之间进行实时聊天。
我已经使用经典的消息频道完成了它,将正确的消息附加到正确的引号。然而,它不符合隐私原则,因为每个客户和每个专业人员都会收到所有聊天消息。
在服务器级别,我可以轻松地将用户(客户端或专业人员)订阅到特定于报价的流,例如:
class QuoteChannel < ApplicationCable::Channel
def subscribed
if current_user
if current_user.quotes.any?
current_user.quotes.each do |quote|
stream_from "#{quote.hashed_id.to_s}_channel"
end
end
end
end
def unsubscribed
end
end
虽然我对客户端有点困难。我的quote.coffee.erb不接受我使用current_user
(在Actioncable连接文件中定义)或任何设计标识符current_client
或current_professionnel
。
所以我不确定如何在客户端对我的订阅进行个性化。我已经读过可以在消息中捕获元素广播,但我不知道如何处理我当前的Coffee.erb文件:
App.quote = App.cable.subscriptions.create "QuoteChannel",
connected: ->
# Called when the subscription is ready for use on the server
disconnected: ->
# Called when the subscription has been terminated by the server
received: (data) ->
$('#cell'+data.quoteid).append '<div>'+'<span>'+data.message+'</span>'+'</div>'
$('.sendmessageinputtext').val("")
quoteid传递给收到的函数,但我需要创建与用户拥有引号一样多的流。在这个帖子http://www.thegreatcodeadventure.com/rails-5-action-cable-with-multiple-chatroom-subscriptions/中,导师遍历所有可用的聊天室,我可以做同样的事情,但它是愚蠢的,因为在某个时间可能有成千上万的实时引号,其中只有少数由{{1已经允许Actioncable连接。