From 3986187c2c1644a3bde697d1ce379ff383231fdd Mon Sep 17 00:00:00 2001 From: Karchnu Date: Wed, 22 Jul 2020 10:14:27 +0200 Subject: [PATCH] websocketc++ --- src/websocketc.cr | 41 ++++++++++++++++++++++++----------------- src/websocketd.cr | 2 +- 2 files changed, 25 insertions(+), 18 deletions(-) diff --git a/src/websocketc.cr b/src/websocketc.cr index 9ff2fc9..b7ab133 100644 --- a/src/websocketc.cr +++ b/src/websocketc.cr @@ -1,14 +1,23 @@ require "http/web_socket" require "option_parser" +require "ipc" require "./colors" require "./utils" +require "./lib_modifications.cr" -uri = "ws://localhost:1234/pong" +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| - uri = 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 @@ -28,7 +37,8 @@ def read_then_print_hexa(ws : WebSocket) end def read(ws : WebSocket) - m = ws.read + puts "reading a message" + m = ws.run_once if m.nil? raise "empty message" end @@ -46,8 +56,7 @@ def send(ws : WebSocket, m : String | Slice) end begin - ws = WebSocket.new(URI.parse(uri)) - + ws = WebSocket.new(URI.parse(CLI.uri)) puts "connection done: sending pong" ws.on_close do |socket| @@ -55,22 +64,20 @@ begin exit 0 end - if uri.ends_with? ".JSON" - json_message = STDIN.gets_to_end - json_message = json_message.chomp - puts "json_message: #{json_message}" - final_json_message = "{ \"mtype\": 2, \"payload\": #{json_message} }" - puts "final json message: #{final_json_message}" - send ws, final_json_message - # send ws, "{ \"mtype\": 2, \"payload\": \"coucou\" }" - read ws + message = if CLI.uri.ends_with? ".JSON" + IPC::Message.new(0, 2.to_u8, 3.to_u8, STDIN.gets_to_end).to_json else - send ws, to_message(2, STDIN.gets_to_end) - read ws + 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 - ws.read + # pp! ws.run_once rescue e puts "Exception: #{e}" end diff --git a/src/websocketd.cr b/src/websocketd.cr index 6a0925a..4ea8ed5 100644 --- a/src/websocketd.cr +++ b/src/websocketd.cr @@ -290,7 +290,7 @@ def main service.loop do |event| # info "current state of the context:" - service.pp + # service.pp case event when IPC::Event::Timer info "#{CORANGE}IPC::Event::Timer#{CRESET}"