websocketc++

dev
Karchnu 4 years ago
parent f74898e2cf
commit 3986187c2c

@ -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

@ -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}"

Loading…
Cancel
Save