Skip to content

Commit

Permalink
Better control process - can execute code.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Apr 27, 2019
1 parent e10dca0 commit 168f74a
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 12 deletions.
10 changes: 1 addition & 9 deletions examples/chat/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,9 @@
endpoint.connect do |socket|
connection = Async::WebSocket::Client.new(socket, URL)

connection.send_message({
user: USER,
status: "connected",
})

input_task = task.async do
while line = stdin.read_until("\n")
connection.send_message({
user: USER,
text: line,
})
connection.send_message({text: line})
end
end

Expand Down
22 changes: 21 additions & 1 deletion examples/chat/config.ru
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ class Room
@connections.each(&block)
end

def command(code)
Async.logger.warn self, "eval(#{code})"

eval(code)
end

def broadcast(message)
Async.logger.info "Broadcast: #{message.inspect}"
start_time = Async::Clock.now
Expand All @@ -45,7 +51,21 @@ class Room
self.connect(connection)

while message = connection.next_message
self.broadcast(message)
if message["text"] =~ /^\/(.*?)$/
begin
result = self.command($1)

if result.is_a? Hash
connection.send_message(result)
else
connection.send_message({result: result.inspect})
end
rescue
connection.send_message({error: $!.inspect})
end
else
self.broadcast(message)
end
end
ensure
self.disconnect(connection)
Expand Down
2 changes: 1 addition & 1 deletion examples/chat/multi-client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def call
count = @options[:count]

connections = Async::Queue.new
progress = TTY::ProgressBar.new("connections [:bar] :current/:total (:eta/:elapsed)", total: count)
progress = TTY::ProgressBar.new(":rate connection/s [:bar] :current/:total (:eta/:elapsed)", total: count)

Async do |task|
task.logger.info!
Expand Down
2 changes: 1 addition & 1 deletion examples/middleware/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

endpoint = Async::HTTP::URLEndpoint.parse(URL)

endpoint.connect do |socket|
endpoint.with(local_address: local_address).connect do |socket|
connection = Async::WebSocket::Client.new(socket, URL)

connection.send_message({
Expand Down

0 comments on commit 168f74a

Please sign in to comment.