websockets: handle errors
parent
16daa80d5d
commit
75197d34e3
|
@ -19,62 +19,55 @@ OptionParser.parse! do |parser|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
ws = WebSocket.new(URI.parse(uri))
|
def read_then_print(ws : WebSocket)
|
||||||
|
m = read ws
|
||||||
# HTTP::WebSocket.new(URI.parse("wss://websocket.example.com/chat")) # Creates a new WebSocket with TLS to `websocket.example.com`
|
puts "message: #{String.new(m)}"
|
||||||
# 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
|
end
|
||||||
# puts "message: #{String.new(m)}"
|
|
||||||
|
|
||||||
# puts "sending pong"
|
def read_then_print_hexa(ws : WebSocket)
|
||||||
ws.send "pong".to_slice
|
m = read ws
|
||||||
m = ws.read
|
print_hexa(String.new(m), "#{CBLUE}Received message hexa#{CRESET}")
|
||||||
if m.nil?
|
|
||||||
puts "oh no"
|
|
||||||
exit
|
|
||||||
end
|
end
|
||||||
# puts "message: #{String.new(m)}"
|
|
||||||
|
|
||||||
# puts "sending coucou"
|
def read(ws : WebSocket)
|
||||||
tosend = to_message 2, "coucou"
|
m = ws.read
|
||||||
# print_hexa(String.new(tosend), "#{CBLUE}Sending message hexa#{CRESET}")
|
if m.nil?
|
||||||
ws.send tosend
|
raise "empty message"
|
||||||
|
end
|
||||||
|
|
||||||
m = ws.read
|
m
|
||||||
if m.nil?
|
|
||||||
puts "oh no"
|
|
||||||
exit
|
|
||||||
end
|
end
|
||||||
puts "message: #{String.new(m)}"
|
|
||||||
|
|
||||||
# ws.on_message do |message|
|
def send_with_announce(ws : WebSocket, m : String)
|
||||||
# if message == "are we websocket yet?"
|
puts "sending #{m}"
|
||||||
# puts "sending pong"
|
send ws, m
|
||||||
# ws.send "pong\n"
|
end
|
||||||
# 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|
|
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"
|
puts "socket is closing"
|
||||||
# socket.close
|
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
|
end
|
||||||
|
|
||||||
# ws.run
|
|
||||||
|
|
||||||
ws.close
|
|
||||||
|
|
||||||
ws.read
|
|
||||||
|
|
|
@ -208,7 +208,13 @@ def websocket_switching_procedure (activefd : Int, context : InstanceStorage)
|
||||||
# The client is a WebSocket on top of a TCP connection
|
# The client is a WebSocket on top of a TCP connection
|
||||||
client = context.socklist[activefd]
|
client = context.socklist[activefd]
|
||||||
wsclient = context.wssocklist[activefd]
|
wsclient = context.wssocklist[activefd]
|
||||||
|
begin
|
||||||
message = wsclient.read
|
message = wsclient.read
|
||||||
|
rescue e
|
||||||
|
puts "#{CRED}Exception (receiving a message)#{CRESET} #{e}"
|
||||||
|
closing_client activefd, context
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
if wsclient.closed?
|
if wsclient.closed?
|
||||||
# puts "#{CBLUE}client is closed#{CRESET}"
|
# puts "#{CBLUE}client is closed#{CRESET}"
|
||||||
|
@ -290,7 +296,13 @@ service.loop do |event|
|
||||||
# since it's an external communication
|
# since it's an external communication
|
||||||
# we have to read the message here, it's not handled by default in libipc
|
# we have to read the message here, it's not handled by default in libipc
|
||||||
wsclient = context.wssocklist[activefd]
|
wsclient = context.wssocklist[activefd]
|
||||||
|
begin
|
||||||
message = wsclient.read
|
message = wsclient.read
|
||||||
|
rescue e
|
||||||
|
puts "#{CRED}Exception (receiving a message)#{CRESET} #{e}"
|
||||||
|
closing_client activefd, context
|
||||||
|
next
|
||||||
|
end
|
||||||
|
|
||||||
if message.nil?
|
if message.nil?
|
||||||
puts "#{CBLUE}disconnection of client#{CRESET} #{event.connection.fd}"
|
puts "#{CBLUE}disconnection of client#{CRESET} #{event.connection.fd}"
|
||||||
|
|
Reference in New Issue