81 lines
1.7 KiB
Crystal
81 lines
1.7 KiB
Crystal
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
|
|
|
|
ws = WebSocket.new(URI.parse(uri))
|
|
|
|
# HTTP::WebSocket.new(URI.parse("wss://websocket.example.com/chat")) # Creates a new WebSocket with TLS to `websocket.example.com`
|
|
# HTTP::WebSocket.new(URI.parse("http://websocket.example.com:8080/chat")) # Creates a new WebSocket to `websocket.example.com` on port `8080`
|
|
# HTTP::WebSocket.new(URI.parse("ws://websocket.example.com/chat"), # Creates a new WebSocket to `websocket.example.com` with an Authorization header
|
|
# HTTP::Headers{"Authorization" => "Bearer authtoken"})
|
|
|
|
m = ws.read
|
|
if m.nil?
|
|
puts "oh no"
|
|
exit
|
|
end
|
|
# puts "message: #{String.new(m)}"
|
|
|
|
# puts "sending pong"
|
|
ws.send "pong".to_slice
|
|
m = ws.read
|
|
if m.nil?
|
|
puts "oh no"
|
|
exit
|
|
end
|
|
# puts "message: #{String.new(m)}"
|
|
|
|
# puts "sending coucou"
|
|
tosend = to_message 2, "coucou"
|
|
# print_hexa(String.new(tosend), "#{CBLUE}Sending message hexa#{CRESET}")
|
|
ws.send tosend
|
|
|
|
m = ws.read
|
|
if m.nil?
|
|
puts "oh no"
|
|
exit
|
|
end
|
|
puts "message: #{String.new(m)}"
|
|
|
|
# ws.on_message do |message|
|
|
# if message == "are we websocket yet?"
|
|
# puts "sending pong"
|
|
# ws.send "pong\n"
|
|
# elsif message.chomp == "OK"
|
|
# puts "sending coucou"
|
|
# m = to_message 2, "coucou"
|
|
# print_hexa(String.new(m), "#{CBLUE}Sending message hexa#{CRESET}")
|
|
# ws.send m
|
|
# else
|
|
# print_hexa message, "#{CORANGE}Receving a message#{CRESET}"
|
|
# end
|
|
# end
|
|
|
|
ws.on_close do |socket|
|
|
puts "socket is closing"
|
|
# socket.close
|
|
end
|
|
|
|
# ws.run
|
|
|
|
ws.close
|
|
|
|
ws.read
|