Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Simplified client DSL

  • Loading branch information...
commit 71ff4794c1768f741a4c4314d7ef05cb05cfd4d1 1 parent a940c35
Tony Arcieri authored
Showing with 12 additions and 4 deletions.
  1. +1 −2  examples/dslified_echo_client.rb
  2. +11 −2 lib/cool.io/dsl.rb
View
3  examples/dslified_echo_client.rb
@@ -6,7 +6,7 @@
ADDR = '127.0.0.1'
PORT = 4321
-cool.io.connection :echo_client_connection do
+cool.io.connect ADDR, PORT do
on_connect do
puts "Connected to #{remote_host}:#{remote_port}"
write "bounce this back to me"
@@ -31,5 +31,4 @@
end
puts "Echo client connecting to #{ADDR}:#{PORT}..."
-cool.io.connect ADDR, PORT, :echo_client_connection
cool.io.run
View
13 lib/cool.io/dsl.rb
@@ -19,8 +19,17 @@ def run
end
# Connect to the given host and port using the given connection class
- def connect(host, port, connection_name, *initializer_args)
- klass = self[connection_name]
+ def connect(host, port, connection_name = nil, *initializer_args, &block)
+ if block_given?
+ initializer_args.unshift connection_name if connection_name
+
+ klass = Class.new Cool.io::TCPSocket
+ connection_builder = ConnectionBuilder.new klass
+ connection_builder.instance_eval &block
+ else
+ raise ArgumentError, "no connection name or block given" unless connection_name
+ klass = self[connection_name]
+ end
client = klass.connect host, port, *initializer_args
client.attach Cool.io::Loop.default
Please sign in to comment.
Something went wrong with that request. Please try again.