【发布时间】:2018-11-28 20:26:41
【问题描述】:
在我的 Rails 拍卖应用中,授权用户可以在产品页面内同时连接到 2 个频道(一个是产品的 all_users 频道,另一个是用于直接消息传递的用户特定频道。)
现在我只想将敏感信息发送给管理员组用户。我认为我可以在咖啡脚本中定义第三个通道连接请求(admin_channel),但我不知道如何根据角色授权第三个通道的用户连接。
另一种选择可能是利用现有的用户特定频道,但在这里我无法弄清楚后端类如何知道管理员组中的哪些用户当前在线(有一个用户频道启动和运行)..
您知道我该如何实现吗?任何形式的支持将不胜感激..
您可以在下面找到我现有的 connection.rb 文件和 coffeescript 文件。
这是我的 connection.rb 文件:
module ApplicationCable
class Connection < ActionCable::Connection::Base
identified_by :current_user
def connect
self.current_user = find_verified_user
end
protected
def find_verified_user # this checks whether a user is authenticated with devise
if verified_user = env['warden'].user
verified_user
else
reject_unauthorized_connection
end
end
end
end
咖啡脚本:
$( document ).ready ->
App.myauction = App.cable.subscriptions.create({
channel: 'MyauctionChannel'
id: $('#auctionID').attr('data-id')
},
connected: ->
console.log "Connected"
# Called when the subscription is ready for use on the server
disconnected: ->
# Called when the subscription has been terminated by the server
speak: (message) ->
@perform 'speak', message: message
received: (data) ->
console.log(data)
# Called when there's incoming data on the websocket for this channel
)
App.myauctionuser = App.cable.subscriptions.create({
channel: 'MyauctionChannel',
id: $('#auctionID').attr('data-uuid-code')
},
connected: ->
console.log "user connected"
# Called when the subscription is ready for use on the server
disconnected: ->
# Called when the subscription has been terminated by the server
speak: (message) ->
@perform 'speak', message: message
received: (data) ->
# console.log ("user channel ")
# console.log(data)
)
【问题讨论】:
-
他们是否连接到同一个频道
MyauctionChannel?但是他们连接到不同的房间,如您的id: $('#auctionID').attr('data-id')所标识,而另一个是id: $('#auctionID').attr('data-uuid-code')? -
@Jay-Ar Polidario 我试图对问题进行更多澄清。如您所述,用户连接到基于产品的“广播”频道 $('#auctionID').attr('data-uuid -code') 以及用户特定的频道。