Skip to content

Commit

Permalink
Merge pull request #7 from picatz/fix-tabs-readme-example
Browse files Browse the repository at this point in the history
Change README example tabs to ruby style 2 spaces
  • Loading branch information
ioquatix committed Apr 10, 2017
2 parents f935846 + fcf31a7 commit 6184c29
Showing 1 changed file with 29 additions and 29 deletions.
58 changes: 29 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,41 +48,41 @@ require 'async'
require 'async/tcp_socket'

def echo_server
Async::Reactor.run do |task|
# This is a synchronous block within the current task:
task.with(TCPServer.new('localhost', 9000)) do |server|
# This is an asynchronous block within the current reactor:
task.reactor.with(server.accept) do |client|
data = client.read(512)
task.sleep(rand)
client.write(data)
end while true
end
end
Async::Reactor.run do |task|
# This is a synchronous block within the current task:
task.with(TCPServer.new('localhost', 9000)) do |server|
# This is an asynchronous block within the current reactor:
task.reactor.with(server.accept) do |client|
data = client.read(512)
task.sleep(rand)
client.write(data)
end while true
end
end
end

def echo_client(data)
Async::Reactor.run do |task|
Async::TCPServer.connect('localhost', 9000) do |socket|
socket.write(data)
puts "echo_client: #{socket.read(512)}"
end
end
Async::Reactor.run do |task|
Async::TCPServer.connect('localhost', 9000) do |socket|
socket.write(data)
puts "echo_client: #{socket.read(512)}"
end
end
end

Async::Reactor.run do
# Start the echo server:
server = echo_server
5.times.collect do |i|
echo_client("Hello World #{i}")
end.each(&:wait) # Wait until all clients are finished.
# Terminate the server and all tasks created within it's async scope:
server.stop
# Start the echo server:
server = echo_server
5.times.collect do |i|
echo_client("Hello World #{i}")
end.each(&:wait) # Wait until all clients are finished.
# Terminate the server and all tasks created within it's async scope:
server.stop
end
```

Expand Down

0 comments on commit 6184c29

Please sign in to comment.