lib/websocket/eventmachine/client.rb in websocket-eventmachine-client-1.0.1 vs lib/websocket/eventmachine/client.rb in websocket-eventmachine-client-1.1.0

- old
+ new

@@ -18,30 +18,39 @@ # Connect to websocket server # @param args [Hash] The request arguments # @option args [String] :host The host IP/DNS name # @option args [Integer] :port The port to connect too(default = 80) # @option args [Integer] :version Version of protocol to use(default = 13) + # @option args [Hash] :headers HTTP headers to use in the handshake + # @option args [Boolean] :ssl Force SSL/TLS connection def self.connect(args = {}) host = nil port = nil if args[:uri] uri = URI.parse(args[:uri]) host = uri.host port = uri.port + args[:ssl] = true if uri.scheme == 'wss' end host = args[:host] if args[:host] port = args[:port] if args[:port] - port ||= 80 + if args[:ssl] + port ||= 443 + else + port ||= 80 + end ::EventMachine.connect host, port, self, args end # Initialize connection # @param args [Hash] Arguments for connection # @option args [String] :host The host IP/DNS name # @option args [Integer] :port The port to connect too(default = 80) # @option args [Integer] :version Version of protocol to use(default = 13) + # @option args [Hash] :headers HTTP headers to use in the handshake + # @option args [Boolean] :ssl Force SSL/TLS connection def initialize(args) @args = args end ############################ @@ -55,13 +64,25 @@ @state = :connecting @handshake = ::WebSocket::Handshake::Client.new(@args) end # Called by EventMachine after connecting. - # Sends handshake to server + # Sends handshake to server or starts SSL/TLS # Eventmachine internal # @private def connection_completed + if @args[:ssl] + start_tls + else + send(@handshake.to_s, :type => :plain) + end + end + + # Called by EventMachine after SSL/TLS handshake. + # Sends websocket handshake + # Eventmachine internal + # @private + def ssl_handshake_completed send(@handshake.to_s, :type => :plain) end private