Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for #13 #21

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions em-twitter.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Gem::Specification.new do |spec|
spec.add_dependency 'http_parser.rb', '~> 0.6'
spec.add_dependency 'simple_oauth', '~> 0.2'
spec.add_dependency 'buftok', '~> 0.2'
spec.add_dependency 'em-connectify', '~> 0.2'

spec.add_development_dependency 'bundler', '~> 1.0'

Expand Down
10 changes: 7 additions & 3 deletions lib/em-twitter/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,18 @@ def initialize(options = {})

if @options[:proxy] && @options[:proxy][:uri]
proxy_uri = URI.parse(@options[:proxy][:uri])
@host = proxy_uri.host
@port = proxy_uri.port
@proxy_host = proxy_uri.host
@proxy_port = proxy_uri.port
end
@connection = nil
end

def connect
@connection = EM.connect(@host, @port, Connection, self, @host, @port)
if @proxy_host
@connection = EM.connect(@proxy_host, @proxy_port, Connection, self, @host, @port, @proxy_host, @proxy_port)
else
@connection = EM.connect(@host, @port, Connection, self, @host, @port, nil, nil)
end
end

def each(&block)
Expand Down
31 changes: 28 additions & 3 deletions lib/em-twitter/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,25 @@

require 'em-twitter/reconnectors/application_failure'
require 'em-twitter/reconnectors/network_failure'
require 'em-connectify'

module EventMachine
module Twitter
class Connection < EM::Connection
include EM::Connectify

STALL_TIMEOUT = 90 unless defined?(STALL_TIMEOUT)
STALL_TIMER = 10 unless defined?(STALL_TIMER)

attr_reader :host, :port, :client, :options, :headers
attr_accessor :reconnector

def initialize(client, host, port)
def initialize(client, host, port, proxy_host, proxy_port)
@client = client
@host = host
@port = port
@proxy_host = proxy_host
@proxy_port = proxy_port
@options = @client.options
@on_inited_callback = @options.delete(:on_inited)

Expand All @@ -42,6 +46,17 @@ def initialize(client, host, port)
# Called after the connection to the server is completed. Initiates a
#
def connection_completed
if @proxy_host
#connects to the host behind the proxy
connectify(@host, @port) do
start_stream
end
else
start_stream
end
end

def start_stream
start_tls(@options[:ssl]) if ssl?

reset_connection
Expand Down Expand Up @@ -286,9 +301,19 @@ def reconnect_after(reconnect_timeout)
@reconnector = @network_reconnector

if reconnect_timeout.zero?
reconnect(@host, @port)
if @proxy_host
reconnect(@proxy_host, @proxy_port)
else
reconnect(@host, @port)
end
else
EM::Timer.new(reconnect_timeout) { reconnect(@host, @port) }
EM::Timer.new(reconnect_timeout) {
if @proxy_host
reconnect(@proxy_host, @proxy_port)
else
reconnect(@host, @port)
end
}
end
end

Expand Down
7 changes: 6 additions & 1 deletion spec/em-twitter/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@
EventMachine::Twitter::Connection,
kind_of(EM::Twitter::Client),
test_options[:host],
test_options[:port])
test_options[:port],
nil,
nil
)
EM::Twitter::Client.connect(default_options)
end

Expand All @@ -36,6 +39,8 @@
8080,
EventMachine::Twitter::Connection,
kind_of(EM::Twitter::Client),
test_options[:host],
test_options[:port],
'my-proxy',
8080)
EM::Twitter::Client.connect(default_options.merge(proxy_options))
Expand Down