Obsolete
/
ipcd
Archived
3
0
Fork 0

websockets: handle errors

master
Philippe PITTOLI 2019-08-05 01:52:54 +02:00
parent 16daa80d5d
commit 75197d34e3
2 changed files with 62 additions and 57 deletions

View File

@ -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`
# 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)}" puts "message: #{String.new(m)}"
end
# ws.on_message do |message| def read_then_print_hexa(ws : WebSocket)
# if message == "are we websocket yet?" m = read ws
# puts "sending pong" print_hexa(String.new(m), "#{CBLUE}Received message hexa#{CRESET}")
# ws.send "pong\n" end
# elsif message.chomp == "OK"
# puts "sending coucou" def read(ws : WebSocket)
# m = to_message 2, "coucou" m = ws.read
# print_hexa(String.new(m), "#{CBLUE}Sending message hexa#{CRESET}") if m.nil?
# ws.send m raise "empty message"
# else end
# print_hexa message, "#{CORANGE}Receving a message#{CRESET}"
# end m
# end end
def send_with_announce(ws : WebSocket, m : String)
puts "sending #{m}"
send ws, m
end
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| ws.on_close do |socket|
puts "socket is closing" puts "socket is closing"
# socket.close exit 0
end end
# ws.run read ws
send ws, "pong"
read ws
send ws, to_message(2, "coucou")
read ws
ws.close ws.close
ws.read ws.read
rescue e
puts "Exception: #{e}"
end

View File

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