examples/chat/client.rb in async-websocket-0.8.0 vs examples/chat/client.rb in async-websocket-0.9.0

- old
+ new

@@ -1,41 +1,32 @@ #!/usr/bin/env ruby require 'async' require 'async/io/stream' require 'async/http/url_endpoint' -require 'async/websocket/client' +require_relative '../../lib/async/websocket/client' USER = ARGV.pop || "anonymous" -URL = ARGV.pop || "ws://localhost:9292" +URL = ARGV.pop || "http://127.0.0.1:8080" +ENDPOINT = Async::HTTP::URLEndpoint.parse(URL) Async do |task| stdin = Async::IO::Stream.new( Async::IO::Generic.new($stdin) ) - endpoint = Async::HTTP::URLEndpoint.parse(URL) - - endpoint.connect do |socket| - connection = Async::WebSocket::Client.new(socket, URL) - - connection.send_message({ - user: USER, - status: "connected", - }) - - task.async do - puts "Waiting for input..." + Async::WebSocket::Client.open(ENDPOINT) do |connection| + input_task = task.async do while line = stdin.read_until("\n") - puts "Sending text: #{line}" - connection.send_message({ - user: USER, - text: line, - }) + connection.send_message({text: line}) + connection.flush end end + puts "Connected..." while message = connection.next_message - puts "From server: #{message.inspect}" + puts "> #{message.inspect}" end + ensure + input_task&.stop end end