# frozen_string_literal: true require 'uri' module WebSocket module Handshake # Construct or parse a client WebSocket handshake. # # @example # @handshake = WebSocket::Handshake::Client.new(url: 'ws://example.com') # # # Create request # @handshake.to_s # GET /demo HTTP/1.1 # # Upgrade: websocket # # Connection: Upgrade # # Host: example.com # # Origin: http://example.com # # Sec-WebSocket-Version: 13 # # Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ== # # # Parse server response # @handshake << <] :protocols An array of supported sub-protocols # @option args [Integer] :version Version of WebSocket to use. Default: 13 (this is version from RFC) # @option args [Hash] :headers HTTP headers to use in the handshake # # @example # Websocket::Handshake::Client.new(url: "ws://example.com/path?query=true") def initialize(args = {}) super if @url || @uri uri = URI.parse(@url || @uri) @secure ||= (uri.scheme == 'wss') @host ||= uri.host @port ||= uri.port || default_port @path ||= uri.path @query ||= uri.query end @path = '/' if @path.nil? || @path.empty? @version ||= DEFAULT_VERSION raise WebSocket::Error::Handshake::NoHostProvided unless @host include_version end rescue_method :initialize # Add text of response from Server. This method will parse content immediately and update state and error(if neccessary) # # @param [String] data Data to add # # @example # @handshake << <