Sha256: 10dfeca709f850d1e32e39ff1ea5c8816f31b70b071b32e9a49d8dae8d0c647f

Contents?: true

Size: 908 Bytes

Versions: 1

Compression:

Stored size: 908 Bytes

Contents

#!/usr/bin/env ruby

require 'async'
require 'async/io/stream'
require 'async/http/url_endpoint'
require 'async/websocket/client'

USER = ARGV.pop || "anonymous"
URL = ARGV.pop || "ws://localhost:9292"

Async do |task|
	stdin = Async::IO::Stream.new(
		Async::IO::Generic.new($stdin)
	)
	
	endpoint = Async::HTTP::URLEndpoint.parse(URL)
	
	endpoint.with(local_address: local_address).connect do |socket|
		connection = Async::WebSocket::Client.new(socket, URL)
		
		connection.send_message({
			user: USER,
			status: "connected",
		})
		
		task.async do
			puts "Waiting for input..."
			begin
				while line = stdin.read_until("\n")
					puts "Sending text: #{line}"
					connection.send_message({
						user: USER,
						text: line,
					})
				end
			rescue
				puts "Client error: #{$!}"
			end
		end
		
		while message = connection.next_message
			puts "From server: #{message.inspect}"
		end
	end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
async-websocket-0.9.0 examples/middleware/client.rb