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.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/openc3/streams/web_socket_client_stream.rb', line 34

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.



26
27
28
# File 'lib/openc3/streams/web_socket_client_stream.rb', line 26

def headers
  @headers
end

Instance Method Details

#connectObject



50
51
52
53
54
55
56
57
58
# File 'lib/openc3/streams/web_socket_client_stream.rb', line 50

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



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
89
90
91
92
93
# File 'lib/openc3/streams/web_socket_client_stream.rb', line 60

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



95
96
97
98
# File 'lib/openc3/streams/web_socket_client_stream.rb', line 95

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