require "http/web_socket" require "option_parser" require "./colors" require "./utils" require "./ws" uri = "ws://localhost:1234/pong" OptionParser.parse! do |parser| parser.on "-u uri", "--uri uri", "URI" do |opturi| uri = opturi 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) m = ws.read 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) ws.send m end def send(ws : WebSocket, m : Slice) ws.send m end begin ws = WebSocket.new(URI.parse(uri)) ws.on_close do |socket| puts "socket is closing" exit 0 end read ws send ws, "pong" read ws send ws, to_message(2, "coucou") read ws ws.close ws.read rescue e puts "Exception: #{e}" end