Class: OpenC3::WebSocketClientStream

Inherits:
TcpipClientStream show all
Defined in:
lib/openc3/streams/web_socket_client_stream.rb

Instance Attribute Summary collapse

Attributes inherited from TcpipSocketStream

#write_socket

Instance Method Summary collapse

Methods inherited from TcpipSocketStream

#connected?, #disconnect, #read_nonblock, #set_option

Methods inherited from Stream

#connected?, #disconnect, #read_nonblock

Constructor Details

#initialize(url, write_timeout, read_timeout, connect_timeout = 5.0) ⇒ WebSocketClientStream

Returns a new instance of WebSocketClientStream.

Parameters:

  • url (String)

    The host to connect to

  • write_timeout (Float)

    Seconds to wait before aborting writes

  • read_timeout (Float|nil)

    Seconds to wait before aborting reads. Pass nil to block until the read is complete.

  • connect_timeout (Float|nil) (defaults to: 5.0)

    Seconds to wait before aborting connect. Pass nil to block until the connection is complete.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/openc3/streams/web_socket_client_stream.rb', line 29

def initialize(url, write_timeout, read_timeout, connect_timeout = 5.0)
  @url = url
  @uri = URI.parse @url
  port = ((@uri.scheme == 'wss' or @uri.scheme == 'https') ? 443 : 80)
  port = @uri.port if @uri.port
  super(@uri.host, port, port, write_timeout, read_timeout, connect_timeout)
  if ['https', 'wss'].include? @uri.scheme
    socket = ::OpenSSL::SSL::SSLSocket.new(@write_socket)
    socket.sync_close = true
    socket.hostname = @uri.host
    @write_socket = socket
    @read_socket = socket
  end
  @headers = {}
end

Instance Attribute Details

#headersObject

Returns the value of attribute headers.



21
22
23
# File 'lib/openc3/streams/web_socket_client_stream.rb', line 21

def headers
  @headers
end

Instance Method Details

#connectObject



45
46
47
48
49
50
51
52
53
# File 'lib/openc3/streams/web_socket_client_stream.rb', line 45

def connect
  super()
  @handshake = ::WebSocket::Handshake::Client.new(:url => @url, :headers => @headers)
  @frame = ::WebSocket::Frame::Incoming::Client.new
  @handshaked = false
  @write_socket.write(@handshake.to_s)
  read() # This should wait for the handshake
  return true
end

#readObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/openc3/streams/web_socket_client_stream.rb', line 55

def read
  while true
    if @handshaked
      msg = @frame.next
      return msg.data if msg
    end

    data = super()
    return data if data.length <= 0

    if @handshaked
      @frame << data
      msg = @frame.next
      return msg.data if msg
    else
      index = 0
      chars = ""
      data.each_char do |char|
        @handshake << char
        chars << char
        index += 1
        if @handshake.finished?
          @handshaked = true
          break
        end
      end
      if @handshaked
        data = data[index..-1]
        @frame << data
        return
      end
    end
  end
end

#write(data, type: :text) ⇒ Object



90
91
92
93
# File 'lib/openc3/streams/web_socket_client_stream.rb', line 90

def write(data, type: :text)
  frame = ::WebSocket::Frame::Outgoing::Client.new(:data => data, :type => type, :version => @handshake.version)
  super(frame.to_s)
end