Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
fc1b32f8
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,发现更多精彩内容 >>
提交
fc1b32f8
编写于
3月 02, 2016
作者:
M
Matthew Draper
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #23976 from danielrhodes/enhancement/ac-ping-to-message-type
ActionCable: Add a "welcome" and "ping" message type
上级
1fdc8394
53e163dc
变更
7
隐藏空白更改
内联
并排
Showing
7 changed file
with
24 addition
and
19 deletion
+24
-19
actioncable/app/assets/javascripts/action_cable/connection.coffee
...ble/app/assets/javascripts/action_cable/connection.coffee
+4
-1
actioncable/app/assets/javascripts/action_cable/connection_monitor.coffee
...assets/javascripts/action_cable/connection_monitor.coffee
+2
-4
actioncable/app/assets/javascripts/action_cable/subscriptions.coffee
.../app/assets/javascripts/action_cable/subscriptions.coffee
+2
-4
actioncable/lib/action_cable.rb
actioncable/lib/action_cable.rb
+2
-3
actioncable/lib/action_cable/connection/base.rb
actioncable/lib/action_cable/connection/base.rb
+5
-5
actioncable/test/client_test.rb
actioncable/test/client_test.rb
+8
-1
actioncable/test/connection/base_test.rb
actioncable/test/connection/base_test.rb
+1
-1
未找到文件。
actioncable/app/assets/javascripts/action_cable/connection.coffee
浏览文件 @
fc1b32f8
...
...
@@ -73,8 +73,11 @@ class ActionCable.Connection
events
:
message
:
(
event
)
->
{
identifier
,
message
,
type
}
=
JSON
.
parse
(
event
.
data
)
switch
type
when
message_types
.
welcome
@
consumer
.
connectionMonitor
.
connected
()
when
message_types
.
ping
@
consumer
.
connectionMonitor
.
ping
()
when
message_types
.
confirmation
@
consumer
.
subscriptions
.
notify
(
identifier
,
"connected"
)
when
message_types
.
rejection
...
...
actioncable/app/assets/javascripts/action_cable/connection_monitor.coffee
浏览文件 @
fc1b32f8
...
...
@@ -7,10 +7,7 @@ class ActionCable.ConnectionMonitor
@
staleThreshold
:
6
# Server::Connections::BEAT_INTERVAL * 2 (missed two pings)
identifier
:
ActionCable
.
INTERNAL
.
identifiers
.
ping
constructor
:
(
@
consumer
)
->
@
consumer
.
subscriptions
.
add
(
this
)
@
start
()
connected
:
->
...
...
@@ -22,11 +19,12 @@ class ActionCable.ConnectionMonitor
disconnected
:
->
@
disconnectedAt
=
now
()
received
:
->
ping
:
->
@
pingedAt
=
now
()
reset
:
->
@
reconnectAttempts
=
0
@
consumer
.
connection
.
isOpen
()
start
:
->
@
reset
()
...
...
actioncable/app/assets/javascripts/action_cable/subscriptions.coffee
浏览文件 @
fc1b32f8
...
...
@@ -58,7 +58,5 @@ class ActionCable.Subscriptions
sendCommand
:
(
subscription
,
command
)
->
{
identifier
}
=
subscription
if
identifier
is
ActionCable
.
INTERNAL
.
identifiers
.
ping
@
consumer
.
connection
.
isOpen
()
else
@
consumer
.
send
({
command
,
identifier
})
@
consumer
.
send
({
command
,
identifier
})
actioncable/lib/action_cable.rb
浏览文件 @
fc1b32f8
...
...
@@ -29,10 +29,9 @@ module ActionCable
extend
ActiveSupport
::
Autoload
INTERNAL
=
{
identifiers:
{
ping:
'_ping'
.
freeze
},
message_types:
{
welcome:
'welcome'
.
freeze
,
ping:
'ping'
.
freeze
,
confirmation:
'confirm_subscription'
.
freeze
,
rejection:
'reject_subscription'
.
freeze
}
...
...
actioncable/lib/action_cable/connection/base.rb
浏览文件 @
fc1b32f8
...
...
@@ -115,7 +115,7 @@ def statistics
end
def
beat
transmit
ActiveSupport
::
JSON
.
encode
(
identifier:
ActionCable
::
INTERNAL
[
:identifier
s
][
:ping
],
message:
Time
.
now
.
to_i
)
transmit
ActiveSupport
::
JSON
.
encode
(
type:
ActionCable
::
INTERNAL
[
:message_type
s
][
:ping
],
message:
Time
.
now
.
to_i
)
end
def
on_open
# :nodoc:
...
...
@@ -155,7 +155,7 @@ def cookies
def
handle_open
connect
if
respond_to?
(
:connect
)
subscribe_to_internal_channel
confirm_connection_monitor_subscription
send_welcome_message
message_buffer
.
process!
server
.
add_connection
(
self
)
...
...
@@ -174,11 +174,11 @@ def handle_close
disconnect
if
respond_to?
(
:disconnect
)
end
def
confirm_connection_monitor_subscription
# Send
confirmation
message to the internal connection monitor channel.
def
send_welcome_message
# Send
welcome
message to the internal connection monitor channel.
# This ensures the connection monitor state is reset after a successful
# websocket connection.
transmit
ActiveSupport
::
JSON
.
encode
(
identifier:
ActionCable
::
INTERNAL
[
:identifiers
][
:ping
],
type:
ActionCable
::
INTERNAL
[
:message_types
][
:confirmation
])
transmit
ActiveSupport
::
JSON
.
encode
(
type:
ActionCable
::
INTERNAL
[
:message_types
][
:welcome
])
end
def
allow_request_origin?
...
...
actioncable/test/client_test.rb
浏览文件 @
fc1b32f8
...
...
@@ -75,7 +75,7 @@ def initialize(port)
@ws
.
on
(
:message
)
do
|
event
|
hash
=
JSON
.
parse
(
event
.
data
)
if
hash
[
'
identifier'
]
==
'_
ping'
if
hash
[
'
type'
]
==
'
ping'
@pings
+=
1
else
@messages
<<
hash
...
...
@@ -146,6 +146,7 @@ def faye_client(port)
def
test_single_client
with_puma_server
do
|
port
|
c
=
faye_client
(
port
)
assert_equal
({
"type"
=>
"welcome"
},
c
.
read_message
)
# pop the first welcome message off the stack
c
.
send_message
command:
'subscribe'
,
identifier:
JSON
.
dump
(
channel:
'EchoChannel'
)
assert_equal
({
"identifier"
=>
"{
\"
channel
\"
:
\"
EchoChannel
\"
}"
,
"type"
=>
"confirm_subscription"
},
c
.
read_message
)
c
.
send_message
command:
'message'
,
identifier:
JSON
.
dump
(
channel:
'EchoChannel'
),
data:
JSON
.
dump
(
action:
'ding'
,
message:
'hello'
)
...
...
@@ -162,6 +163,7 @@ def test_interacting_clients
barrier_2
=
Concurrent
::
CyclicBarrier
.
new
(
clients
.
size
)
clients
.
map
{
|
c
|
Concurrent
::
Future
.
execute
{
assert_equal
({
"type"
=>
"welcome"
},
c
.
read_message
)
# pop the first welcome message off the stack
c
.
send_message
command:
'subscribe'
,
identifier:
JSON
.
dump
(
channel:
'EchoChannel'
)
assert_equal
({
"identifier"
=>
'{"channel":"EchoChannel"}'
,
"type"
=>
"confirm_subscription"
},
c
.
read_message
)
c
.
send_message
command:
'message'
,
identifier:
JSON
.
dump
(
channel:
'EchoChannel'
),
data:
JSON
.
dump
(
action:
'ding'
,
message:
'hello'
)
...
...
@@ -181,6 +183,7 @@ def test_many_clients
clients
=
100
.
times
.
map
{
faye_client
(
port
)
}
clients
.
map
{
|
c
|
Concurrent
::
Future
.
execute
{
assert_equal
({
"type"
=>
"welcome"
},
c
.
read_message
)
# pop the first welcome message off the stack
c
.
send_message
command:
'subscribe'
,
identifier:
JSON
.
dump
(
channel:
'EchoChannel'
)
assert_equal
({
"identifier"
=>
'{"channel":"EchoChannel"}'
,
"type"
=>
"confirm_subscription"
},
c
.
read_message
)
c
.
send_message
command:
'message'
,
identifier:
JSON
.
dump
(
channel:
'EchoChannel'
),
data:
JSON
.
dump
(
action:
'ding'
,
message:
'hello'
)
...
...
@@ -194,12 +197,14 @@ def test_many_clients
def
test_disappearing_client
with_puma_server
do
|
port
|
c
=
faye_client
(
port
)
assert_equal
({
"type"
=>
"welcome"
},
c
.
read_message
)
# pop the first welcome message off the stack
c
.
send_message
command:
'subscribe'
,
identifier:
JSON
.
dump
(
channel:
'EchoChannel'
)
assert_equal
({
"identifier"
=>
"{
\"
channel
\"
:
\"
EchoChannel
\"
}"
,
"type"
=>
"confirm_subscription"
},
c
.
read_message
)
c
.
send_message
command:
'message'
,
identifier:
JSON
.
dump
(
channel:
'EchoChannel'
),
data:
JSON
.
dump
(
action:
'delay'
,
message:
'hello'
)
c
.
close
# disappear before write
c
=
faye_client
(
port
)
assert_equal
({
"type"
=>
"welcome"
},
c
.
read_message
)
# pop the first welcome message off the stack
c
.
send_message
command:
'subscribe'
,
identifier:
JSON
.
dump
(
channel:
'EchoChannel'
)
assert_equal
({
"identifier"
=>
"{
\"
channel
\"
:
\"
EchoChannel
\"
}"
,
"type"
=>
"confirm_subscription"
},
c
.
read_message
)
c
.
send_message
command:
'message'
,
identifier:
JSON
.
dump
(
channel:
'EchoChannel'
),
data:
JSON
.
dump
(
action:
'ding'
,
message:
'hello'
)
...
...
@@ -214,6 +219,7 @@ def test_unsubscribe_client
identifier
=
JSON
.
dump
(
channel:
'EchoChannel'
)
c
=
faye_client
(
port
)
assert_equal
({
"type"
=>
"welcome"
},
c
.
read_message
)
c
.
send_message
command:
'subscribe'
,
identifier:
identifier
assert_equal
({
"identifier"
=>
"{
\"
channel
\"
:
\"
EchoChannel
\"
}"
,
"type"
=>
"confirm_subscription"
},
c
.
read_message
)
assert_equal
(
1
,
app
.
connections
.
count
)
...
...
@@ -232,6 +238,7 @@ def test_unsubscribe_client
def
test_server_restart
with_puma_server
do
|
port
|
c
=
faye_client
(
port
)
assert_equal
({
"type"
=>
"welcome"
},
c
.
read_message
)
c
.
send_message
command:
'subscribe'
,
identifier:
JSON
.
dump
(
channel:
'EchoChannel'
)
assert_equal
({
"identifier"
=>
"{
\"
channel
\"
:
\"
EchoChannel
\"
}"
,
"type"
=>
"confirm_subscription"
},
c
.
read_message
)
...
...
actioncable/test/connection/base_test.rb
浏览文件 @
fc1b32f8
...
...
@@ -56,7 +56,7 @@ def send_async(method, *args)
run_in_eventmachine
do
connection
=
open_connection
connection
.
websocket
.
expects
(
:transmit
).
with
({
identifier:
"_ping"
,
type:
"confirm_subscription
"
}.
to_json
)
connection
.
websocket
.
expects
(
:transmit
).
with
({
type:
"welcome
"
}.
to_json
)
connection
.
message_buffer
.
expects
(
:process!
)
connection
.
process
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录