首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ActionCable.server =>‘/cable’仅限于authenticated_rooth_path

ActionCable.server =>‘/cable’仅限于authenticated_rooth_path
EN

Stack Overflow用户
提问于 2017-07-10 21:19:26
回答 1查看 895关注 0票数 0

考虑以下情况:

1) WebSocket验证连接.

代码语言:javascript
复制
def connect
  self.current_user = find_verified_user
  logger.add_tags "ActionCable", "User #{current_user.id}"
end

( 2)当建立连接时,通知用户

代码语言:javascript
复制
  connected: ->
    $("body").append("<div class='connection ok'>Connected.</div>")

3)当连接丢失时,通知用户

代码语言:javascript
复制
  disconnected: ->
    $("pop-up").append("<div class='connection'>Offline, trying to reconnect...</div>")

4)当用户注销时.

代码语言:javascript
复制
An unauthorized connection attempt was rejected
###User is now informed connection is lost. Which should not happen.

我的问题:如何更改:

代码语言:javascript
复制
  mount ActionCable.server => '/cable'

若要仅在的范围内运行,请执行以下操作:

代码语言:javascript
复制
 authenticated :user do
   root 'users#index', as: :authenticated_root
 end
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-07-11 13:06:16

替代解

未经授权的连接尝试被拒绝。

...happens,当reject_unauthorized_connectionconnection.rb中被调用时。

  • 这可能是故意的而不是
代码语言:javascript
复制
- remove `reject_unauthorized_connection` if you want to allow non-signed-users to subscribe to the channel: `current_user` becomes `nil`
代码语言:javascript
复制
    - To be able to still identify the user, you can add another identifier (`:session_id`) : 

模块ApplicationCable类连接< ActionCable:: Connection ::Base identified_by :current_user identified_by :session_id def self.current_user = find_verified_user self.session_id = request.session.id end私有def find_verified_user User.find_by(id: cookies.signed:user_id) end #.

-如果您需要客人和登录用户之间的进一步授权规则,您可能希望在您的*_channel.rb中而不是在connection.rb中写入您自己的授权。

代码语言:javascript
复制
- retain `reject_unauthorized_connection` if you only want signed-in users to be able to subscribe to your channels.

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45021756

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档