class DEBUGGER__::UI_CDP::WebSocketClient

def handshake port, path

def handshake port, path
  key = SecureRandom.hex(11)
  req = "GET #{path} HTTP/1.1\r\nHost: 127.0.0.1:#{port}\r\nConnection: Upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Version: 13\r\nSec-WebSocket-Key: #{key}==\r\n\r\n"
  show_protocol :>, req
  @sock.print req
  res = @sock.readpartial 4092
  show_protocol :<, res
  if res.match /^Sec-WebSocket-Accept: (.*)\r\n/
    correct_key = Base64.strict_encode64 Digest::SHA1.digest "#{key}==258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
    raise "The Sec-WebSocket-Accept value: #{$1} is not valid" unless $1 == correct_key
  else
    raise "Unknown response: #{res}"
  end
end