Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
496bb388
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,体验更适合开发者的 AI 搜索 >>
提交
496bb388
编写于
7月 09, 2015
作者:
L
Lachlan Sylvester
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
update README to include creating the ApplicationCabel::Channel
上级
2253ebd3
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
22 addition
and
10 deletion
+22
-10
README.md
README.md
+12
-0
lib/action_cable/channel/base.rb
lib/action_cable/channel/base.rb
+10
-10
未找到文件。
README.md
浏览文件 @
496bb388
...
...
@@ -62,6 +62,18 @@ module ApplicationCable
end
```
Then you should define your
`ApplicationCable::Channel`
class in Ruby. This is the place where you put
shared logic between your channels.
```
ruby
# app/channels/application_cable/channel.rb
```
ruby
module ApplicationCable
class Channel < ActionCable::Channel::Base
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
automatically sent to the connection instance when a new connection is attempted, and you
...
...
lib/action_cable/channel/base.rb
浏览文件 @
496bb388
...
...
@@ -7,7 +7,7 @@ module Channel
# Channel instances are long-lived. A channel object will be instantiated when the cable consumer becomes a subscriber, and then
# lives until the consumer disconnects. This may be seconds, minutes, hours, or even days. That means you have to take special care
# not to do anything silly in a channel that would balloon its memory footprint or whatever. The references are forever, so they won't be released
# as is normally the case with a controller instance that gets thrown away after every request.
# as is normally the case with a controller instance that gets thrown away after every request.
#
# Long-lived channels (and connections) also mean you're responsible for ensuring that the data is fresh. If you hold a reference to a user
# record, but the name is changed while that reference is held, you may be sending stale data if you don't take precautions to avoid it.
...
...
@@ -15,7 +15,7 @@ module Channel
# The upside of long-lived channel instances is that you can use instance variables to keep reference to objects that future subscriber requests
# can interact with. Here's a quick example:
#
# class ChatChannel < ApplicationChannel
# class ChatChannel < ApplicationC
able::C
hannel
# def subscribed
# @room = Chat::Room[params[:room_number]]
# end
...
...
@@ -39,19 +39,19 @@ module Channel
# def subscribed
# @connection_token = generate_connection_token
# end
#
#
# def unsubscribed
# current_user.disappear @connection_token
# end
#
#
# def appear(data)
# current_user.appear @connection_token, on: data['appearing_on']
# end
#
#
# def away
# current_user.away @connection_token
# end
#
#
# private
# def generate_connection_token
# SecureRandom.hex(36)
...
...
@@ -93,7 +93,7 @@ def perform_action(data)
end
end
# Called by the cable connection when its cut so the channel has a chance to cleanup with callbacks.
# Called by the cable connection when its cut so the channel has a chance to cleanup with callbacks.
# This method is not intended to be called directly by the user. Instead, overwrite the #unsubscribed callback.
def
unsubscribe_from_channel
run_unsubscribe_callbacks
...
...
@@ -113,8 +113,8 @@ def subscribed
def
unsubscribed
# Override in subclasses
end
# Transmit a hash of data to the subscriber. The hash will automatically be wrapped in a JSON envelope with
# Transmit a hash of data to the subscriber. The hash will automatically be wrapped in a JSON envelope with
# the proper channel identifier marked as the recipient.
def
transmit
(
data
,
via:
nil
)
logger
.
info
"
#{
self
.
class
.
name
}
transmitting
#{
data
.
inspect
}
"
.
tap
{
|
m
|
m
<<
" (via
#{
via
}
)"
if
via
}
...
...
@@ -139,7 +139,7 @@ def processable_action?(action)
def
dispatch_action
(
action
,
data
)
logger
.
info
action_signature
(
action
,
data
)
if
method
(
action
).
arity
==
1
public_send
action
,
data
else
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录