require "http/web_socket" require "option_parser" require "ipc" require "./colors" require "./utils" require "./lib_modifications.cr" class CLI class_property uri = "ws://localhost:1234/pong" class_property rounds = 1 end OptionParser.parse do |parser| parser.on "-u uri", "--uri uri", "URI" do |opturi| CLI.uri = opturi end parser.on "-r rounds", "--rounds nb-messages", "Nb messages to send." do |opt| CLI.rounds = opt.to_i end parser.on "-h", "--help", "Show this help" do puts parser exit 0 end end def read_then_print(ws : WebSocket) m = read ws puts "message: #{String.new(m)}" end def read_then_print_hexa(ws : WebSocket) m = read ws print_hexa(String.new(m), "#{CBLUE}Received message hexa#{CRESET}") end def read(ws : WebSocket) puts "reading a message" m = ws.run_once if m.nil? raise "empty message" end m end def send_with_announce(ws : WebSocket, m : String) puts "sending #{m}" send ws, m end def send(ws : WebSocket, m : String | Slice) ws.send m end begin ws = WebSocket.new(URI.parse(CLI.uri)) puts "connection done: sending pong" ws.on_close do |socket| puts "socket is closing" exit 0 end message = if CLI.uri.ends_with? ".JSON" IPC::Message.new(0, 2.to_u8, 3.to_u8, STDIN.gets_to_end).to_json else IPC::Message.new(0, 2.to_u8, 3.to_u8, STDIN.gets_to_end).to_packet end puts "final message: #{message}" CLI.rounds.times do |i| send ws, message pp! read ws end ws.close # pp! ws.run_once rescue e puts "Exception: #{e}" end