websockets: handle errors
parent
16daa80d5d
commit
75197d34e3
|
@ -19,62 +19,55 @@ OptionParser.parse! do |parser|
|
|||
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
|
||||
def read_then_print(ws : WebSocket)
|
||||
m = read ws
|
||||
puts "message: #{String.new(m)}"
|
||||
end
|
||||
# puts "message: #{String.new(m)}"
|
||||
|
||||
# puts "sending pong"
|
||||
ws.send "pong".to_slice
|
||||
m = ws.read
|
||||
if m.nil?
|
||||
puts "oh no"
|
||||
exit
|
||||
def read_then_print_hexa(ws : WebSocket)
|
||||
m = read ws
|
||||
print_hexa(String.new(m), "#{CBLUE}Received message hexa#{CRESET}")
|
||||
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
|
||||
def read(ws : WebSocket)
|
||||
m = ws.read
|
||||
if m.nil?
|
||||
raise "empty message"
|
||||
end
|
||||
|
||||
m = ws.read
|
||||
if m.nil?
|
||||
puts "oh no"
|
||||
exit
|
||||
m
|
||||
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
|
||||
def send_with_announce(ws : WebSocket, m : String)
|
||||
puts "sending #{m}"
|
||||
send ws, m
|
||||
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"
|
||||
# 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
|
||||
|
||||
# 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
|
||||
client = context.socklist[activefd]
|
||||
wsclient = context.wssocklist[activefd]
|
||||
begin
|
||||
message = wsclient.read
|
||||
rescue e
|
||||
puts "#{CRED}Exception (receiving a message)#{CRESET} #{e}"
|
||||
closing_client activefd, context
|
||||
return
|
||||
end
|
||||
|
||||
if wsclient.closed?
|
||||
# puts "#{CBLUE}client is closed#{CRESET}"
|
||||
|
@ -290,7 +296,13 @@ service.loop do |event|
|
|||
# since it's an external communication
|
||||
# we have to read the message here, it's not handled by default in libipc
|
||||
wsclient = context.wssocklist[activefd]
|
||||
begin
|
||||
message = wsclient.read
|
||||
rescue e
|
||||
puts "#{CRED}Exception (receiving a message)#{CRESET} #{e}"
|
||||
closing_client activefd, context
|
||||
next
|
||||
end
|
||||
|
||||
if message.nil?
|
||||
puts "#{CBLUE}disconnection of client#{CRESET} #{event.connection.fd}"
|
||||
|
|
Reference in New Issue