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

Change README example tabs to ruby style 2 spaces #7

Merged
merged 1 commit into from
Apr 10, 2017
Merged
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
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