Class: OpenC3::WebSocketClientStream

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

Constant Summary

Constants inherited from TcpipSocketStream

TcpipSocketStream::FAST_READ

Instance Attribute Summary collapse

Attributes inherited from TcpipSocketStream

#write_socket

Instance Method Summary collapse

Methods inherited from TcpipSocketStream

#connected?, #disconnect, #read_nonblock

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|nil)

    Number of seconds to wait for the write to complete or nil to block until the socket is ready to write.

  • read_timeout (Float|nil)

    Number of seconds to wait for the read to complete or nil to block until the socket is ready to read.



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

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



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

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



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

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



93
94
95
96
# File 'lib/openc3/streams/web_socket_client_stream.rb', line 93

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