From 840a118cd50c29b6688bf0aaea760ca47fc2ddb4 Mon Sep 17 00:00:00 2001 From: James Coglan Date: Sun, 12 Feb 2012 23:53:32 +0000 Subject: [PATCH] Only send EventSource ping messages if the user specifies an interval, for symmetry with WebSocket. --- lib/faye/eventsource.rb | 12 ++++++------ lib/faye/websocket.rb | 9 ++++----- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/lib/faye/eventsource.rb b/lib/faye/eventsource.rb index a69af1c..70c3f82 100644 --- a/lib/faye/eventsource.rb +++ b/lib/faye/eventsource.rb @@ -2,7 +2,6 @@ require File.expand_path('../websocket', __FILE__) unless defined?(Faye::WebSock module Faye class EventSource - DEFAULT_PING = 10 DEFAULT_RETRY = 5 include WebSocket::API @@ -26,12 +25,14 @@ module Faye def initialize(env, options = {}) @env = env - @ping = options[:ping] || DEFAULT_PING + @ping = options[:ping] @retry = (options[:retry] || DEFAULT_RETRY).to_f @url = EventSource.determine_url(env) @stream = Stream.new(self) @ready_state = CONNECTING + @send_buffer = [] + EventMachine.next_tick { open } callback = @env['async.callback'] callback.call([101, {}, @stream]) @@ -42,10 +43,9 @@ module Faye "\r\n\r\n" + "retry: #{ (@retry * 1000).floor }\r\n\r\n") - @send_buffer = [] - EventMachine.next_tick { open } - - @ping_timer = EventMachine.add_periodic_timer(@ping) { ping } + if @ping + @ping_timer = EventMachine.add_periodic_timer(@ping) { ping } + end end def last_event_id diff --git a/lib/faye/websocket.rb b/lib/faye/websocket.rb index 505f17f..045fabb 100644 --- a/lib/faye/websocket.rb +++ b/lib/faye/websocket.rb @@ -92,8 +92,10 @@ module Faye include API def initialize(env, supported_protos = nil, options = {}) - @env = env - @stream = Stream.new(self) + @env = env + @stream = Stream.new(self) + @ping = options[:ping] + @ping_id = 0 @url = WebSocket.determine_url(@env) @ready_state = CONNECTING @@ -110,9 +112,6 @@ module Faye @ready_state = OPEN if @parser.open? - @ping = options[:ping] - @ping_id = 0 - if @ping @ping_timer = EventMachine.add_periodic_timer(@ping) do @ping_id += 1