websocketc++
This commit is contained in:
parent
f74898e2cf
commit
3986187c2c
@ -1,14 +1,23 @@
|
|||||||
require "http/web_socket"
|
require "http/web_socket"
|
||||||
require "option_parser"
|
require "option_parser"
|
||||||
|
|
||||||
|
require "ipc"
|
||||||
require "./colors"
|
require "./colors"
|
||||||
require "./utils"
|
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|
|
OptionParser.parse do |parser|
|
||||||
parser.on "-u uri", "--uri uri", "URI" do |opturi|
|
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
|
end
|
||||||
|
|
||||||
parser.on "-h", "--help", "Show this help" do
|
parser.on "-h", "--help", "Show this help" do
|
||||||
@ -28,7 +37,8 @@ def read_then_print_hexa(ws : WebSocket)
|
|||||||
end
|
end
|
||||||
|
|
||||||
def read(ws : WebSocket)
|
def read(ws : WebSocket)
|
||||||
m = ws.read
|
puts "reading a message"
|
||||||
|
m = ws.run_once
|
||||||
if m.nil?
|
if m.nil?
|
||||||
raise "empty message"
|
raise "empty message"
|
||||||
end
|
end
|
||||||
@ -46,8 +56,7 @@ def send(ws : WebSocket, m : String | Slice)
|
|||||||
end
|
end
|
||||||
|
|
||||||
begin
|
begin
|
||||||
ws = WebSocket.new(URI.parse(uri))
|
ws = WebSocket.new(URI.parse(CLI.uri))
|
||||||
|
|
||||||
puts "connection done: sending pong"
|
puts "connection done: sending pong"
|
||||||
|
|
||||||
ws.on_close do |socket|
|
ws.on_close do |socket|
|
||||||
@ -55,22 +64,20 @@ begin
|
|||||||
exit 0
|
exit 0
|
||||||
end
|
end
|
||||||
|
|
||||||
if uri.ends_with? ".JSON"
|
message = if CLI.uri.ends_with? ".JSON"
|
||||||
json_message = STDIN.gets_to_end
|
IPC::Message.new(0, 2.to_u8, 3.to_u8, STDIN.gets_to_end).to_json
|
||||||
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
|
|
||||||
else
|
else
|
||||||
send ws, to_message(2, STDIN.gets_to_end)
|
IPC::Message.new(0, 2.to_u8, 3.to_u8, STDIN.gets_to_end).to_packet
|
||||||
read ws
|
end
|
||||||
|
|
||||||
|
puts "final message: #{message}"
|
||||||
|
CLI.rounds.times do |i|
|
||||||
|
send ws, message
|
||||||
|
pp! read ws
|
||||||
end
|
end
|
||||||
|
|
||||||
ws.close
|
ws.close
|
||||||
ws.read
|
# pp! ws.run_once
|
||||||
rescue e
|
rescue e
|
||||||
puts "Exception: #{e}"
|
puts "Exception: #{e}"
|
||||||
end
|
end
|
||||||
|
@ -290,7 +290,7 @@ def main
|
|||||||
|
|
||||||
service.loop do |event|
|
service.loop do |event|
|
||||||
# info "current state of the context:"
|
# info "current state of the context:"
|
||||||
service.pp
|
# service.pp
|
||||||
case event
|
case event
|
||||||
when IPC::Event::Timer
|
when IPC::Event::Timer
|
||||||
info "#{CORANGE}IPC::Event::Timer#{CRESET}"
|
info "#{CORANGE}IPC::Event::Timer#{CRESET}"
|
||||||
|
Loading…
Reference in New Issue
Block a user