1
0
Fork 0
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:
Daniel Rhodes 2016-03-01 02:48:15 +01:00
parent f51cb7eef5
commit 53e163dc3f
6 changed files with 10 additions and 15 deletions

View file

@ -73,10 +73,11 @@ class ActionCable.Connection
events: events:
message: (event) -> message: (event) ->
{identifier, message, type} = JSON.parse(event.data) {identifier, message, type} = JSON.parse(event.data)
switch type switch type
when message_types.welcome when message_types.welcome
@consumer.subscriptions.notify(@consumer.connectionMonitor, "connected") @consumer.connectionMonitor.connected()
when message_types.ping
@consumer.connectionMonitor.ping()
when message_types.confirmation when message_types.confirmation
@consumer.subscriptions.notify(identifier, "connected") @consumer.subscriptions.notify(identifier, "connected")
when message_types.rejection when message_types.rejection

View file

@ -7,10 +7,7 @@ class ActionCable.ConnectionMonitor
@staleThreshold: 6 # Server::Connections::BEAT_INTERVAL * 2 (missed two pings) @staleThreshold: 6 # Server::Connections::BEAT_INTERVAL * 2 (missed two pings)
identifier: ActionCable.INTERNAL.identifiers.ping
constructor: (@consumer) -> constructor: (@consumer) ->
@consumer.subscriptions.add(this)
@start() @start()
connected: -> connected: ->
@ -22,11 +19,12 @@ class ActionCable.ConnectionMonitor
disconnected: -> disconnected: ->
@disconnectedAt = now() @disconnectedAt = now()
received: -> ping: ->
@pingedAt = now() @pingedAt = now()
reset: -> reset: ->
@reconnectAttempts = 0 @reconnectAttempts = 0
@consumer.connection.isOpen()
start: -> start: ->
@reset() @reset()

View file

@ -58,7 +58,5 @@ class ActionCable.Subscriptions
sendCommand: (subscription, command) -> sendCommand: (subscription, command) ->
{identifier} = subscription {identifier} = subscription
if identifier is ActionCable.INTERNAL.identifiers.ping @consumer.send({command, identifier})
@consumer.connection.isOpen()
else
@consumer.send({command, identifier})

View file

@ -29,11 +29,9 @@ module ActionCable
extend ActiveSupport::Autoload extend ActiveSupport::Autoload
INTERNAL = { INTERNAL = {
identifiers: {
ping: '_ping'.freeze
},
message_types: { message_types: {
welcome: 'welcome'.freeze, welcome: 'welcome'.freeze,
ping: 'ping'.freeze,
confirmation: 'confirm_subscription'.freeze, confirmation: 'confirm_subscription'.freeze,
rejection: 'reject_subscription'.freeze rejection: 'reject_subscription'.freeze
} }

View file

@ -114,7 +114,7 @@ module ActionCable
end end
def beat 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 end
def on_open # :nodoc: def on_open # :nodoc:

View file

@ -75,7 +75,7 @@ class ClientTest < ActionCable::TestCase
@ws.on(:message) do |event| @ws.on(:message) do |event|
hash = JSON.parse(event.data) hash = JSON.parse(event.data)
if hash['identifier'] == '_ping' if hash['type'] == 'ping'
@pings += 1 @pings += 1
else else
@messages << hash @messages << hash