Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
fb902ea2
R
rails
项目概览
张重言
/
rails
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
rails
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
fb902ea2
编写于
7月 08, 2015
作者:
D
David Heinemeier Hansson
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Update README.md
上级
e408cc8b
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
97 addition
and
86 deletion
+97
-86
README.md
README.md
+97
-86
未找到文件。
README.md
浏览文件 @
fb902ea2
# Action Cable
--
Integrated websockets for Rails
# Action Cable
–
Integrated websockets for Rails
Action Cable seamlessly integrates websockets with the rest of your Rails application.
It allows for real-time features to be written in Ruby in the same style
...
...
@@ -36,29 +36,31 @@ reflections of each unit.
## A full-stack example
The first thing you must do is defined your
ApplicationCable::Connection
class in Ruby. This
The first thing you must do is defined your
`ApplicationCable::Connection`
class in Ruby. This
is the place where you do authorization of the incoming connection, and proceed to establish it
if all is well. Here's the simplest example starting with the server-side connection class:
# app/channels/application_cable/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
if current_user = User.find cookies.signed[:user_id]
current_user
else
reject_unauthorized_connection
end
end
```
ruby
# app/channels/application_cable/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
if
current_user
=
User
.
find
cookies
.
signed
[
:user_id
]
current_user
else
reject_unauthorized_connection
end
end
end
end
```
This relies on the fact that you will already have handled authentication of the user, and
that a successful authentication sets a signed cookie with the user_id. This cookie is then
...
...
@@ -69,9 +71,11 @@ potentially disconnect them all if the user is deleted or deauthorized).
The client-side needs to setup a consumer instance of this connection. That's done like so:
# app/assets/javascripts/cable.coffee
@App = {}
App.cable = Cable.createConsumer "http://cable.example.com"
```
coffeescript
# app/assets/javascripts/cable.coffee
@
App
=
{}
App
.
cable
=
Cable
.
createConsumer
"http://cable.example.com"
```
The http://cable.example.com address must point to your set of Action Cable servers, and it
must share a cookie namespace with the rest of the application (which may live under http://example.com).
...
...
@@ -89,59 +93,63 @@ Here's a simple example of a channel that tracks whether a user is online or not
First you declare the server-side channel:
# app/channels/appearance_channel.rb
class AppearanceChannel < ApplicationCable::Channel
def subscribed
current_user.appear
end
def unsubscribed
current_user.disappear
end
def appear(data)
current_user.appear on: data['appearing_on']
end
def away
current_user.
away
end
```
ruby
# app/channels/appearance_channel.rb
class
AppearanceChannel
<
ApplicationCable
::
Channel
def
subscribed
current_user
.
appear
end
def
unsubscribed
current_user
.
disappear
end
def
appear
(
data
)
current_user
.
appear
on:
data
[
'appearing_on'
]
end
def
away
current_user
.
away
end
end
```
The
#subscribed
callback is invoked when, as we'll show below, a client-side subscription is initiated. In this case,
The
`#subscribed`
callback is invoked when, as we'll show below, a client-side subscription is initiated. In this case,
we take that opportunity to say "the current user has indeed appeared". That appear/disappear API could be backed by
Redis or a database or whatever else. Here's what the client-side of that looks like:
# app/assets/javascripts/cable/subscriptions/appearance.coffee
App.appearance = App.cable.subscriptions.create "AppearanceChannel",
connected: ->
# Called once the subscription has been successfully completed
appear: ->
@perform 'appear', appearing_on: @appearingOn()
away: ->
@perform 'away'
appearingOn: ->
$('main').data 'appearing-on'
$(document).on 'page:change', ->
App.appearance.appear()
$(document).on 'click', '[data-behavior~=appear_away]', ->
App.appearance.away()
false
Simply calling App.cable.subscriptions.create will setup the subscription, which will call AppearanceChannel#subscribed,
which in turn is linked to original App.consumer -> ApplicationCable::Connection instances.
We then link App.appearance#appear to AppearanceChannel#appear(data). This is possible because the server-side
```
coffeescript
# app/assets/javascripts/cable/subscriptions/appearance.coffee
App
.
appearance
=
App
.
cable
.
subscriptions
.
create
"AppearanceChannel"
,
connected
:
->
# Called once the subscription has been successfully completed
appear
:
->
@
perform
'appear'
,
appearing_on
:
@
appearingOn
()
away
:
->
@
perform
'away'
appearingOn
:
->
$
(
'main'
).
data
'appearing-on'
$
(
document
).
on
'page:change'
,
->
App
.
appearance
.
appear
()
$
(
document
).
on
'click'
,
'[data-behavior~=appear_away]'
,
->
App
.
appearance
.
away
()
false
```
Simply calling
`App.cable.subscriptions.create`
will setup the subscription, which will call
`AppearanceChannel#subscribed`
,
which in turn is linked to original
`App.consumer`
->
`ApplicationCable::Connection`
instances.
We then link
`App.appearance#appear`
to
`AppearanceChannel#appear(data)`
. This is possible because the server-side
channel instance will automatically expose the public methods declared on the class (minus the callbacks), so that these
can be reached as remote procedure calls via
App.appearance#perform
.
can be reached as remote procedure calls via
`App.appearance#perform`
.
Finally, we expose
App.appearance to the machinations of the application itself by hooking the #appear
call into the
Turbolinks
page:change callback and allowing the user to click a data-behavior link that triggers the #away
call.
Finally, we expose
`App.appearance`
to the machinations of the application itself by hooking the
`#appear`
call into the
Turbolinks
`page:change`
callback and allowing the user to click a data-behavior link that triggers the
`#away`
call.
## Channel example 2: Receiving new web notifications
...
...
@@ -153,26 +161,29 @@ action on the client.
This is a web notification channel that allows you to trigger client-side web notifications when you broadcast to the right
streams:
# app/channels/web_notifications.rb
class WebNotificationsChannel < ApplicationCable::Channel
def subscribed
stream_from "web_notifications_#{current_user.id}"
end
```
ruby
# app/channels/web_notifications.rb
class
WebNotificationsChannel
<
ApplicationCable
::
Channel
def
subscribed
stream_from
"web_notifications_
#{
current_user
.
id
}
"
end
# Somewhere in your app this is called, perhaps from a NewCommentJob
ActionCable.server.broadcast
\
"web_notifications_1", { title: 'New things!', body: 'All shit fit for print' }
# Client-side which assumes you've already requested the right to send web notifications
App.cable.subscriptions.create "WebNotificationsChannel",
received: (data) ->
web_notification = new Notification data['title'], body: data['body']
The ActionCable.server.broadcast call places a message in the Redis' pubsub queue under the broadcasting name of "web_notifications_1".
end
```
```
coffeescript
# Somewhere in your app this is called, perhaps from a NewCommentJob
ActionCable
.
server
.
broadcast
\
"web_notifications_1"
,
{
title
:
'New things!'
,
body
:
'All shit fit for print'
}
# Client-side which assumes you've already requested the right to send web notifications
App
.
cable
.
subscriptions
.
create
"WebNotificationsChannel"
,
received
:
(
data
)
->
web_notification
=
new
Notification
data
[
'title'
],
body
:
data
[
'body'
]
```
The
`ActionCable.server.broadcast`
call places a message in the Redis' pubsub queue under the broadcasting name of "web_notifications_1".
The channel has been instructed to stream everything that arrives at "web_notifications_1" directly to the client by invoking the
#received(data)
callback. The data is the hash sent as the second parameter to the server-side broadcast call, JSON encoded for the trip
across the wire, and unpacked for the data argument arriving to
#received
.
`#received(data)`
callback. The data is the hash sent as the second parameter to the server-side broadcast call, JSON encoded for the trip
across the wire, and unpacked for the data argument arriving to
`#received`
.
## Dependencies
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录