使用 Pusherapp 的私人频道(使用 Rails)
我刚刚完成了 Pusherapp 的hello world。现在我想创建私人频道,以便用户只阅读他们应该阅读的消息。
Pusher 文档仅提供了有关如何执行此操作的一些详细信息,我有点迷失了。
来自文档:
... 返回 Pusher JS 库 连接时的socket_id 推手。
当它尝试订阅 私人频道,它发回一个 AJAX 向您的服务器请求 Channel_name 和 socket_id 为 参数。
默认 URL 是 http://yourserver.com/pusher/auth。 ...
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),我如何验证该用户然后让他/她订阅相应的频道?
谢谢
I just got through the hello world for Pusherapp. Now I want to create private channels so users only read messages that they are supposed to read.
The Pusher docs only give some details on how to do this, and I'm kind of lost.
From the 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
http://yourserver.com/pusher/auth.
...
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
Given a unique user id (current_user.id), how can I authenticate that user then have him/her subscribe to the corresponding channel?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这篇关于实现的博客文章似乎对事情做了更多解释: https://pusher.com/docs/ client_api_guide/client_private_channels
听起来您的应用程序的业务逻辑应该对用户进行身份验证并决定他们应该访问专用通道。
他们的图表显示:
经过身份验证后,应用程序会请求订阅用户。 Pusher 使用 socket_id 进行回复。然后他们用它连接起来。
他们是这样描述的:
博客文章底部的示例进一步说明了:
假设您有一个名为 project-3 的频道,用户 A 和 B 可以访问该频道,但不能访问 C。您希望将此频道设为私有,以便用户 C 无法收听参加私人活动。只需将事件发送到 private-project-3 并在浏览器中订阅它即可。只要您使用最新的 javascript(版本 1.3 或更高版本),您就会看到向您的应用程序发出了一个发送到 /pusher/auth 的 POST 请求。目前这将失败,因此不会向套接字发出订阅请求。
所以,对我来说,这听起来像:
1)订阅请求发送给Pusher
2) Pusher POST 到您的 /auth 方法以确定用户是否可以访问该频道
3)如果您的业务逻辑允许用户访问此通道,则 auth 方法将返回“ok”响应:
我没有使用 Pusher 本身,但它的模型似乎反映了其他基于推送的模型的结构。希望这有帮助!
This blog post on the implementation seems to explain things a bit more: https://pusher.com/docs/client_api_guide/client_private_channels
Sounds like your application's business logic should authenticate the user and decide that they should access the private channel.
Their diagram shows:
Once authenticated, the app requests to subscribe the user. Pusher replies with the socket_id. Then they are connected using that.
Here's how they describe it:
The example at the bottom of the blog post further clarifies:
Suppose you have a channel called project-3, to which users A and B have access, but not C. You'd like to make this channel private so that user C cannot listen in on the private events. Simply send events to private-project-3 and subscribe to it in the browser. As long as you're using the latest javascript (version 1.3 or above), you'll see that a POST request is made to your application to /pusher/auth. This will currently fail, and therefore the subscribe request will not be made to the socket.
So, to me this sounds like:
1) Request to subscribe is sent to Pusher
2) Pusher POSTs to your /auth method to determine if the user can access the channel
3) If your business logic allows the user to access this channel, the auth method returns the "ok" response:
I haven't used Pusher itself, but its model seems to mirror the structure of other push-based models. Hope this helps!