mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Make ping into a message type
This change makes ping into a message type, which makes the whole protocol a lot more consistent. Also fixes hacks on the client side to make this all work.
This commit is contained in:
parent
f51cb7eef5
commit
53e163dc3f
6 changed files with 10 additions and 15 deletions
|
@ -73,10 +73,11 @@ class ActionCable.Connection
|
|||
events:
|
||||
message: (event) ->
|
||||
{identifier, message, type} = JSON.parse(event.data)
|
||||
|
||||
switch type
|
||||
when message_types.welcome
|
||||
@consumer.subscriptions.notify(@consumer.connectionMonitor, "connected")
|
||||
@consumer.connectionMonitor.connected()
|
||||
when message_types.ping
|
||||
@consumer.connectionMonitor.ping()
|
||||
when message_types.confirmation
|
||||
@consumer.subscriptions.notify(identifier, "connected")
|
||||
when message_types.rejection
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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})
|
||||
|
||||
|
|
|
@ -29,11 +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
|
||||
}
|
||||
|
|
|
@ -114,7 +114,7 @@ module ActionCable
|
|||
end
|
||||
|
||||
def beat
|
||||
transmit ActiveSupport::JSON.encode(identifier: ActionCable::INTERNAL[:identifiers][:ping], message: Time.now.to_i)
|
||||
transmit ActiveSupport::JSON.encode(type: ActionCable::INTERNAL[:message_types][:ping], message: Time.now.to_i)
|
||||
end
|
||||
|
||||
def on_open # :nodoc:
|
||||
|
|
|
@ -75,7 +75,7 @@ class ClientTest < ActionCable::TestCase
|
|||
|
||||
@ws.on(:message) do |event|
|
||||
hash = JSON.parse(event.data)
|
||||
if hash['identifier'] == '_ping'
|
||||
if hash['type'] == 'ping'
|
||||
@pings += 1
|
||||
else
|
||||
@messages << hash
|
||||
|
|
Loading…
Reference in a new issue