加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 使用Pusherapp的私有频道(使用Rails)

发布时间:2020-12-17 03:52:17 所属栏目:百科 来源:网络整理
导读:我刚刚通过 hello world为Pusherapp.现在我想创建私有通道,以便用户只读取他们应该阅读的消息. Pusher的文档只提供了一些如何做到这一点的细节,我有点迷茫. 从docs开始: … The Pusher JS library is returned a socket_id when it connects to Pusher. Whe
我刚刚通过 hello world为Pusherapp.现在我想创建私有通道,以便用户只读取他们应该阅读的消息.

Pusher的文档只提供了一些如何做到这一点的细节,我有点迷茫.

从docs开始:


The Pusher JS library is returned
a socket_id when it connects to
Pusher.

When it attempts to subscribe to a
private channel,it sends back an AJAX
request to your server with the
channel_name and socket_id as
parameters.

The default URL for this is
07002.

class PusherController < ApplicationController
  def auth
    if current_user
      response = Pusher[params[:channel_name]].authenticate(params[:socket_id])
      render :json => response
    else
      render :text => "Not authorized",:status => '403'
    end
  end
end

给定唯一的用户ID(current_user.id),如何验证该用户然后让他/她订阅相应的频道?

谢谢

解决方法

这篇关于实现的博客文章似乎更多地解释了一些事情: https://pusher.com/docs/client_api_guide/client_private_channels

The authorization scheme is based on
the idea that,rather than
implementing custom user
authentication,and adding complexity
and state to pusher,we should trust
the existing level of authentication
offered by your application. We also
wanted to ensure that someone reading
data sent from your application to the
browser would not be able to connect
to a channel as that user,and
therefore couldn’t include any secrets
in the page HTML.

听起来您的应用程序的业务逻辑应该对用户进行身份验证并确定他们应该访问私有通道.

他们的图表显示:

经过身份验证后,应用程序会请求订阅用户. Pusher用socket_id回复.然后他们使用它连接.

以下是他们描述的方式:

As shown in this diagram,a unique
socket id is generated and sent to the
browser by Pusher. This is sent to
your application (1) via an AJAX
request which authorizes the user to
access the channel against your
existing authentication system. If
successful your application returns an
authorization string to the browser
signed with you Pusher secret. This is
sent to Pusher over the WebSocket,
which completes the authorization (2)
if the authorization string matches.

博客文章底部的示例进一步阐明:

假设您有一个名为project-3的频道,用户A和B可以访问该频道,但不能访问C.您希望将此频道设为私有,以便用户C无法收听私人事件.只需将事件发送到private-project-3并在浏览器中订阅即可.只要您使用最新的JavaScript(版本1.3或更高版本),您就会看到对您的应用程序发出POST请求/ pusher / auth.这将失败,因此订阅请求将不会发生在套接字上.

所以,对我来说这听起来像:
1)订阅请求发送给Pusher
2)Pusher POST到您的/ auth方法以确定用户是否可以访问该频道
3)如果您的业务逻辑允许用户访问此通道,则auth方法返回“ok”响应:

auth = Pusher[params[:channel_name]].socket_auth(params[:socket_id])

    content_type 'application/json'
    return JSON.generate({
      :auth => auth
    })

我没有使用过Pusher本身,但它的模型似乎反映了其他基于推送的模型的结构.希望这可以帮助!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读