Sha256: 63c0e089df2ecaeb356d125da4aabe1bd768f4a82dd93f8e26dc2388cf5dee1d
Contents?: true
Size: 1.6 KB
Versions: 7
Compression:
Stored size: 1.6 KB
Contents
# frozen_string_literal: true require 'socket' module Whatup module Client class Client include Thor::Shell def initialize ip:, port: @dest = { ip: ip, port: port, address: "#{@ip}:#{@port}" } end def connect say "Connecting to #{@dest[:ip]}:#{@dest[:port]} ..." @socket = TCPSocket.open @dest[:ip], @dest[:port] puts 'Please enter your username to establish a connection...' @request = request! @response = listen! [@request, @response].each &:join rescue SignalException say 'Exiting ...', :red exit end private # Loop and send all input to the server def request! Thread.new do loop do input = Readline.readline '~> ', true next if input.nil? @socket.puts input end end rescue IOError => e puts e.message @socket.close end # Continually listen to the server, and print anything received def listen! Thread.new do loop do response = @socket.gets&.chomp if response == 'END' puts kill_all_but_current_thread! exit end puts response # unless response.nil? end end rescue IOError => e puts e.message @socket.close end def kill_all_but_current_thread! Thread.list.each do |thread| thread.exit unless thread == Thread.current end end end end end
Version data entries
7 entries across 7 versions & 1 rubygems